Blog Infos
Author
Published
Topics
,
Published

Google recently announced the first Developer Preview of Android 13, while there are a lot of new APIs, the Photo picker excited me a lot both as a user and a developer.

If you want to try the Android 13 APIs, you can follow the instructions mentioned in the documentation. Although if you follow the documentation word by word you will not be able to compile the project, the documentation recommends adding the following code snippet in your to set up the SDK.

android {
compileSdkVersion "Tiramisu"
defaultConfig {
targetSdkVersion "Tiramisu"
}
}
view raw build.gradle hosted with ❤ by GitHub

But at the time of writing this article, you will get the following error:

Unsupported value: Tiramisu. Format must be one of:
- android-31
- android-31-ext2
- android-T
- vendorName:addonName:31

The fix is to use and instead like it is done in the code snippet below:

android {
compileSdkPreview "Tiramisu"
defaultConfig {
targetSdkPreview "Tiramisu"
}
}
view raw build.gradle hosted with ❤ by GitHub

Now you should be able to work with the Android 13 SDK 😃

What’s a Photo picker?

Photo picker provides you a new way to select media files stored in your device and cloud using an .

You may be thinking, isn’t there already an to open the Gallery? Yes, the Photo picker is similar to that in terms of functionality and it comes with a new refreshed UI.

Let’s have a look at the existing way of opening a Gallery through an

val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, REQUEST_CODE_SINGLE_SELECT)
view raw MainActivity.kt hosted with ❤ by GitHub

The following GIF shows how this would open up your Gallery

 

 

Now let’s see how to use the Photo picker in Android 13

val intent = Intent(MediaStore.ACTION_PICK_IMAGES)
startActivityForResult(intent, REQUEST_CODE_SINGLE_SELECT)
view raw MainActivity.kt hosted with ❤ by GitHub

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, ,

From Scoped Storage to Photo Picker: Everything to know about Storage

Persistence is a core element of every mobile app. Android provides different APIs to access or expose files with different tradeoffs.
Watch Video

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocate
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer ad ...
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocat ...
Google

Jobs

This code snippet would open up a dialog from the bottom and allow the user to select an image from the Gallery.

 

 

If you want to select multiple images you can provide the maximum number of images using extra

val mediaSelectionLimit = 2
val intent = Intent(MediaStore.ACTION_PICK_IMAGES)
intent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, mediaSelectionLimit)
startActivityForResult(intent, REQUEST_CODE_SINGLE_SELECT)
view raw MainActivity.kt hosted with ❤ by GitHub

The maximum value that we are using in the above code snippet can be any positive integer greater than 1 and less than or equal to the value returned from

Now the dialog would open up in full screen and you also have the option to view the selected images in a preview.

You can also provide the media type, in case you want the user to select only videos or images

val intent = Intent(MediaStore.ACTION_PICK_IMAGES)
intent.type = "video/*" // or "image/*"
startActivityForResult(intent, REQUEST_CODE_SINGLE_SELECT)
view raw MainActivity.kt hosted with ❤ by GitHub

At the time of writing this article, if you use the above , all your videos would be listed under the photos tab as you can see in the screenshot below (Hopefully this will get fixed soon)

While using these to select media files, you can retrieve the of the media file in , you will either get a single or a list of depending on the type of you have used.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
when (requestCode) {
REQUEST_CODE_SINGLE_SELECT -> {
val uri = data?.data
// Do stuff with uri
}
REQUEST_CODE_MULTI_SELECT -> {
var mediaCount = 0
while (mediaCount < data?.clipData!!.itemCount) {
val uri = data.clipData!!.getItemAt(mediaCount).uri
// Do stuff with uri
mediaCount++
}
}
}
}
}
view raw MainActivity.kt hosted with ❤ by GitHub

I like the new Photo picker and since it is a dialog that pops up in an app, it feels that you are still in the app while selecting an image/video. I feel its name is a little confusing since you can use the Photo picker to select a video as well, I guess Media picker would be less confusing but naming is hard so let’s not get into it 😛 Let me know what to do you think about the Photo picker.

If you have feedback, feel free to tweet or message me on Twitter.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
While Targeting Android 13 , OnbackPressed Override Function is deprecated😢. Usually, we used to…
READ MORE
blog
Today, We will explore the New notification 🔔 runtime permission that was added in…
READ MORE
blog
In this article, we will explore Android 13’s new clipboard UI, as well as…
READ MORE
blog
App launcher icons, the very first interaction that someone has with your app is…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu