Issue
I want to use system camera to take photos and save to external storage. Here is what I've done.
val dUri = if (Build.VERSION.SDK_INT > 28) {
MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else {
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
}
val fileName = "ride_map-camera-${System.currentTimeMillis()}.jpg"
val newImageDetails = ContentValues().apply {
put(MediaStore.Images.Media.DISPLAY_NAME, fileName)
put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
}
val newUri = context.contentResolver.insert(dUri,newImageDetails)
val cameraLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.TakePicture()) { it: Boolean ->
}
Button(
modifier = Modifier.size(90.dp, 60.dp),
onClick = {
val permissionCheckResult = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
if (permissionCheckResult == PackageManager.PERMISSION_GRANTED) {
cameraLauncher.launch(newUri)
} else {
permissionLauncher.launch(Manifest.permission.CAMERA)
}
}
) {
Icon(painter = painterResource(
id = R.drawable.camera),
contentDescription = null,
tint = MaterialTheme.colorScheme.tertiary
)
}
Problem is Latest image will replace the former one. It only saves one image. It saves empty image if I open the camera without taking a photo or dismiss the photo. The empty image has 0KB size and -1*-1 dimension.
The first problem is fixed. It seems inevitable that an empty file will be saved. How can i delete it?
Solution
Problem is fixed. Just create a temp file to save the result of takePicture. If it returns ture then save the temp file to gallery. Or you can delete the empty file when it returns false.
Answered By - Ruineie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.