I use cameraX api for taking a picture and save an image as a file to the internal memory.
When the callback for taking a picture has succeeded, I update the ui in onImageSaved showing the user the captured image, and asking if they want to save it or take a new picture. If the user chooses to take a new picture I delete the taken picture from internal memory and update the ui so the user can take a new picture.
private fun takePhoto() {
// Create time stamped name and File entry.
val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US)
.format(System.currentTimeMillis())
val imageFile = File(context?.filesDir, "$name.jpg")
val outputOptions = activity?.contentResolver?.let {
ImageCapture.OutputFileOptions.Builder(imageFile).build()
}
This function has a callback onImageSaved which updates ui when image capture is successful
onImageSaved(output: ImageCapture.OutputFileResults){
savedImageUri = output.savedUri
updateUiForImagePreview(savedImageUri)
}
If the user doesn't want the captured image and press the button "take new picture", this will delete the savedImageFile
savedImageFile?.delete()
The problem I have is if the app crash, or if the user shuts the app down, the image is still present in the internal memory.
QUESTION
Is there a way to present the image to the user and not save the image to internal memory before the user press the "accept image" button?