Saturday, 7 May 2022

Passing uri between compose screens causes: SecurityException: Permission Denial

I receive a uri in screen "A" by:

val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
        if(activityResult.resultCode == Activity.RESULT_OK) {
            val uri = activityResult.data?.data!!
            context.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
            viewModel.onUriReceived(uri)
        }
    }
LaunchedEffect(launcher) {
     val intent = Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Video.Media.EXTERNAL_CONTENT_URI).apply {
         addCategory(Intent.CATEGORY_OPENABLE)
         type = "video/*"
         addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
         addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
     }
     coroutineScope.launch {
         launcher.launch(intent)
     }
  }

I can open the uri in screen "A" but if I pass the uri to screen "B" I receive:

 java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/video:38 from pid=6074, uid=10146 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:153)
        at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:780)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:2027)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1842)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:1518)


from Passing uri between compose screens causes: SecurityException: Permission Denial

No comments:

Post a Comment