You are on page 1of 3

Camera & Gallary And Permission

activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_margin="30dp">
<ImageView
android:id="@+id/ivImage"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="#dedede"
/>
<Button
android:id="@+id/btCapturePhoto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="@android:color/white"
android:text="Capture Photo"
/>
<Button
android:id="@+id/btOpenGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="@android:color/white"
android:text="Open Gallery"
/>
</LinearLayout>
</RelativeLayout>
MainActivity.kt
package com.example.capp
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
private val REQUEST_PERMISSION = 100
private val REQUEST_IMAGE_CAPTURE = 1
private val REQUEST_PICK_IMAGE = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var btCapturePhoto=findViewById<Button>(R.id.btCapturePhoto)
var btOpenGallery=findViewById<Button>(R.id.btOpenGallery)
btCapturePhoto.setOnClickListener {
openCamera()
}
btOpenGallery.setOnClickListener {
openGallery()
}
}
override fun onResume() {
super.onResume()
checkCameraPermission()
}
private fun checkCameraPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Toast.makeText(this, "Permission granted",
Toast.LENGTH_LONG).show()
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.CAMERA),
REQUEST_PERMISSION)
//Toast.makeText(this, "Permission denied",
Toast.LENGTH_LONG).show()
}
}
private fun openCamera() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { intent ->
intent.resolveActivity(packageManager)?.also {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE)
}
}
}
private fun openGallery() {
Intent(Intent.ACTION_GET_CONTENT).also { intent ->
intent.type = "image/*"
intent.resolveActivity(packageManager)?.also {
startActivityForResult(intent, REQUEST_PICK_IMAGE)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data:
Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
var ivImage=findViewById<ImageView>(R.id.ivImage)
val bitmap = data?.extras?.get("data") as Bitmap
ivImage.setImageBitmap(bitmap)
}
else if (requestCode == REQUEST_PICK_IMAGE) {
var ivImage=findViewById<ImageView>(R.id.ivImage)
val uri = data?.getData()
ivImage.setImageURI(uri)
}
}
}
}

You might also like