Taking Camera Pictures in Android
Lets see how to take a picture using camera in android and display it on an ImageView.
So the first step is to add the necessary permission that needed for accessing camera on the AndroidManifest File which is
<uses-permission android:name="android.permission.CAMERA"/>
So once we created new project we will be having activity_main.xml file. so that we can start designing the screen which has a Button(Take Picture) and an ImageView for displaying captured image.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/button_take_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture" />
<ImageView
android:id="@+id/imageview_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Lets Jump on to our MainActivity.kt File we see code step by step explanation and at the last the entire code
first we need to get permission for accessing camera from user. so we display permission screen
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
}
Lets see how the requestPermissions screen look like
So if the permission denied it will jump on to the else block where we show dialog with message as “Camera permission was denied. Unable to take a picture.” or else we will opening mobile camera for taking pictures.
// 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.");
}
}
}
so on permission allowed we have to open camera so the function openCameraInterface() will trigger camera
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_CODE)
}
imageUri ---> Defines Path of imagee ssaved
Once image captured using camera on success we will be displaying picture on on imageview or if not taken and pressed back we will be displaying error message as “Failed to take camera picture”
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Callback from camera intent
if (resultCode == Activity.RESULT_OK){
// Set image captured to image view
imageView?.setImageURI(imageUri)
}
else {
// Failed to take picture
showAlert("Failed to take camera picture")
}
}
Final Output:
GitHub Source Code: