Does not have permission to access picker uri in Android Jetpack Compose

258 Views Asked by At

I made an Android app using Jetpack Compose. As can be seen from the photos of the application, I take the information of the countries using retrofit and save it to Mongo. When the user goes to the Country details screen, he can select a photo from his gallery and register for this country. When I install the application on the device for the first time, I can save photos, but when I restart it, it crashes. I store the images in Firebase with URI, but if the upload to Firebase is interrupted, I save this session in dao and check it again later. I don't get any errors when I don't call this check.

enter image description here enter image description here enter image description here

FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: com.omersungur.theworldwander, PID: 13346 java.lang.SecurityException: Calling uid ( 10410 ) does not have permission to access picker uri: content://media/picker/0/com.android.providers.media.photopicker/media/1000005801 at android.os.Parcel.createExceptionOrNull(Parcel.java:3069) at android.os.Parcel.createException(Parcel.java:3053) at android.os.Parcel.readException(Parcel.java:3036) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190) at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:153) at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:814) at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:2043) at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1858) at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1689) at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:1635) at com.google.firebase.storage.UploadTask.(UploadTask.java:140) at com.google.firebase.storage.StorageReference.putFile(StorageReference.java:284) at com.omersungur.theworldwander.core.ExtFunctionKt.retryUploadingImageToFirebase(ExtFunction.kt:61) at com.omersungur.theworldwander.presentation.MainActivityKt$cleanupCheck$1.invokeSuspend(MainActivity.kt:108) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108) at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684) Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@b372588, Dispatchers.IO]

But I don't think so about image uri.

Image picker in Galler Uploader composable:

 val context = LocalContext.current
    val multiplePhotoPicker = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.PickMultipleVisualMedia(maxItems = 8),
    ) { images ->
        images.forEach {
            onImageSelect(it)
            val name = context.packageName
            context.grantUriPermission(name, it, Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }
    }

I have the images that cannot be loaded or deleted checked-in main activity:

private fun cleanupCheck(
    scope: CoroutineScope,
    imageToUploadDao: ImageToUploadDao,
    imageToDeleteDao: ImageToDeleteDao
) {
    scope.launch(Dispatchers.IO) {
        val result = imageToUploadDao.getAllImages()
        result.forEach { imageToUpload ->
            retryUploadingImageToFirebase(
                imageToUpload = imageToUpload,
                onSuccess = {
                    scope.launch(Dispatchers.IO) {
                        imageToUploadDao.cleanupImage(imageToUpload.id)
                    }
                }
            )
        }
        val result2 = imageToDeleteDao.getAllImages()
        result2.forEach { imageToDelete ->
            retryDeletingImageFromFirebase(
                imageToDelete = imageToDelete,
                onSuccess = {
                    scope.launch(Dispatchers.IO) {
                        imageToDeleteDao.cleanupImage(imageId = imageToDelete.id)
                    }
                }
            )
        }
    }
}

and I call the above function in onCreate:

cleanupCheck(scope = lifecycleScope, imageToUploadDao, imageToDeleteDao)

Extension functions:

fun retryUploadingImageToFirebase(
    imageToUpload: ImageToUpload,
    onSuccess: () -> Unit
) {
    val storage = FirebaseStorage.getInstance().reference
    storage.child(imageToUpload.remoteImagePath).putFile(
        imageToUpload.imageUrl.toUri(),
        storageMetadata { },
        imageToUpload.sessionUri.toUri()
    ).addOnSuccessListener { onSuccess() }
}

fun retryDeletingImageFromFirebase(
    imageToDelete: ImageToDelete,
    onSuccess: () -> Unit
) {
    val storage = FirebaseStorage.getInstance().reference
    storage.child(imageToDelete.remoteImagePath).delete()
        .addOnSuccessListener { onSuccess() }
}

If I delete cleanUpCheck call, I don't get any errors.

and this is the link of the project: https://github.com/omersungur/TheWorldWander

0

There are 0 best solutions below