How to Capture Image using Camera in Android Kotlin.

Harshita Bambure
2 min readNov 21, 2021

Everyone clicks images every day using the camera but do you know how the camera functionality is working in android?

Today we will learn about camera functionality in android kotlin.

First of all, we need to add a build feature in the build.Gradle(:app) file.

android {
buildFeatures {
viewBinding true
}
}

Then we need to add uses features in the Manifest. kt file.

<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />

Then we will design our activity_main.xml file.

<?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/btn_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Camera"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="30dp" />

<ImageView
android:id="@+id/img_viewer"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/btn_camera"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

In last we will code in MainActivity.kt

package com.example.camera

import android.content.ActivityNotFoundException
import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import com.example.camera.databinding.ActivityMainBinding

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

}
private fun dispatchTakePictureIntent() {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
try {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
} catch (e: ActivityNotFoundException) {
// display error state to the user
}

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data?.extras?.get("data") as Bitmap
binding.imgViewer.setImageBitmap(imageBitmap)
}
}
}

That’s it.

Image capture using camera.

The source code.

Thank you for reading. Happy Coding :)

--

--

Harshita Bambure

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