3

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

FiXiT
  • 769
  • 1
  • 12
  • 27
  • I found this and it solves the problem, add `mActivity.grantUriPermission(mActivity.getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)` before the `takePersistableUriPermission()` https://andreamaglie.com/software-development/access-storage-framework-and-the-uri-permissions-nightmare/ – neo Aug 13 '22 at 19:36

1 Answers1

3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am still getting this crash. I have removed data.flags from the code. @CommonsWare – FiXiT Apr 09 '21 at 07:36
  • I am able to reproduce the crash by selecting ~540 images at a time . I read there is a limit of 128| 512 images while taking Persistable Permission. – FiXiT Apr 09 '21 at 12:01
  • 2
    @FiXiT: Ah! Yes, if you are selecting that many images, that is very likely to be the source of your problem. That limit is unchangeable, at least for existing versions of Android, though there is always hope that they raise it for future versions of Android. Still, it is likely that there will always be some limit. Also note that AFAIK the limit is per *app*, not per `ACTION_OPEN_DOCUMENT` call -- two `ACTION_OPEN_DOCUMENT` calls with ~270 images at a time should give you similar results in the second batch, or the first on devices with a 128 `Uri` limit. – CommonsWare Apr 09 '21 at 12:21
  • 2
    @FiXiT: Basically, the vision behind `takePersistableUriPermission()` is for a small number of documents, not images for a gallery or something like that. And, since you are working with images, you might consider switching to querying the `MediaStore` and presenting your own gallery UI. – CommonsWare Apr 09 '21 at 12:23