Consider implementing an app, that lets users pick images taken with a camera from Gallery, and then use those pics on the app.
Project settings are
targetSdk=34, compileSdk=34, minSdk=22
Using MediaStore API, picker can be launched
startActivityForResult(Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ,1);
and then acquire a picture in onActivityResult()
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
super.onActivityResult(requestCode, resultCode, intent)
val uri = intent!!.data!!
val inputStream = contentResolver.openInputStream(uri)!!
val bitmapBytes = inputStream.readBytes()
val bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.size)
}
Question 1: What permissions are needed to produce Bitmap? Reading specs it seems that READ_EXTERNAL_STORAGE would be needed. But the code works without any permission in SDK 34-24, and starts only requiring READ_EXTERNAL_STORAGE on <= SDK 23.
Question 2: Why READ_MEDIA_IMAGES is not needed on >= SDK 33? As it states on another specs, implementation needs that permission to read images other app produced on SDK 33 onwards.
Question 3: What would be the advantage of using PhotoPicker API (instead of MediaStore)?
What permissions are needed to produce Bitmap?
If the Uri
has a content
scheme (and they all should on modern versions of Android), you should already have permission, courtesy of the user being involved in the selection via ACTION_PICK
.
starts only requiring READ_EXTERNAL_STORAGE on <= SDK 23
Your Uri
presumably has the file
scheme on those older devices.
Why READ_MEDIA_IMAGES is not needed on >= SDK 33?
See above.
What would be the advantage of using PhotoPicker API (instead of MediaStore)?
You are not using MediaStore
. You are using ACTION_PICK
. What handles ACTION_PICK
for a MediaStore
-supplied Uri
varies by OS version. On the most recent versions of Android, it might be the PhotoPicker — I have not used ACTION_PICK
seriously in several years.
The PhotoPicker API:
Supports multiple images, which ACTION_PICK
does not
Supports cloud providers (sometimes), which ACTION_PICK
may or may not