Implement Android Photo Picker in Android Studio

Everyday Programmer
4 min readAug 15, 2023
Implement Android Photo Picker in Android Studio

Adding a photo picker to your Android app can greatly enhance the user experience, allowing users to easily select images from their device’s gallery or camera. In this tutorial, we will walk you through the process of implementing an Android photo picker in Android Studio using Java.

About Photo picker:

The photo picker provides a browsable, searchable interface that presents the user with their media library, sorted by date from newest to oldest. he photo picker provides a safe, built-in way for users to grant your app access to only selected images and videos, instead of their entire media library. The tool updates automatically, offering expanded functionality to your app’s users over time without requiring any code changes.

Implementation:

Step 1: Create a New Android Project

  1. Launch Android Studio and create a new Android project with an appropriate name and package.
  2. Choose an Empty Activity template or a template that suits your app’s needs.

Step 2: Add dependency:

Add the following dependency to build.gradle file:

implementation 'androidx.activity:activity:1.7.0'

Step 3: Design the Layout

Design your layout with appropriate UI elements like buttons and image views. For example:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:layout_width="300sp"
android:layout_height="300sp"
android:layout_centerHorizontal="true"
android:id="@+id/imageView"
android:layout_marginTop="20sp"/>

<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Image"
android:layout_centerHorizontal="true"
android:layout_below="@id/imageView"
android:id="@+id/pickImage"
android:layout_marginTop="20sp"/>

</RelativeLayout>

Step 4: Implement Photo Picker

Select a single media item:

To select a single media item, use the PickVisualMedia activity result contract, as shown in the following code snippet:

ActivityResultLauncher<PickVisualMediaRequest> pickMedia =
registerForActivityResult(new PickVisualMedia(), uri -> {
// Callback is invoked after the user selects a media item or closes the
// photo picker.
if (uri != null) {
Log.d("PhotoPicker", "Selected URI: " + uri);
} else {
Log.d("PhotoPicker", "No media selected");
}
});

// Include only one of the following calls to launch(), depending on the types
// of media that you want to let the user choose from.

// Launch the photo picker and let the user choose images and videos.
pickMedia.launch(new PickVisualMediaRequest.Builder()
.setMediaType(PickVisualMedia.ImageAndVideo.INSTANCE)
.build());

// Launch the photo picker and let the user choose only images.
pickMedia.launch(new PickVisualMediaRequest.Builder()
.setMediaType(PickVisualMedia.ImageOnly.INSTANCE)
.build());

// Launch the photo picker and let the user choose only videos.
pickMedia.launch(new PickVisualMediaRequest.Builder()
.setMediaType(PickVisualMedia.VideoOnly.INSTANCE)
.build());

// Launch the photo picker and let the user choose only images/videos of a
// specific MIME type, such as GIFs.
String mimeType = "image/gif";
pickMedia.launch(new PickVisualMediaRequest.Builder()
.setMediaType(new PickVisualMedia.SingleMimeType(mimeType))
.build());

Select multiple media items:

To select multiple media items, set a maximum number of selectable media files, as shown in the following code snippet.

// Registers a photo picker activity launcher in multi-select mode.
// In this example, the app lets the user select up to 5 media files.
ActivityResultLauncher<PickVisualMediaRequest> pickMultipleMedia =
registerForActivityResult(new PickMultipleVisualMedia(5), uris -> {
// Callback is invoked after the user selects media items or closes the
// photo picker.
if (!uris.isEmpty()) {
Log.d("PhotoPicker", "Number of items selected: " + uris.size());
} else {
Log.d("PhotoPicker", "No media selected");
}
});

// For this example, launch the photo picker and let the user choose images
// and videos. If you want the user to select a specific type of media file,
// use the overloaded versions of launch(), as shown in the section about how
// to select a single media item.
pickMultipleMedia.launch(new PickVisualMediaRequest.Builder()
.setMediaType(PickVisualMedia.ImageAndVideo.INSTANCE)
.build());

The platform limits the maximum number of files that you can ask the user to select in the photo picker. To access this limit, call getPickImagesMaxLimit(). On devices where the photo picker isn't supported, this limit is ignored.

Note: If the photo picker isn’t available and the support library invokes the ACTION_OPEN_DOCUMENT intent action, the system ignores the specified maximum number of selectable media files.

Example MainActivity.java:

public class MainActivity extends AppCompatActivity {
ImageView imageView;
ActivityResultLauncher<PickVisualMediaRequest> launcher = registerForActivityResult(new ActivityResultContracts.PickVisualMedia(), new ActivityResultCallback<Uri>() {
@Override
public void onActivityResult(Uri o) {
if (o == null) {
Toast.makeText(MainActivity.this, "No image Selected", Toast.LENGTH_SHORT).show();
} else {
Glide.with(getApplicationContext()).load(o).into(imageView);
}
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);
MaterialButton pickImage = findViewById(R.id.pickImage);

pickImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
launcher.launch(new PickVisualMediaRequest.Builder()
.setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE)
.build());
}
});
}
}

In the above example when the pickImage is clicked activity result launcher is launched with the PickVisalMediaRequest. In this example the media type is set to image only so the photo picker will display only the images. After the user selects an image if the result is not null then the image is displayed in an image view using Glide. This is just a simple example you can change the media type and handle the result however you need.

Conclusion:

Congratulations! You have successfully implemented Android photo picker in your app using Android Studio and Java. Users can now easily select images from their device’s gallery, enhancing the overall user experience of your app.

You can find the source code on Github from here: https://github.com/Everyday-Programmer/Android-Image-Picker

Watch the YouTube tutorial here: https://youtu.be/pUrPw0Q8Inc

Watch YouTube tutorial on how to implement photo picker to pick videos from here: https://youtu.be/pwdJZ7lWGOs

Video Picker Source Code: https://github.com/Everyday-Programmer/Android-Video-Picker

Happy Coding :)

--

--

Everyday Programmer

Join me on my coding journey as I explore the realms of algorithms, data structures, and software magic. Let's conquer the code together! 🚀