FiXiT
FiXiT

Reputation: 819

No persistable permission grants found for UID 10208 [ Security Exception ]

I am using Storage Access Framework for Image Picker in my app. Below is the code

val types = arrayOf("image/png", "image/jpeg", "image/jpg")
val intent = Intents.createDocumentIntent(types, true)
if (canDeviceHandle(intent)) caller.startActivityForResult(intent, OPEN_GALLERY)

Here is the intent for creating document

 fun createDocumentIntent(types: Array<String>, allowedMultiple: Boolean): Intent {
        return Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
            addCategory(Intent.CATEGORY_OPENABLE)
            type = if (!types.isNullOrEmpty()) {
                putExtra(Intent.EXTRA_MIME_TYPES, types)
                types[0]
            } else "*/*"
            putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowedMultiple)
            addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
            addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        }
    }

This is in OnActivityResult

    private fun handleGalleryActivityResult(data: Intent?, callbacks: FilePicker.Callbacks) {
        if (data == null) return

        val files = mutableListOf<Uri>()
        when {
            data.clipData != null -> {
                val clipData = data.clipData ?: return
                (0 until clipData.itemCount).forEach { files.add(clipData.getItemAt(it).uri) }
            }
            data.data != null -> {
                files.add(data.data!!)
            }
            else -> return
        }

        files.forEach {
            val flags = data.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION
            activity.contentResolver.takePersistableUriPermission(it, flags)
        }

        callbacks.onFilesPicked(files)
    }

I am getting crash in line

 activity.contentResolver.takePersistableUriPermission(it, flags)

in onActivityResult.

I read many solutions regarding this crash like adding persistable (FLAG_GRANT_PERSISTABLE_URI_PERMISSION) flag or adding takePersistableUriPermission but I have already have this but still I am getting this crash . I couldn't find any solution till now and my app users are facing this issue also on my phone I am not able to reproduce it myself.

Also on side note: I am using target version -> 11

Upvotes: 5

Views: 3724

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007474

Replace:

val flags = data.flags and Intent.FLAG_GRANT_READ_URI_PERMISSION

with:

val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION

The only values that you pass to takePersistableUriPermission() are FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION, and you have no idea what data.flags has in it.

Upvotes: 3

Related Questions