2

I am trying to capture Image using camera Intent and send it to server.

I have followed the official doc https://developer.android.com/training/camera/photobasics

But getting exception while using FileProvider to get the Uri from the filepath.

I am getting an exception

Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.app/files/Pictures/JPEG_20200310_160944_8900302509758571991.jpg

Code:

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-files-path name="my_images" path="Android/data/com.example.app/files" />
</paths>

AndroidManifest.xml

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.app.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
</provider>

CameraActivity.kt

private fun dispatchTakePictureIntent() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
        takePictureIntent.resolveActivity(packageManager)?.also {
            val photoFile: File? = try {
                viewModel.createFile()
            } catch (ex: IOException) {
                // Error occurred while creating the File
                null
            }
            photoFile?.also {
                 photoURI= FileProvider.getUriForFile(
                    this,
                    "com.example.app.fileprovider",
                    it
                )
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
                startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO)
            }
        }
    }
}

Please help me figure out where I am wrong.

Although I have checked the possible answers on stackoverflow but unable to resolve it.

Saket Mayank
  • 55
  • 1
  • 16

1 Answers1

2

Replace:

path="Android/data/com.example.app/files"

with:

path="."

<external-files-path> already points to the location that you are identifying in path.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am getting the URI as below **content://com.example.app.fileprovider/my_images/Pictures/JPEG_20200310_185418_5229824733138978989.jpg** And when I try to access the file it's giving **/my_images/Pictures/JPEG_20200310_185418_5229824733138978989.jpg (No such file or directory)** @CommonsWare – Saket Mayank Mar 10 '20 at 13:29
  • 1
    "I am getting the URI as below content://com.example.app.fileprovider/my_images/Pictures/JPEG_20200310_185418_5229824733138978989.jpg" -- that seems correct. "And when I try to access the file it's giving /my_images/Pictures/JPEG_20200310_185418_5229824733138978989.jpg (No such file or directory)" -- presumably, you are calling `getPath()` on the `Uri` and trying to do something with that. Stop doing that. You already have the `File` -- you are passing it into `getUriForFile()`. – CommonsWare Mar 10 '20 at 13:41
  • as an alternate answer - I've successfully used `` together with `val location = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)` – Someone Somewhere Nov 24 '20 at 01:44