Camera & Gallery (with Permission)
build.gradle (:app)
...
android {
buildFeatures {
viewBinding true
}
...
}
dependencies {
implementation "androidx.activity:activity-ktx:1.3.1"
implementation "androidx.fragment:fragment-ktx:1.3.1"
...
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
lateinit var cameraPermission: ActivityResultLauncher<String> // Camera Permission
lateinit var storagePermission: ActivityResultLauncher<String> // Storage Permission
lateinit var cameraLauncher: ActivityResultLauncher<Uri> // Call Camera Application
lateinit var galleryLauncher: ActivityResultLauncher<String> // Call Gallery Application
var photoUri: Uri? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
storagePermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
setViews()
} else {
Toast.makeText(baseContext, "Permission must be approved", Toast.LENGTH_SHORT).show()
finish()
}
}
cameraPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
openCamera()
} else {
Toast.makeText(baseContext, "Permission must be approved", Toast.LENGTH_SHORT).show()
}
}
cameraLauncher = registerForActivityResult(ActivityResultContracts.TakePicture()) { isSuccess ->
if (isSuccess) {
binding.imageView.setImageURI(photoUri)
}
}
galleryLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
binding.imageView.setImageURI(uri)
}
storagePermission.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
private fun setViews() {
binding.btnCamera.setOnClickListener {
cameraPermission.launch(Manifest.permission.CAMERA)
}
binding.btnGallery.setOnClickListener {
openGallery()
}
}
private fun openCamera() {
val photoFile = File.createTempFile("IMG_", ".jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES))
photoUri = FileProvider.getUriForFile(this, "${packageName}.provider", photoFile)
cameraLauncher.launch(photoUri)
}
private fun openGallery() {
galleryLauncher.launch("image/*")
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pokycookie.camera">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="CAMERA"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Camera">
<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: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/file_paths" />
</provider>
</application>
</manifest>
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="my_images"
path="Android/data/com.pokycookie.camera/files/Pictures" />
</paths>