I’ve been working with the Access Storage Framework introduced with Android KitKat, a feature that I’ve been waiting for a long time.
Everything seemed to be alright, quite easy to implement, until I faced a strange issue.
Once the file has been selected by the user, I wanted to store the file’s URI and re-open that URI the next time application is started. To do this, I’ve followed what the official documentation says:
When your app opens a file for reading or writing, the system gives your app a URI permission grant for that file. It lasts until the user’s device restarts. But suppose your app is an image-editing app, and you want users to be able to access the last 5 images they edited, directly from your app. If the user’s device has restarted, you’d have to send the user back to the system picker to find the files, which is obviously not ideal.
To prevent this from happening, you can persist the permissions the system gives your app. Effectively, your app “takes” the persistable URI permission grant that the system is offering. This gives the user continued access to the files through your app, even if the device has been restarted:
It didn’t work. Doh!
I was getting a
java.lang.SecurityException while trying to open the URI using
android.permission.MANAGE_DOCUMENTS to AndroidManifest.xml didn’t helped, and the many solutions found on stackoverflow.com didn’t work.
After taking a deeper look at the APIs and the source code I’ve found a working way to grant URI permissions:
place this call before
getContentResolver().takePersistableUriPermission(uri, takeFlags) and read permissions will be granted! Yeah!
The resulting code will be: