androidkotlinandroid-jetpack-compose

Does not have permission to access picker uri


I open the image in the repository via Uri.

var imageUri by remember { mutableStateOf<Uri?>(null) }

val launcher = rememberLauncherForActivityResult(
    contract =
    ActivityResultContracts.GetContent()
) { uri: Uri? ->
    imageUri = uri
}

launcher.launch("image/*")

After I transfer the picture via shared prefs, I switch to another screen, there is a picture on this screen, everything is OK. But, if I restart the application, I get an error.

enter image description here

 DatabaseUtils           com...ndroid.providers.media.module  E  Writing exception to parcel
                                                                                                    java.lang.SecurityException: Calling uid ( 10192 ) does not have permission to access picker uri: content://media/picker/0/com.android.providers.media.photopicker/media/1000000037
                                                                                                        at com.android.providers.media.PickerUriResolver.checkUriPermission(PickerUriResolver.java:294)
                                                                                                        at com.android.providers.media.PickerUriResolver.openTypedAssetFile(PickerUriResolver.java:118)
                                                                                                        at com.android.providers.media.MediaProvider.openTypedAssetFileCommon(MediaProvider.java:8368)
                                                                                                        at com.android.providers.media.MediaProvider.openTypedAssetFile(MediaProvider.java:8325)
                                                                                                        at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:667)
                                                                                                        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:341)
                                                                                                        at android.os.Binder.execTransactInternal(Binder.java:1339)
                                                                                                        at android.os.Binder.execTransact(Binder.java:1275)

I open the picture on another screen in this way:

AsyncImage(
    model = imageUri,
    contentDescription = null,
    modifier = Modifier)

Therefore, I conclude that if I need to get a picture in this way, I need to open the storage every time. This decision is extremely stupid, so I ask for advice :)


Solution

  • But, if I restart the application, I get an error.

    Yes. Why dont you mention the error?

    After restart the permission to read the uri has gone.

    To prevent this takePersistableUriPermission() in onActivityResult.;