0

We are getting some issue in the prod as below and It only observed to a few devices in the prod. I believe we don't need to give any read write or read image permission to access any storage framework. Does anyone know what would be the issue ?

Android 12- Non-fatal Exception: java.lang.IllegalStateExcetion: Only owner is able to interact with pending/trashed item content://media/external_primary_images/Media Android 10/9 - Non-fatal exception: java.io.FileNotFoundException: open failed: ENOENT

@Composable
fun HomeScreenNavigationEntry() {

    val context = LocalContext.current
    val filePickerLauncher = rememberFilePickerLauncher(context) { intent ->
        Toast.makeText(context, "File received!!", Toast.LENGTH_SHORT).show()
    }

    HomeScreen(
        fileLauncher = {
             val intent = filePickerIntent()
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            filePickerLauncher.launch( filePickerIntent())
        }
    )

}

@Composable
fun rememberFilePickerLauncher(context: Context, 
    onRememberFilePickerLauncherResult: (intent: Intent?) -> Unit,
): ManagedActivityResultLauncher<Intent, ActivityResult> {
    return rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()){ activityResult ->
        if (activityResult.resultCode == Activity.RESULT_OK) {
            onRememberFilePickerLauncherResult(activityResult.data, context)
        } else {
            onRememberFilePickerLauncherResult(null)
        }
    }
}

private fun filePickerIntent(): Intent {
    val fileType = "application/pdf"
    val mimeTypes = arrayOf(fileType)
    return Intent(Intent.ACTION_OPEN_DOCUMENT)
        .addCategory(Intent.CATEGORY_OPENABLE)
        .setType("*/*")
        .putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
        .putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
}

fun onRememberFilePickerLauncherResult(intent: Intent?, context: Context) {
        val uri = intent?.data ?: return
        val flag = Intent.FLAG_GRANT_READ_URI_PERMISSION
        val contentResolver = context.contentResolver
        contentResolver.takePersistableUriPermission(uri, flag)
        processUri(uri)
}

trying to access below line and getting crash:(Fatal Exception: java/lang.SecurityException: PErmission Denial: Opening provider com.android.providers.downloads.DownloadStorageProvider from processRecord(URI) requires that you obtain access using ACTION_OPEN_DOCUMENT or relate)


fun processUri(uri: Uri) {
Cursor cursor = contenrResolver.query(uri, null, null, null)
}

All code is written in a single activity.

9
  • There is no code that would produce those exceptions. Commented Aug 10, 2023 at 19:25
  • You only should go for persistable uri permission if you want to use the uri in a different activity or later. Commented Aug 10, 2023 at 19:26
  • Thanks, I was waiting for our prod release and we are still getting simillar kind of exception Fatal Exception: java/lang.SecurityException: PErmission Denial: Opening provider com.android.providers.downloads.DownloadStorageProvider from processRecord(URI) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs Commented Sep 13, 2023 at 18:47
  • You still did not post code that could produce such an exception. Commented Sep 13, 2023 at 19:02
  • If you use startActivityForResult then there should be an onActivityResult. Commented Sep 13, 2023 at 19:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.