I've had success using
getContentResolver().takePersistableUriPermission(myUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
to allow for access to a document across reboots (previously obtained using ACTION_OPEN_DOCUMENT).
But I wonder what happens if it fails as there is no return value and there is no documented exceptions (checked or otherwise) as shown here: docs.
Yet when searching on SO I see there is in fact a SecurityException possible shown here:
exception. And it would stand to reason there are errors possible, for example an invalid URI provided - or document no longer exists, etc.
So the question is how to properly handle undocumented RuntimeExceptions which evidently may occur on this call. Are these the only valid approaches?
(A)
try {
getContentResolver().takePersistableUriPermission(myUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
} catch (SecurityException e) {
// handle failure to get persistable permission though NOT DOCUMENTED
}
(B)
try {
getContentResolver().takePersistableUriPermission(myUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
} catch (Exception e) {
// handle failure to get persistable permission
}
And more specifically is there any way to find out all possible RuntimeExceptions from this call ?
You could catch
Throwableinstead ofSecurityExceptionorException. But, in general,try/catchis how you would catch whatever is thrown.Not realistically. There are tens of thousands of Android device models, and any of them could contain modifications to Android that affect what gets thrown by the code paths behind
takePersistableUriPermission().