Android 'has no access to content://...' when using Intent.ACTION_GET_CONTENT

248 Views Asked by At

This is a question about permission of Android file system. If there could be more base/extended knowledge, I would be very grateful.

  • When using default app(file selector/default file explorer/gallery), the program runs OK.
  • When using Quickpic(a gallery software) to choose image, the program still runs normally.
  • But when using ES File Explorer, code crushes with SecurityException

enter image description here

The log showes both above return uri selectFile: content://media/external/images/media/212736, why the latter failed. How/Can I write a program which can support all third part file selector app?(without asking for file permission. You know, using default file explorer to pick file doesn't need permission of READING)

Here's some my hello world code

mPickResultLauncher = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    if (result.resultCode == RESULT_OK) {
        val data = result.data
        val uri = data?.data
        Log.d(TAG, "selectFile: $uri")

        // open file
        contentResolver.openInputStream(uri!!)?.use {
            val bytes = it.readBytes()
            Log.d(TAG, "selectFile: ${bytes.size}")
        }
    }
}
private fun selectFile() {
    val intent = Intent(Intent.ACTION_GET_CONTENT)
    mPickResultLauncher.launch(intent)
}

My test phone is Redmi K30, MIUI 14(Android 12).

0

There are 0 best solutions below