In this article, we will see how to pick image from gallery and capture image from camera and set to imageview in Android Studio by using Kotlin Language. Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish Step 2: Go to activity_picker.xml file and add the following code <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <ImageView <Button <Button </LinearLayout> Step 3: Add following permissions in AndroidManifest.xml file <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> Step 4: Add provider inside application tag in AndroidManifest.xml file Step 5: Create a new Directory in res folder (name: "xml") and inside xml create a new xml resource file (name: "provider_paths.xml") and add the following code inside provider_paths.xml file <?xml version="1.0" encoding="utf-8"?> Step 6: Open PickerActivity.kt file and add the following code. private var imageView: ImageView? = null override fun onCreate(savedInstanceState: Bundle?) { initializeWidgets() btnCapture.setOnClickListener{capturePhoto()} private fun initializeWidgets() { private fun show(message: String) { val intent = Intent("android.media.action.IMAGE_CAPTURE") override fun onRequestPermissionsResult(requestCode: Int, permissions: Array override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { Step 7: Run the app on emulator or real device, you will get the output as following in video Complete Source Code of Image Picker and Capture from Camera Example activity_picker.xml file <?xml version="1.0" encoding="utf-8"?> <ImageView <Button <Button </LinearLayout> PickerActivity.kt file import android.annotation.SuppressLint class PickerActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { initializeWidgets() btnCapture.setOnClickListener{capturePhoto()} private fun initializeWidgets() { private fun show(message: String) { val intent = Intent("android.media.action.IMAGE_CAPTURE") override fun onRequestPermissionsResult(requestCode: Int, permissions: Array override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { provider_paths.xml file <?xml version="1.0" encoding="utf-8"?> AndroidManifest.xml file <?xml version="1.0" encoding="utf-8"?> <application <category android:name="android.intent.category.LAUNCHER" /> <provider </manifest> Conclusion: In this article we have covered how to Pick Images from Gallery and Capture Image from Camera in Android Studio by using Kotlin Language.
Implementation:
Step 1: Create a new Project in android studio.
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".PickerActivity">
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#D7EDBC"/>
android:id="@+id/btnCapture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="5dp"
android:text="Capture"
android:textColor="@color/white" />
android:id="@+id/btnChoose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Choose from Gallery"
android:textColor="@color/white" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
private var uri: Uri? = null
//Our widgets
private lateinit var btnCapture: Button
private lateinit var btnChoose : Button
//Our constants
private val CAPTURE_PHOTO = 1
private val CHOOSE_PHOTO = 2
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_picker)
btnChoose.setOnClickListener{
//check permission at runtime
val checkSelfPermission = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
if (checkSelfPermission != PackageManager.PERMISSION_GRANTED){
//Requests permissions to be granted to this application at runtime
ActivityCompat.requestPermissions(this,
arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
}
else{
openGallery()
}
}
}
btnCapture = findViewById(R.id.btnCapture)
btnChoose = findViewById(R.id.btnChoose)
imageView = findViewById(R.id.imageView)
}
Toast.makeText(this,message, Toast.LENGTH_SHORT).show()
}
private fun capturePhoto(){
val capturedImage = File(externalCacheDir, "My_Captured_Photo.jpg")
if(capturedImage.exists()) {
capturedImage.delete()
}
capturedImage.createNewFile()
uri = if(Build.VERSION.SDK_INT >= 24){
FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider",
capturedImage)
} else {
Uri.fromFile(capturedImage)
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, CAPTURE_PHOTO)
}
private fun openGallery(){
val intent = Intent("android.intent.action.GET_CONTENT")
intent.type = "image/*"
startActivityForResult(intent, CHOOSE_PHOTO)
}
private fun renderImage(imagePath: String?){
if (imagePath != null) {
val bitmap = BitmapFactory.decodeFile(imagePath)
imageView?.setImageBitmap(bitmap)
}
else {
show("ImagePath is null")
}
}
@SuppressLint("Range")
private fun getImagePath(uri: Uri?, selection: String?): String {
var path: String? = null
val cursor = contentResolver.query(uri!!, null, selection, null, null )
if (cursor != null){
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
}
cursor.close()
}
return path!!
}
@TargetApi(19)
private fun handleImageOnKitkat(data: Intent?) {
var imagePath: String? = null
val uri = data!!.data
//DocumentsContract defines the contract between a documents provider and the platform.
if (DocumentsContract.isDocumentUri(this, uri)){
val docId = DocumentsContract.getDocumentId(uri)
if ("com.android.providers.media.documents" == uri!!.authority){
val id = docId.split(":")[1]
val selsetion = MediaStore.Images.Media._ID + "=" + id
imagePath = getImagePath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
selsetion)
}
else if ("com.android.providers.downloads.documents" == uri.authority){
val contentUri = ContentUris.withAppendedId(
Uri.parse(
"content://downloads/public_downloads"), java.lang.Long.valueOf(docId))
imagePath = getImagePath(contentUri, null)
}
}
else if ("content".equals(uri!!.scheme, ignoreCase = true)){
imagePath = getImagePath(uri, null)
}
else if ("file".equals(uri!!.scheme, ignoreCase = true)){
imagePath = uri!!.path
}
renderImage(imagePath)
}
, grantedResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantedResults)
when(requestCode){
1 ->
if (grantedResults.isNotEmpty() && grantedResults.get(0) ==
PackageManager.PERMISSION_GRANTED){
openGallery()
}else {
show("Unfortunately You are Denied Permission to Perform this Operataion.")
}
}
}
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
CAPTURE_PHOTO ->
if (resultCode == Activity.RESULT_OK) {
val bitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri!!))
imageView!!.setImageBitmap(bitmap)
}
CHOOSE_PHOTO ->
if (resultCode == Activity.RESULT_OK) {
if (Build.VERSION.SDK_INT >= 19) {
handleImageOnKitkat(data)
}
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".PickerActivity">
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#D7EDBC"/>
android:id="@+id/btnCapture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:padding="5dp"
android:text="Capture"
android:textColor="@color/white" />
android:id="@+id/btnChoose"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Choose from Gallery"
android:textColor="@color/white" />
import android.annotation.TargetApi
import android.app.Activity
import android.content.ContentUris
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
import com.nishajain.kotinexamples.BuildConfig
import java.io.File
private var imageView: ImageView? = null
private var uri: Uri? = null
//Our widgets
private lateinit var btnCapture: Button
private lateinit var btnChoose : Button
//Our constants
private val CAPTURE_PHOTO = 1
private val CHOOSE_PHOTO = 2
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_picker)
btnChoose.setOnClickListener{
//check permission at runtime
val checkSelfPermission = ContextCompat.checkSelfPermission(this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
if (checkSelfPermission != PackageManager.PERMISSION_GRANTED){
//Requests permissions to be granted to this application at runtime
ActivityCompat.requestPermissions(this,
arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 1)
}
else{
openGallery()
}
}
}
btnCapture = findViewById(R.id.btnCapture)
btnChoose = findViewById(R.id.btnChoose)
imageView = findViewById(R.id.imageView)
}
Toast.makeText(this,message, Toast.LENGTH_SHORT).show()
}
private fun capturePhoto(){
val capturedImage = File(externalCacheDir, "My_Captured_Photo.jpg")
if(capturedImage.exists()) {
capturedImage.delete()
}
capturedImage.createNewFile()
uri = if(Build.VERSION.SDK_INT >= 24){
FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider",
capturedImage)
} else {
Uri.fromFile(capturedImage)
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, CAPTURE_PHOTO)
}
private fun openGallery(){
val intent = Intent("android.intent.action.GET_CONTENT")
intent.type = "image/*"
startActivityForResult(intent, CHOOSE_PHOTO)
}
private fun renderImage(imagePath: String?){
if (imagePath != null) {
val bitmap = BitmapFactory.decodeFile(imagePath)
imageView?.setImageBitmap(bitmap)
}
else {
show("ImagePath is null")
}
}
@SuppressLint("Range")
private fun getImagePath(uri: Uri?, selection: String?): String {
var path: String? = null
val cursor = contentResolver.query(uri!!, null, selection, null, null )
if (cursor != null){
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA))
}
cursor.close()
}
return path!!
}
@TargetApi(19)
private fun handleImageOnKitkat(data: Intent?) {
var imagePath: String? = null
val uri = data!!.data
//DocumentsContract defines the contract between a documents provider and the platform.
if (DocumentsContract.isDocumentUri(this, uri)){
val docId = DocumentsContract.getDocumentId(uri)
if ("com.android.providers.media.documents" == uri!!.authority){
val id = docId.split(":")[1]
val selsetion = MediaStore.Images.Media._ID + "=" + id
imagePath = getImagePath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
selsetion)
}
else if ("com.android.providers.downloads.documents" == uri.authority){
val contentUri = ContentUris.withAppendedId(
Uri.parse(
"content://downloads/public_downloads"), java.lang.Long.valueOf(docId))
imagePath = getImagePath(contentUri, null)
}
}
else if ("content".equals(uri!!.scheme, ignoreCase = true)){
imagePath = getImagePath(uri, null)
}
else if ("file".equals(uri!!.scheme, ignoreCase = true)){
imagePath = uri!!.path
}
renderImage(imagePath)
}
, grantedResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantedResults)
when(requestCode){
1 ->
if (grantedResults.isNotEmpty() && grantedResults.get(0) ==
PackageManager.PERMISSION_GRANTED){
openGallery()
}else {
show("Unfortunately You are Denied Permission to Perform this Operataion.")
}
}
}
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
CAPTURE_PHOTO ->
if (resultCode == Activity.RESULT_OK) {
val bitmap = BitmapFactory.decodeStream(
getContentResolver().openInputStream(uri!!))
imageView!!.setImageBitmap(bitmap)
}
CHOOSE_PHOTO ->
if (resultCode == Activity.RESULT_OK) {
if (Build.VERSION.SDK_INT >= 19) {
handleImageOnKitkat(data)
}
}
}
}
}
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Your Package Name">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
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.(AppName)>
<activity
android:name=".PickerActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
Article Contributed By :
|
|
|
|
2999 Views |