Android

Easy way to Pick image from gallery in android kotlin

Today we are going to learn a very simple and easy method to pick image from gallery in android in Kotlin language.

For picking image from gallery we will use PhotoPicker.

Pick image from gallery using PhotoPicker

The photo picker is like a tool that helps user to find and choose photos and videos. It puts pictures and videos in order from newest to oldest, and  can easily search for users. This tool also makes sure that when user use it, user only give permission for the app to see certain pictures and videos user pick, not all of them. This helps keep user’s privacy safe.

Let’s start it.

Step 1: Create design in activity_main.xml

  • Open layout file of MainActivity and add a button, on click on this button we will open the photo picker interface to select images.
  • Add an ImageView in layout file, we will display the image in it, which will be picked by user from gallery.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button_pick_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Picke Image" />

</LinearLayout>

Step 2 : Register photo picker activity launcher

As we know that onActivityResult is deprecated in android, so we will register a photo picker activity launcher in onCreate method.

val pickMedia =
    registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
        if (uri != null) {
            imageView.setImageURI(uri)
        } else {
            Log.d("PhotoPicker", "No media selected")
        }
    }

The registerForActivityResult function is used to set up an activity result listener for picking visual media. Visual media here refers to pictures or videos.

(ActivityResultContracts.PickVisualMedia()). This part tells the app that we want to pick a picture or video using a built-in activity that the system provides.

Step 3: Launch the photo picker

Now we will open the photo picker interface, so that we can pick image from there. For this we will set click listener on button and on click on it will launch the photo picker interface.

  button.setOnClickListener {
            pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo))
        }

This code will open the gallery form where we can select any image. After selecting an image from gallery, gallery will close and the picked image will display in image view that we have added in our activity design.

imageView.setImageURI(uri)

This code will display the picked image in ImageView.

Select multiple images from gallery

Sometimes we face some scenarios where we have to select multiple images from gallery.  To select multiple images we will use PickMultipleVisualMedia contract and set the maximum number of selectable files like this.

val pickMultipleMedia =
    registerForActivityResult(ActivityResultContracts.PickMultipleVisualMedia(5)) { uris ->
        if (uris.isNotEmpty()) {
            Log.d("PhotoPicker", "Number of items selected: ${uris.size}")
        } else {
            Log.d("PhotoPicker", "No media selected")
        }
    }


pickMultipleMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo))

This code will return an array of URIs, because we have selected multiple images. Now we can display each selected image.

Leave a Reply

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