Choose Image From Gallery in android

Pandiyan Mani
4 min readMar 30, 2023

For that first we need to get READ_EXTERNAL_STORAGE permission from user so we add this below permission in AndroidManifest.xml file

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

Next we create two variables of type val one is checking permission and another one will be passed as an argument on startActivityForResult(intent, IMAGE_CHOOSE)

private val READ_PERMISSION_CODE = 1001
private val IMAGE_CHOOSE = 1000
private fun chooseImageGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_CHOOSE)
}
requestPermissions(permission, READ_PERMISSION_CODE)

When the user taps on the button first we need to request permission for accessing device storage so lets see how we can request permission on user taps

findViewById<Button>(R.id.button_choosefromgallery).setOnClickListener {
// Request permission
val permissionGranted = requestStoragePermission()
if (permissionGranted) {
// Open the Gallery
}
}
 private fun requestStoragePermission(): Boolean {
var permissionGranted = false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val storagePermissionNotGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_DENIED
if (storagePermissionNotGranted) {
val permission = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
requestPermissions(permission, READ_PERMISSION_CODE)
} else {
permissionGranted = true
}
} else {
permissionGranted = true
}
return permissionGranted
}

Then we need to Handle Allow or Deny response from the permission dialog

if (requestCode == READ_PERMISSION_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val storagePermissionGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
if (storagePermissionGranted) {
chooseImageGallery()
} else {
showAlert("Storage permission was denied.")
}
} else {
showAlert("Storage permission Not Needed.")
}
}

So finally when the user taps on the image from gallery we need to set it on the ImageView.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_CHOOSE) {
imageView?.setImageURI(data?.data)
}
}

Lets see the complete code of MainActivity.kt

package com.mobiledeveloperblog.camerasample1

import android.Manifest
import android.app.Activity
import android.content.ContentValues
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat

class MainActivity : AppCompatActivity() {
private val CAMERA_PERMISSION_CODE = 1000
private val READ_PERMISSION_CODE = 1001

private val IMAGE_CHOOSE = 1000
private val IMAGE_CAPTURE = 1001

private var imageUri: Uri? = null
private var imageView: ImageView? = null


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

imageView = findViewById(R.id.imageview_picture)

findViewById<Button>(R.id.button_take_picture).setOnClickListener {
// Request permission
val permissionGranted = requestCameraPermission()
if (permissionGranted) {
// Open the camera interface
openCameraInterface()
}
}

findViewById<Button>(R.id.button_choosefromgallery).setOnClickListener {
// Request permission
val permissionGranted = requestStoragePermission()
if (permissionGranted) {
// Open the Gallery
chooseImageGallery()
}
}
}

private fun requestStoragePermission(): Boolean {
var permissionGranted = false
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val storagePermissionNotGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_DENIED
if (storagePermissionNotGranted) {
val permission = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
requestPermissions(permission, READ_PERMISSION_CODE)
} else {
permissionGranted = true
}
} else {
permissionGranted = true
}
return permissionGranted
}

private fun requestCameraPermission(): Boolean {
var permissionGranted = false

// If system os is Marshmallow or Above, we need to request runtime permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val cameraPermissionNotGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_DENIED
if (cameraPermissionNotGranted) {
val permission = arrayOf(Manifest.permission.CAMERA)

// Display permission dialog
requestPermissions(permission, CAMERA_PERMISSION_CODE)
} else {
// Permission already granted
permissionGranted = true
}
} else {
// Android version earlier than M -> no need to request permission
permissionGranted = true
}

return permissionGranted
}

// Handle Allow or Deny response from the permission dialog
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == CAMERA_PERMISSION_CODE) {
if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission was granted
openCameraInterface()
} else {
// Permission was denied
showAlert("Camera permission was denied. Unable to take a picture.")
}
} else if (requestCode == READ_PERMISSION_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val storagePermissionGranted = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
if (storagePermissionGranted) {
chooseImageGallery()
} else {
showAlert("Storage permission was denied.")
}
} else {
showAlert("Storage permission Not Needed.")
}
}
}

private fun chooseImageGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_CHOOSE)
}

private fun openCameraInterface() {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, R.string.take_picture)
imageUri = contentResolver?.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

// Create camera intent
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)

// Launch intent
startActivityForResult(intent, IMAGE_CAPTURE)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

// Callback from camera intent
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_CAPTURE) {
// Set image captured to image view
imageView?.setImageURI(imageUri)
} else if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_CHOOSE) {
imageView?.setImageURI(data?.data)
}
}

private fun showAlert(message: String) {
val builder = AlertDialog.Builder(this)
builder.setMessage(message)
builder.setPositiveButton(R.string.ok_button_title, null)

val dialog = builder.create()
dialog.show()
}
}

Final Output Image:

Final Github Source Code Link:

https://github.com/Pandiyanmk/Camera-Storage

--

--