Do I need read/write external to store photo in getExternalFilesDir(...)?

109 Views Asked by At

I am running into a problem when launching a camera intent when targeting Android 33 and running on Android 33.

I am using a file provider to create and pass a file to the camera

Manifest:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

filepaths:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path name="attachments" path="attachments/"/>
    <cache-path name="geopackage" path="geopackage/"/>
    <external-files-path name="pictures" path="Pictures"/>
    <external-files-path name="videos" path="Movies"/>
    <external-files-path name="audio" path="Music"/>
</paths>

Permissions request

  private val requestImagePermission = registerForActivityResult(
    ActivityResultContracts.RequestPermission()
  ) { result ->
    this.onPermission(result)
  }

Image request

  private val getImage = registerForActivityResult(
    ActivityResultContracts.TakePicture()
  ) { status ->
    onMediaResult(status)
  }

Launch camera intent

private fun launchCameraIntent() {
  val file = File.createTempFile(
    UUID.randomUUID().toString(),
    ".jpg",
    getExternalFilesDir(Environment.DIRECTORY_PICTURES)
  )

  val uri = FileProvider.getUriForFile(applicationContext, applicationContext.packageName + ".fileprovider", file)

  currentMediaPath = file.absolutePath
  getImage.launch(uri)
}

I am prompted for camera permission and accept. However upon launch of the camera I get:

Permission needed. To use camera, turn on Camera, Microphone, and Storage permissions.

enter image description here

Using the old permissions API this worked just fine when asking for just the Camera permission and storage permission. However I was under the impression that storing in getExternalFilesDir you did not need to ask for storage permission.

Any ideas?

1

There are 1 best solutions below

3
CommonsWare On

However I was under the impression that storing in getExternalFilesDir you did not need to ask for storage permission.

Correct. However, that dialog is not from the platform, but from that particular camera app. They probably did not take into account the ACTION_IMAGE_CAPTURE / ActivityResultContracts.TakePicture scenario, where it is the responsibility of the caller to supply a location that is writeable.