How to Pick Image from Gallery in Android Studio using Kotlin?

In this tutorial, I am going to teach you “How to Pick Image from Gallery in Android Studio using Kotlin“. Okay, let’s move on to the article. I am going to use “registerForActivityResult” because “onActivityResult” is deprecated.

Pick Image from Gallery in Android Studio using Kotlin

Pick Image from Gallery in Android Studio using Kotlin

activity_main.xml

I added a simple button to pick image from gallery and imageview to show the selected image.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/selectImages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Images"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.361" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="@+id/selectImages"
        app:layout_constraintTop_toBottomOf="@+id/selectImages"
        app:layout_constraintEnd_toEndOf="@+id/selectImages"
        android:layout_marginTop="50dp"
        tools:srcCompat="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.example.pickimagekotlin

import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val selectImages = findViewById<Button>(R.id.selectImages)
        val imageView = findViewById<ImageView>(R.id.imageView)

        val chooseImage = registerForActivityResult<String, Uri>(ActivityResultContracts.GetContent()) { result: Uri? ->
            imageView.setImageURI(result)
        }

        selectImages.setOnClickListener {
            chooseImage.launch("image/*")
        }
    }
}

Output

Final Words

I hope this article helps you to “Pick Image from Gallery in Android Studio using Kotlin“. If you have any doubts about this topic then please ask via the comments section. Share this article with other Android developers.

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment