val projection = arrayOf( MediaStore.Files.FileColumns.DISPLAY_NAME )
val selection = MediaStore.Files.FileColumns.RELATIVE_PATH + " = '/'"
val mediaStoreVolumes = MediaStore.getExternalVolumeNames(requireActivity())
mediaStoreVolumes.forEach { volume ->
val uri = MediaStore.Files.getContentUri(volume)
val cursor = cr.query(uri, projection, selection, null, null)
if (cursor != null) {
while (cursor.moveToNext()) {
Log.e("Volume: "+volume, "File: "+ cursor.getString(0))
}
cursor.close()
}
}
The above code print file names in 'external_primary' and SDCARD. But for USB OTG it fails with exception:
E FATAL EXCEPTION: main
Process: com.starsolutions.starfilemanager, PID: 10093
java.lang.IllegalArgumentException: Volume a80d-7c6d not found
But the volume name is returned by MediaStore by MediaStore.getExternalVolumeNames()
Is it possible to list files in a USB OTG drive using MediaStore API?
This is a bug of Android 11. It fixed in android 12. https://issuetracker.google.com/u/0/issues/246857526