How to access the SDCARD on Android 4.4(API 19)

Asked

Viewed 676 times

5

Currently I have an application running on 100 phones with android 2.3(established by the client), but are changing the phones to 4.4 and use the sd card to store and then display as product listing.

The problem is in Android 4.4 I can’t create folders inside the sd card, I currently use the following code for checking and creating the folder.

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        
        file = new File(Environment.getExternalStorageDirectory() + File.separator + "imagem");
        file.mkdirs();
        

        if (file.isDirectory())

        {
         //metodos que fazem pesquisa de imagens omitidos

        }

path returned by the above methods on Android 2.3:

/mnt/sdcard/image

path returned by the above methods on Android 4.4 it lists as "Created" but does not create:

/Storage/Emulated/0/image

I looked at the documentation and got no results, changed the way of access between versions?

The AndroidManifest.xml you are already allowed to read and write to sdcard.

2 answers

5


The latest smartphones may have more than one External Storage. Beyond the sdcard have an intern. The path returned by Environment.getExternalStorageDirectory() refers to the primary external storage device which, in these cases, is the internal.

From version 19 (Android 4.4) to class Context provides the method getExternalFilesDirs() that returns a File[] with the paths for all application-specific directories of all external storage devices.

The first way back on array is the same as the returned by getExternal().

To obtain the paths to store application data, as referred to in documentation, the above methods shall be used and Environment.getExternalStorageDirectory().

-1

Add to Androidmanifest.xml

Then go Android device in: Settings -> Apps -> Advanced -> Permission Management-> Files and Media -> Your App -> Allow management of all files

Enjoy and Test your code Again!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.