Cannot save captured image to external media directory using CameraX

666 views
Skip to first unread message

Mason

unread,
Sep 20, 2020, 6:36:52 PM9/20/20
to Android CameraX Discussion Group

I was following this code labs example Link on how to use CameraX API on Android, however when I tried to capture and save the image to the external media directory that is created in Oncreate method in the main activity I get an error message saying: Cannot save capture result to specified location

Here is the method that creates the directory, which is called in Oncreate method:

private fun getOutputDirectory(): File { val mediaDir = externalMediaDirs.firstOrNull()?.let { File(it, resources.getString(R.string.app_name)).apply { mkdirs() } } return if (mediaDir != null && mediaDir.exists()) { Log.i(TAG, mediaDir.path); mediaDir } else { filesDir } }

I was following this code labs example Link on how to use CameraX API on Android, however when I tried to capture and save the image to the external media directory that is created in Oncreate method in the main activity I get an error message saying: Cannot save capture result to specified location

Here is the method that creates the directory, which is called in Oncreate method:

private fun getOutputDirectory(): File { val mediaDir = externalMediaDirs.firstOrNull()?.let { File(it, resources.getString(R.string.app_name)).apply { mkdirs() } } return if (mediaDir != null && mediaDir.exists()) { Log.i(TAG, mediaDir.path); mediaDir } else { filesDir } }

from what I read in Android documentation externalMediaDirs creates a folder in external storage, and although my phone doesn't have external storage, the folder was successfully created at this path: /storage/emulated/0/Android/media/com.example.camerax/cameraX

then this method gets called when the take picture image button gets clicked:

private fun takePhoto() { // Get a stable reference of the modifiable image capture use case val imageCapture = imageCapture ?: return // Create time-stamped output file to hold the image val photoFile = File( outputDirectory, SimpleDateFormat(FILENAME_FORMAT, Locale.US ).format(System.currentTimeMillis()) + ".jpg") // Create output options object which contains file + metadata val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile).build() // Set up image capture listener, which is triggered after photo has // been taken imageCapture.takePicture( outputOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback { override fun onError(exc: ImageCaptureException) { Log.e(TAG, "Photo capture failed: ${exc.message}", exc) } override fun onImageSaved(output: ImageCapture.OutputFileResults) { val savedUri = Uri.fromFile(photoFile) val msg = "Photo capture succeeded: $savedUri" Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() Log.d(TAG, msg) } }) }

However, when I click the button to capture and save the image, I get this error message:ImageCaptureException: "Cannot save capture result to specified location"

What I tried:

I tried to create a folder that is local to the App and save the image there, and it worked fine, I used this method:

private fun takePhoto() { . . . folder: File = File(getFilesDir(), "testFolder"); if (!file.exists()) { file.mkdir(); } testImage = File(folder, "test.jpg"); val outputOptions = ImageCapture.OutputFileOptions.Builder(testImage).build() . . . . . .

Im not sure what could be the problem, any help is appreciated.

Mason

unread,
Sep 20, 2020, 6:39:21 PM9/20/20
to Android CameraX Discussion Group, Mason
the formatting here is not clear.. check this link for a more clear formatting:

Leo Huang

unread,
Sep 20, 2020, 10:54:45 PM9/20/20
to Android CameraX Discussion Group, Mason
Hi 
Thank you for reporting this issue. From the error message "Cannot save capture result to specified location", you should be using the CameraX 1.0.0-beta09.
There is a known issue in CameraX 1.0.0-beta09 when using ImageCapture to take photo. We are currently fixing it and will release the next version as soon as possible. So please use CameraX 1.0.0-beta08 for now to see if the problem still exists. Sorry for any inconvenience caused.

To change the CameraX version, simply modify the build.gradle file.
Ex:
// CameraX core library
def camerax_version = '1.0.0-beta08'
implementation "androidx.camera:camera-core:$camerax_version"

// CameraX Camera2 extensions
implementation "androidx.camera:camera-camera2:$camerax_version"

// CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:$camerax_version"



Mason 在 2020年9月21日 星期一上午6:39:21 [UTC+8] 的信中寫道:

Mason

unread,
Sep 21, 2020, 12:56:05 AM9/21/20
to Android CameraX Discussion Group, leoh...@google.com
I was using cameraX beta08 but with camera extension 1.0.0-alpha16, not im using 1.0.0-alpha15 and it works fine. that for the help.
Reply all
Reply to author
Forward
0 new messages