androidandroid-cameraandroid-fileprovider

Android FileProvider.getUriForFile Failed to find configured root


The issue is when I call getTmpFileUri() I get something like this:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.sample.myapp/cache/tmp_image_file_1764639852023756356.jpg

Here is my code for getting the Uri:

fun getTmpFileUri(context: Context, cacheDir: File): Uri {
            val tmpFile = File.createTempFile("tmp_image_file_", ".jpg", cacheDir).apply {
                deleteOnExit()
            }
            return FileProvider.getUriForFile(
                context,
                "${BuildConfig.APPLICATION_ID}.fileprovider",
                tmpFile
            )
        }

My file_provider_paths.xml looks like this:

<paths>,
    <external-files-path
        name="app_images"
        path="." />
    <cache-path
        name="cached_files"
        path="." />
    <files-path
        name="images"
        path="." />
</paths>

In my Manifest I have:

        <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/file_provider_paths" />
        </provider>

I am using the exact same setup and code in two other apps that also use the camera. The apps all use the same minSdk, gradle version, and libraries (33)For some reason the code in this app doesn't work. This app is more complicated and has a lot of permissions defined in it. I did verify that the camera permission is not in the merged xml file, in case that would cause an issue.

I've tried various SO solutions, some of which suggest changes to the xml file, which did not work for me. This post is the closest to my issue, but the solution does not work: Android: FileProvider.getUriForFile "Failed to find configured root"

Thanks in advance for any suggestions!


Solution

  • Thanks for quick responses. After wasting all kinds of time on this, I couldn't see any problems with it, so I finally Invalidated Cache and restarted (restart wasn't enough) and voila, it works! omg, thanks Android!