Skip to content

Instantly share code, notes, and snippets.

@amoozeshbebin
Created September 6, 2023 09:51
Show Gist options
  • Save amoozeshbebin/8b245e08b9f2730fe21d01d9d5823a45 to your computer and use it in GitHub Desktop.
Save amoozeshbebin/8b245e08b9f2730fe21d01d9d5823a45 to your computer and use it in GitHub Desktop.
Camera Permission in android/kotlin
MainActivity.kt:
package com.example.permission
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
class MainActivity : AppCompatActivity() {
lateinit var imageView:ImageView
lateinit var button:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
private fun init(){
bindView()
button.setOnClickListener { chackandOpencamera() }
}
fun bindView(){
button = findViewById(R.id.button)
imageView = findViewById(R.id.imageView)
}
val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
opencamera()
} else {
Toast.makeText(this,"permission deind",Toast.LENGTH_LONG).show()
}
}
fun chackandOpencamera(){
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// Register the permissions callback, which handles the user's response to the
// system permissions dialog. Save the return value, an instance of
// ActivityResultLauncher. You can use either a val, as shown in this snippet,
// or a lateinit var in your onAttach() or onCreate() method.
if(checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
opencamera()
}else{
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
}
}
fun opencamera(){
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
requestImageCapture.launch(takePictureIntent)
}
val requestImageCapture = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result ->
if (result.resultCode == RESULT_OK && result.data != null){
val img = result.data?.extras!!["data"] as Bitmap
imageView.setImageBitmap(img)
}
}
}
_______________________________________________________________________
activity_main.xml:
<?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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="129dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="207dp"
android:layout_height="204dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout><?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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="129dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="207dp"
android:layout_height="204dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
____________________________________________________________
androidManifest:
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment