I have implemented the Android photo picker library as described in the documentation, but on Android devices running Android 11 and 12 (API 30 and 31), the library automatically invokes the ACTION_OPEN_DOCUMENT intent action rather than the gallery.
The code:
private lateinit var pickMultipleMediaLauncher: ActivityResultLauncher<PickVisualMediaRequest>
Registers the photo picker activity launcher in multi-select mode.
private fun registerPhotoPicker() {
pickMultipleMediaLauncher =
registerForActivityResult(PickMultipleVisualMedia(imageSelectionLimitCount)) { uris ->
uris.forEach {
getRealPathFromMediaURI(it)?.let { imagePath -> imagesPath.add(imagePath) }
}
}
}
Launch the photo picker
pickMultipleMediaLauncher.launch(PickVisualMediaRequest.Builder().build())
Get the real path from media URI
fun getRealPathFromMediaURI(uri: Uri): String? {
val projection = arrayOf(MediaStore.MediaColumns.DATA)
val cursor = context.contentResolver.query(uri, projection, null, null, null)
val columnIndex = cursor?.getColumnIndex(MediaStore.MediaColumns.DATA)
return try {
if (cursor != null) {
cursor.moveToFirst()
val filePath = columnIndex?.let { cursor.getString(it) }
cursor.close()
filePath
} else null
} catch (exception: Exception) {
null
}
}
Following the documentation, from this section, you can find a small paragraph explaining when and why the Intent
ACTION_OPEN_DOCUMENT
is automatically called.Scrolling down the documentation to the section Device Availability will provide you a more detailed explanation and how to possibly solve your problem: