I'm using the Storage Access Framework to get write access on the sd-card (API >= 21).
It works fine on most devices, but some like the Galaxy S7 (Edge, API 23) throw an SecurityException when calling takePersistableUriPermission().
Caused by: java.lang.SecurityException: No persistable permission grants found for UID 10150 and Uri 0 @ content://com.android.externalstorage.documents/tree/70CD-6B92
code:
//call the SAF
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, Config.REQUEST_SAF);
//Persist permissions
int flags = resultData.getFlags();
flags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
this.getContentResolver().takePersistableUriPermission(treeUri, flags); //throws exception
The exception doesn't occur on all S7 devices. I've tested the code successfully on 2 devices but this one throws the SecurityException.
May i have to add some flags to the call of the SAF?
intent.addFlags(
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Any help is appreciated.