Image Picker in Android Kotlin.

Harshita Bambure
2 min readSep 25, 2021

Sometimes we need to pick the image from the gallery so today we will learn about the Image pickers in android kotlin.

I am using view binding in this project so we need to add build Features in build.gradle(:app).

android {
buildFeatures {
viewBinding true
}
}

For the Image Picker, we need to add the permission in the manifest file. I used external storage permission.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Now we will create the XML design for the image picker.

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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">

<!--ImageView where image will be set-->
<ImageView
android:id="@+id/image_view"
android:scaleType="centerCrop"
android:src="@drawable/ic_baseline_image_24"
android:layout_width="400dp"
android:layout_height="400dp" />
<!--Button to pick image-->
<Button
android:id="@+id/img_pick_btn"
android:text="Choose Image"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Now its time to code for image picker.

package com.example.imagepicker

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.imagepicker.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)

binding.imgPickBtn.setOnClickListener {
//check runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED){
//permission denied
val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
//show popup to request runtime permission
requestPermissions(permissions, PERMISSION_CODE);
}
else{
//permission already granted
pickImageFromGallery()
}
}
else{
//system OS is < Marshmallow
pickImageFromGallery()
}
}
}
private fun pickImageFromGallery() {
//Intent to pick image
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_PICK_CODE)
}

companion object {
//image pick code
private val IMAGE_PICK_CODE = 1000
//Permission code
private val PERMISSION_CODE = 1001
}

//handle requested permission result
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when(requestCode){
PERMISSION_CODE -> {
if (grantResults.size >0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
//permission from popup granted
pickImageFromGallery()
}
else{
//permission from popup denied
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
}

//handle result of picked image
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE){
binding.imageView.setImageURI(data?.data)
}
}
}
Image Picker.

The source code https://github.com/HarshitaBambure/ImagePicker

Thats it.

--

--

Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .