Open In App

How to Use Photo Picker in Android 13?

Improve
Improve
Like Article
Like
Save
Share
Report

With Android 13, Google unveiled a new specialized media picker. This new option provides a more private and user-friendly alternative for those times when you want to share photographs, as opposed to potentially granting apps access to all of your files via the well-known document picker. You don’t actually need Android 13 on your phone to use this media picker because Google was able to integrate it into the Google Play Services. In fact, Google just announced that it had backported the functionality all the way back to Android 4.4 KitKat, which was initially introduced in 2013. The photo picker offers a browse-and-search interface that shows the user their media library, arranged by date (newest to oldest) and searchable. The photo picker offers a secure, built-in solution for users to provide your app access to just the selected photos and videos instead of their whole media collection

This implies that developers may incorporate the media picker into their programs considerably more quickly than they would typically. The media picker is a standard component of any phone that uses Play Services, so developers won’t have to wait for the majority of Android phones to be updated to a version that supports this functionality. Without requiring any code modifications, the tool updates automatically, providing users of your app with more capabilities over time. Devices that fit the following description can use the photo picker:

  1. Run Android 11 (API level 30) or a later version.
  2. Google System Updates provides updates for Modular System Components.

How to Implement the new Android 13 Image Picker in the Android Application?

Even if it sounds hard in the first place to get the cool new image picker quickly in Android Deployment, it’s actually very easy, we will be using Jetpack to get our work done, just follow the below steps in order to get the thing up and running in no time! But, before jumping in, there are two types of intent we can use to choose our images, they are:

  • PickVisualMedia to pick a single still or moving image.
  • PickMultipleVisualMedia to choose a number of pictures or movies.

Step #1: If you need to upload only single files

If you wish to allow the user to only select a single file, then you can use the code below to achieve the same. 

Java




ActivityResultLauncher<PickVisualMediaRequest>
    gfgMediaPicker = registerForActivityResult(
        new PickVisualMedia(), uri -> {
            if (uri != null) {
                Log.d("PhotoPicker",
                      "Selected URI: " + uri);
            }
            else {
                Log.d("Opened the picker",
                      "Select something geek");
            }
        });
    gfgMediaPicker.launch(
        new PickVisualMediaRequest.Builder()
        .setMediaType(
            PickVisualMedia.ImageAndVideo.INSTANCE)
        .build());
 
    gfgMediaPicker.launch(
        new PickVisualMediaRequest.Builder()
        .setMediaType(PickVisualMedia.ImageOnly.INSTANCE)
        .build());
    .gfgMediaPicker.launch(
        new PickVisualMediaRequest.Builder()
        .setMediaType(PickVisualMedia.VideoOnly.INSTANCE)
        .build());
 
    String mimeType = "image/gif";
    gfgMediaPicker.launch(
        new PickVisualMediaRequest.Builder()
        .setMediaType(
            new PickVisualMedia.SingleMimeType(mimeType))
        .build());


Step #2: If you need to upload multiple images

Sometimes, you may require the user to upload multiple files, if like for example, if you have a social media application, where the user can post multiple images, the process to select multiple images are as follows:

Java




ActivityResultLauncher<PickVisualMediaRequest>
    pickMultipleMedia = registerForActivityResult(
        new PickMultipleVisualMedia(5), gfgPhotoPicker -> {
            if (!gfgPhotoPicker.isEmpty()) {
                Log.d("PhotoPicker",
                      "Number of items selected: "
                          + gfgPhotoPicker.size());
            }
            else {
                Log.d("Opened Picker",
                      "Choose something Geek");
            }
        });
    pickMultipleMedia.launch(
        new PickVisualMediaRequest.Builder()
        .setMediaType(
            PickVisualMedia.ImageAndVideo.INSTANCE)
        .build());


What if the app needs background access to photos?

If your app is a photo uploading application, or perhaps a photo back-up application, then you might need to require continuous access to the photos, even after the device has been rebooted. To do this, we will simply take the takePersistableUriPermission.

Java




int gfgFlagger = Intent.FLAG_GRANT_READ_URI_PERMISSION;
context.contentResolver.takePersistableUriPermission(uri, flag);


Conclusion

That’s it, you have completed adding the newest photo-picker to your Android Application, and can now leverage it to allow your users an even smoother experience. The new pop-up is user-friendly and is also baked from keeping privacy in mind from the start, hope you use this to great use.



Last Updated : 06 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads