'How to preview an image and only saving it if the user accepts it using cameraX
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?
Solution 1:[1]
You can save your temporary images somewhere of your internal storage, for example temp folder. If the user presses "accept image" button you can copy the image from temp folder to your desired location. If the user exits the app or the app crashes, the temporary image may remain in the temp folder.
And don't forget to clear your temp folder when you enter the application next time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Javlon |