Android 13 Photo Picker gives FileNotFoundException

1.2k Views Asked by At

I used a photo picker to retrieve photos and I follow https://developer.android.com/training/data-storage/shared/photopicker. We have seen a few devices below crash in Android 11 and above devices.

Non-fatal Exception: java.io.FileNotFoundException: No such file or directory at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:156)

// Fragment  to get a photo uri.

val launcher = rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->

    passToAdapter(uri)
}

Button(
    choosePhoto = {
        //On button press, launch the photo picker
        launcher.launch(PickVisualMediaRequest(
            //Here we request only photos. Change this to .ImageAndVideo if you want videos too.
            //Or use .VideoOnly if you only want videos.
            mediaType = ActivityResultContracts.PickVisualMedia.ImageOnly
        ))
    }
)

   
Adapter:

// get application context .

val inputStream = context.contentResolver.openInputStream(uri) // crash at here.

Please note: I access this uri in different fragments/activities so if I use application class, I believe this should solve my problem.

I suspect the issue is because of the missing below lines

val flag = Intent.FLAG_GRANT_READ_URI_PERMISSION context.contentResolver.takePersistableUriPermission(uri, flag)

0

There are 0 best solutions below