Friday, 14 May 2021

Calling ActivityResultContracts.TakePicture() false is returned so the image isn't being saved as expected

I am trying to save an image from the camera and it keeps returning false. I thought the image wasn't being displayed but when I looked at the filesystem I have lots of files but they are all of length zero.

Just some set up here.

val pictureResult = remember { mutableStateOf<Boolean?>(null)}
val context = LocalContext.current
val cameraLauncher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicture()) {
    pictureResult.value = it
    // $it is false
}

This will create the file that is in the screenshot.

fun createImageFile(extension:String): File {
    // Create an image file name
    val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
    val storageDir: File? = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
    val imagePath: File = File(context.filesDir, directoryFromExtension(extension))
    return File(imagePath, "justme${timeStamp}${extension}"
    ).apply {
        myNewsCreatorViewModel.currentPhotoPath = absolutePath
    }
}

When the button is clicked I go through these steps, starting with the file returned from the previous method.

                val uri = FileProvider.getUriForFile(context, "com.mine.fileprovider", file)
                cameraLauncher.launch(uri)

This is the result after I set the pictureResult.value, the idea being to then show the image, or I currently show a text message if imageSaved was false that the file wasn't saved.

        pictureResult.value?.let { imageSaved ->
            run {
                // imageSaved is false
            }
        }

I can't tell what I am doing wrong, and I can't tell how to get more information as to why it failed to save the image properly.

Screenshot of file system showing files with length zero



from Calling ActivityResultContracts.TakePicture() false is returned so the image isn't being saved as expected

No comments:

Post a Comment