Skip to main content

How to capture or pick image from Device in Kotlin Android

How to capture or pick image from Device in Kotlin Android.

Here's a step-by-step tutorial on how to capture or pick an image from a device using Kotlin in Android.

Step 1: Set up the project

  • Create a new Android project in Android Studio.
  • Choose an appropriate name, package, and minimum SDK version for your project.

Step 2: Add necessary permissions to the manifest file

  • Open the AndroidManifest.xml file.
  • Add the following permissions to access the device's camera and external storage:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 3: Create the layout for the activity

  • Open the activity_main.xml layout file.
  • Design the layout as per your requirements. In this example, we will use a simple layout with a button to capture or pick an image.

Step 4: Request runtime permissions (if necessary)

  • Starting from Android 6.0 (API level 23), you need to request certain permissions at runtime.
  • Add the following code to the activity's onCreate method to request the necessary permissions:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_PERMISSION)
}

Step 5: Implement image capture or pick functionality

  • Create a new Kotlin class called ImageUtils.kt.
  • Add the following code to capture an image from the device's camera:
fun captureImage(activity: Activity, requestCode: Int) {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
activity.startActivityForResult(intent, requestCode)
}
  • Add the following code to pick an image from the device's gallery:
fun pickImage(activity: Activity, requestCode: Int) {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type = "image/*"
activity.startActivityForResult(intent, requestCode)
}

Step 6: Handle the result in the activity

  • Open the activity file (e.g., MainActivity.kt).
  • Add the following code to handle the result of image capture or pick:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
REQUEST_IMAGE_CAPTURE -> {
val imageBitmap = data?.extras?.get("data") as Bitmap
// Do something with the captured image bitmap
}
REQUEST_IMAGE_PICK -> {
val imageUri = data?.data
// Do something with the picked image URI
}
}
}
}

Step 7: Use the image capture or pick functionality

  • Open the MainActivity.kt file.
  • Add the following code to capture an image when the capture button is clicked:
captureButton.setOnClickListener {
ImageUtils.captureImage(this, REQUEST_IMAGE_CAPTURE)
}
  • Add the following code to pick an image when the pick button is clicked:
pickButton.setOnClickListener {
ImageUtils.pickImage(this, REQUEST_IMAGE_PICK)
}

More Examples