Pick an Image from the Gallery – Android Studio - Kotlin

How to Pick an Image from the Gallery?

In this tutorial, we will walk through the complete process of selecting an image from the device’s Gallery in an Android application. This feature is commonly used in many modern apps—for uploading profile pictures, choosing photos for posts, or attaching images to forms.

We will start by creating a simple layout with a button that the user can tap to open the Gallery. Once the button is clicked, the system’s built-in image picker will appear, allowing the user to browse their photo library and select a single image. After the user chooses an image, we will retrieve its URI and display it inside an ImageView within our app.

>> Check for Java
>> Check for Kotlin
>> Check for Compose

Code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="50dp"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageIv"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_image_black" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/pickImageBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pick Image Gallery" />

</LinearLayout>

MainActivity.kt

package com.technifysoft.myapplication

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.technifysoft.myapplication.databinding.ActivityMainBinding

class MainActivityKt : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val TAG = "MAIN_TAG"

    private var imageUri: Uri? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.pickImageBtn.setOnClickListener {
            pickImageGallery()
        }
    }

    private fun pickImageGallery() {
        Log.d(TAG, "pickImageGallery: ")
        //Intent to launch Image Picker e.g. Gallery
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.setType("image/*")
        galleryActivityResultLauncher.launch(intent)
    }

    private val galleryActivityResultLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        //Check if image is picked or not
        if (result.resultCode == RESULT_OK) {
            //Image Picked from Gallery, get data/intent from ActivityResult
            val data = result.data
            //get uri of image picked
            imageUri = data!!.data
            Log.d(TAG, "onActivityResult: Image Picked From Gallery: $imageUri")
            try {
                //set to profileIv
                binding.imageIv.setImageURI(imageUri)
            } catch (e: java.lang.Exception) {
                Log.e(TAG, "onActivityResult: ", e)
            }
        } else {
            //Cancelled
            Log.d(TAG, "onActivityResult: Cancelled...")
            Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show()
        }
    }

}

Screenshot:

Pick an Image from the Gallery – Android Studio - Kotlin

Comments

Popular posts from this blog

Picture In Picture - Android Studio - Kotlin

Manage External Storage Permission - Android Studio - Kotlin

How to add AIDL folder | Android Studio