Kotlin Capture Image From Camera Example

Kotlin Capture Image From Camera Example

Project Flow Image:
build.gradle(:app) :
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
compileSdk 32

defaultConfig {
applicationId "com.kotlinkatta.demo"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
dataBinding true;
viewBinding true
}
}

dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
AndroidManifest.xml :
add following code into AndroidManifest.xml file.
<provider
android:authorities="com.kotlinkatta.demo.fileProvider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kotlinkatta.demo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Demo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:authorities="com.kotlinkatta.demo.fileProvider"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
</manifest>
Step 1:
Create resource filepaths.xml file (res -> xml-> filepaths.xml) and add following code.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path
name="capture_photos"
path="." />
</paths>
Step 2:
Open activity_main.xml file and add following code.
<?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"
tools:context=".MainActivity">

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btncapphoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#FF6200EE"
android:text="Capture Photo"
android:textAllCaps="false"
android:textColor="@color/white" />

<ImageView
android:id="@+id/imgprofileimage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Step 3:
Open MainActivity.xml file and add following code.
package com.kotlinkatta.demo

import android.net.Uri
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import com.kotlinkatta.demo.databinding.ActivityMainBinding
import java.io.File

class MainActivity : AppCompatActivity() {
lateinit var activityMainBinding: ActivityMainBinding
lateinit var imageUri: Uri
val getContract = registerForActivityResult(ActivityResultContracts.TakePicture()) {
activityMainBinding.imgprofileimage.setImageURI(null)
activityMainBinding.imgprofileimage.setImageURI(imageUri)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
val view = activityMainBinding.root
setContentView(view)

imageUri = createImageUri()!!
activityMainBinding.btncapphoto.setOnClickListener {
getContract.launch(imageUri)
}
}

private fun createImageUri(): Uri? {
val image = File(applicationContext.filesDir, "capture_photo.png")
return FileProvider.getUriForFile(
applicationContext,
"com.kotlinkatta.demo.fileProvider",
image
)
}
}
Output :

Comments