Access Storage Framework and the URI permissions nightmare

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:

finalinttakeFlags=intent.getFlags()&(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);// Check for the freshest data.getContentResolver().takePersistableUriPermission(uri,takeFlags);

It didn’t work. Doh!

I was getting a java.lang.SecurityException while trying to open the URI using getContentResolver().openInputStream(uri) or getContentResolver().openAssetFileDescriptor(uri, "r"):