Search code examples
androidkotlinandroid-fileprovider

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/


I am trying to use ACTION_IMAGE_CAPTURE intent to capture an image and then save it to /data/data/**/app_profile/profile_picture.jpg I call the intent with the following code:

val cw = ContextWrapper([email protected])
val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)

output = File(dir, "/profile_picture.jpg")
intent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)

**val newPhotoUri = FileProvider.getUriForFile(applicationContext, "$packageName.fileprovider", output)**

intent.putExtra(MediaStore.EXTRA_OUTPUT, newPhotoUri)
startActivityForResult(intent, REQUEST_CAMERA)

Log.e(TAG, "output: ${output.absolutePath}") ///data/user/0/**/app_profile/profile_picture.jpg

But I get the following crash on FileProvider.getUriForFile()

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/**/app_profile/profile_picture.jpg
    at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
    at **.UserActivity.takePhoto(UserActivity.kt:147)

I have the following set in Manifest:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="**.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths" />
</provider>

My filepaths.xml:

<paths>
    <files-path name="app_profile" path="." />
</paths>

I have already tried

<files-path name="app_profile" path="app_profile/" />

and a lot of other combinations. Can someone guide me through?


Solution

  • I fixed it myself. Instead of doing

    val dir:File = cw.getDir("profile", Context.MODE_PRIVATE)
    

    I had to do

    val dir:File = cw.filesDir
    

    Which returns the correct /files/ directory. Now everything is working fine.