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


    Mobile Developer Android (m/w/d) Firebase

    REWE digital
    Köln / Cologne
    • Full Time
    apply now

    Senior Android Developer

    Komoot
    remote
    • Full Time
    apply now

    Android Engineering Manager

    Peloton
    New York, USA
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
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
Today, We will explore the New notification 🔔 runtime permission that was added in…
READ MORE
blog
Android 13 (API 33) introduces a new tool called photo picker 🖼. Today, we…
READ MORE
blog
How to use the new feature from Android 13 to display your app in…
READ MORE
blog
In the recently published Android 13 Developer Preview blog post, Dave Burke noted two…
READ MORE

Leave a Reply

Your email address will not be published.

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

Menu