My users can select files via ACTION_OPEN_DOCUMENT
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.OpenDocument(),
onResult = onResult
)
After which I use takePersistableUriPermission
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
This works great! Now the picker UI is not what I'd call optimal, so I want to also allow inverting the control flow by sharing the file with my app.
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
And in onCreate
or onNewIntent
in my Activity
, I want to also take the persistable URI permission.
override fun onNewIntent(intent: Intent?) {
// null checks etc
val uri = intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri ?: return
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
// ...
}
This however, results in a SecurityException:
java.lang.SecurityException: No persistable permission grants found for UID 10146 and Uri content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/61/REQUIRE_ORIGINAL/NONE/image/jpeg/702648108
Is there a way to achieve this?
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
That is not the way to do it. Please look at examples. First look in the offered flags if that flag is there. – Bacitracin