Working With Camera & Gallery for Image Picker in Jetpack compose

 




Lets have some code to know. 

First, Android 13 introduced the no permission image picker from galley , using this we can pick image from galley without taking permission for Galley . It returns the Image  Uri , To display this image set up coil compose already 

Step 1: -  Create a state variable to store the uri of selected image 

var pickedUri by remember { mutableStateOf<Uri?>(null) }

Step 2: Create the pick Media activity launcher contract 

val pickMedia =
rememberLauncherForActivityResult(ActivityResultContracts.PickVisualMedia()) { mUri ->
if (mUri != null) {
pickedUri = mUri
} else {
Log.d("PARASH", "No Media Selected ")
}

}


Step 3 :- Launch the launcher to pick the image 

Button(onClick = {
pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))

}) {
Text(text = "Pick Image Without Permission ")
}


Now display it using coil compose 

Next Lets setup camera for image capture 

using this approach, we get the image bitmap and then, we display the bitmap in our image 

Step 1 : Create the state variable to store image Bitmap 

var imageBitmap by remember { mutableStateOf<Bitmap?>(null) }


Step 2 : Create a activity launcher Contract for this , 

val cameraPicker =
rememberLauncherForActivityResult(contract = ActivityResultContracts.TakePicturePreview()) {

if (it != null) {
imageBitmap = it
} else {
Log.d("PARASH", "No Bitmap Selected ")
}

}



Step 3: Launch this using this code on any button click 

cameraPicker.launch()



Points to be remember using second method requires the camera permission so follow this step to setup camera permission 


[Android Manifest]

<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />

Also, for the imagePicker from gallery to work in previous android add these lines 

<!-- Trigger Google Play services to install the backported photo picker module. -->
<service android:name="com.google.android.gms.metadata.ModuleDependencies"
android:enabled="false"
android:exported="false"
tools:ignore="MissingClass">
<intent-filter>
<action android:name="com.google.android.gms.metadata.MODULE_DEPENDENCIES" />
</intent-filter>
<meta-data android:name="photopicker_activity:0:required" android:value="" />
</service>



Now request for runtime Permission 

Step 1: create PermissionLauncher Contract 

val permissionLauncher =
rememberLauncherForActivityResult(contract = ActivityResultContracts.RequestPermission()) { isGranted ->


Log.d("PARASH", "isGranted $isGranted")
if (isGranted) {
cameraPicker.launch()
} else {
shouldShowCameraRationale = ActivityCompat.shouldShowRequestPermissionRationale(
context as Activity,
Manifest.permission.CAMERA
)

Log.d("PARASH", "isRationale $shouldShowCameraRationale")
if (!shouldShowCameraRationale) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", context.packageName, null)
}

context.startActivity(intent)
}
}
}


Using this code take the permission by launching 


Button(onClick = {
if (ContextCompat.checkSelfPermission(
context,
Manifest.permission.CAMERA
) == PackageManager.PERMISSION_GRANTED
) {
cameraPicker.launch()
} else {
permissionLauncher.launch(Manifest.permission.CAMERA)

if (shouldShowCameraRationale) {
Toast.makeText(context, "Camera Permission Required", Toast.LENGTH_SHORT)
.show()
}

}


}) {
Text("Pick Image From Camera ")
}



.

Post a Comment

0 Comments