Reputation: 174
Recently we are seeing a few crashes for our app where exception is
Fatal Exception: java.io.FileNotFoundException /data/user/0/<package-id>/files/folder_name/file_name.jpg : open failed: ENOENT (No such file or directory)
Observations while debugging this issue
val file = File("${context.filesDir}/$folderName")
this is how app is creating a File objectSuccessfully queried cache dir: /data/user_de/0/<package_id>/code_cache
Setting cache dir: /data/user_de/0/<package_id>/code_cache
user_de: is something I could not understand why/from where it is coming up but when I explored more I got this context.createDeviceProtectedStorageContext().filesDir
But without this line of code My app was working all okay using context.filesDir
Is there anything I am missing here?
Edit:
This is the code that produces an exception (only on some devices for example SM-G398FN, SM-G525F, there are some instances where device model is same but we did not get exceptions things went fine we got all needed images)
CameraX implementation for capturing and storing images at context.filesDir
private fun takePhoto() {
val imageCapture = imageCapture ?: return
val imagesFolderFile = File(
context.filesDir,
"$folderName${File.separator}$imagesFolderName"
)
val photoFile = File(
imagesFolderFile,
"Images/image_timestamp.jpg"
)
photoFile.mkdirs()
val outputOptions =
ImageCapture.OutputFileOptions.Builder(photoFile).build()
imageCapture.takePicture(
outputOptions,
ContextCompat.getMainExecutor(this),
object : ImageCapture.OnImageSavedCallback {
override fun onError(exc: ImageCaptureException) {
// We are getting exceptions from this line
Log.e(imageCaptureLogs, exc.message.toString())
exc.printStackTrace()
}
override fun onImageSaved(output:
ImageCapture.OutputFileResults) {
}
}
}
Upvotes: 0
Views: 361
Reputation: 5600
It looks like the issue might be related to the creation of the photo file and its parent directories. Specifically, there's an error in the way you are creating the directories for the photoFile.
Instead of using photoFile.mkdirs(), which creates both the file and its parent directories, you should create the parent directories first using File.mkdirs() and then create the file
Upvotes: 0