title: Cannot save captured image to external media directory using CameraX
body: <p>I was following this code labs example <a href="https://codelabs.developers.google.com/codelabs/camerax-getting-started/index.html?index=..%2F..index#4" rel="nofollow noreferrer">Link</a> 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</p>
  <p>Here is the method that creates the directory, which is called in Oncreate method:</p>
  <pre class="lang-kotlin prettyprint-override"><code>    private fun getOutputDirectory(): File {
              val mediaDir = externalMediaDirs.firstOrNull()?.let {
                  File(it, resources.getString(R.string.app_name)).apply { mkdirs() } }
              return if (mediaDir != null &amp;&amp; mediaDir.exists())
              { Log.i(TAG, mediaDir.path); mediaDir } else {  filesDir }
          }
  </code></pre>
  <p>from what I read in Android documentation <code>externalMediaDirs</code> creates a folder in external storage, and although my phone doesn't have external storage, the folder was successfully created at this path: <code>/storage/emulated/0/Android/media/com.example.camerax/cameraX</code></p>
  <p>then this method gets called when the take picture image button gets clicked:</p>
  <pre class="lang-kotlin prettyprint-override"><code>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()) + &quot;.jpg&quot;)
  
  
          // 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, &quot;Photo capture failed: ${exc.message}&quot;, exc)
                  }
  
                  override fun onImageSaved(output: ImageCapture.OutputFileResults) {
                      val savedUri = Uri.fromFile(photoFile)
                      val msg = &quot;Photo capture succeeded: $savedUri&quot;
                      Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                      Log.d(TAG, msg)
                  }
              })
  
  
      }
  </code></pre>
  <p>However, when I click the button to capture and save the image, I get this error message:
  <em>ImageCaptureException: &quot;Cannot save capture result to specified location&quot;</em></p>
  <p>What I tried:</p>
  <p>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:</p>
  <pre class="lang-kotlin prettyprint-override"><code>private fun takePhoto() {
  .
  .
  .
   folder: File = File(getFilesDir(), &quot;testFolder&quot;);
              if (!file.exists()) {
                  file.mkdir();
              }
  testImage = File(folder, &quot;test.jpg&quot;);
  
  val outputOptions = ImageCapture.OutputFileOptions.Builder(testImage).build()
  .
  .
  .
  .
  .
  .
  
  
  
  </code></pre>
  <p>Im not sure what could be the problem, any help is appreciated.</p>
  <p><strong>Update:</strong>
  Apparently the problem is due to a bug in CameraX API CameraX 1.0.0-beta09 along with camera-extension 1.0.0-alpha16. when using CameraX 1.0.0-beta08 along with camera-extension 1.0.0-alpha15 its working fine.</p>