Handle the Kitkat new external storage policy (write access)

Saturday, May 3, 2014

I recently found a big problem on my users' phones running the latest Kitkat version and using an external storage such as an SD card.

My app is storing photos on the external storage, in a specific folder.

The problem is that Google completely changed the permissions and it is now just impossible to write into a secondary storage such as an external SD card:

The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled. (source)

Now Google is asking to use the following:

If your app reads from external storage... Your app can not read shared files on the external storage when running on Android 4.4, unless your app has the READ_EXTERNAL_STORAGE permission. That is, files within the directory returned by getExternalStoragePublicDirectory() are no longer accessible without the permission. However, if you need to access only your app-specific directories, provided by getExternalFilesDir(), then you do not need the READ_EXTERNAL_STORAGE permission. (source)

It seems the only know to be able to write on external SD card is by using: getExternalFilesDir() that is a private folder for the app.

The problem is that I have been using getExternalStorageDirectory() until now to access and write the SD card.

What is the best way to handle this change? The only solution I have in mind is to move all the files from the old folder to the new private one provided by getExternalFilesDir. But that's a heavy change and I am afraid of the consequences...

I think Google is trying to avoid the use of SD cards, but they forgot too quickly that lots of phones still have a limited default space and that the SD card is not just an option...