Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving data to /externalsdcard/Android/data/<app package name> in KITKAT

I read alot about KITKAT and the prevention of writing to the secondary external sdcard. It seems to be a new policy from google. I also read about storing the app's data under /externalsdcard/Android/data/.

Now my question, because I can't reproduce or test: If the app is not allowed to write data directly under < secundary external sdcard >, Will it then be possible to store the app's data under < secundary external sdcard >/Android/data/< app package name > ?

If possible. Could someone share a codesample ?

like image 348
mcfly soft Avatar asked Mar 14 '14 19:03

mcfly soft


1 Answers

  • Never assume the removable secondary storage is larger than primar internal. On most modern devices internal storage can be up to 64 GB, but SD Card can be as small as 1 MB large (if user inserted small old card).

  • There is no way to distinguish which one is "external" using current APIs.

So the right way is using Context.getExternalFilesDirs() which returns all external storages to which you can store application-specific files without any permissions.

Use StatFs to find the largest storage available, and write data into it.

final File[] dirs = context.getExternalFilesDirs(null); //null means default type
//find a dir that has most of the space and save using StatFs

As the documentation says,

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:

External files are not always available: they will disappear if the user mounts the external storage on a computer or removes it. There is no security enforced with these files.

External storage devices returned here are considered a permanent part of the device, including both emulated external storage and physical media slots, such as SD cards in a battery compartment. The returned paths do not include transient devices, such as USB flash drives.

An application may store data on any or all of the returned devices. For example, an app may choose to store large files on the device with the most available space, as measured by StatFs.

No permissions are required to read or write to the returned paths; they are always accessible to the calling app. Write access outside of these paths on secondary external storage devices is not available.

The first path returned is the same as getExternalFilesDir(String). Returned paths may be null if a storage device is unavailable.

like image 84
Yaroslav Mytkalyk Avatar answered Oct 18 '22 00:10

Yaroslav Mytkalyk