IllegalArgumentException: Failed to find configuration root that contains xxx on FileProvider.getUriForFile

22,555

Solution 1

The path of that file begins exactly with what getExternalFilesDir(Environment.DIRECTORY_PICTURES) returns.

AFAIK, that will not give you a directory named SpCars_album/.

I just cant understand what raises the error because everything seems to fit.

The file you supplied is not one that can be served by the FileProvider from your defined roots.


UPDATE

I forgot that this is tied to a documentation bug on FileProvider. FileProvider and <external-path> does NOT serve files from getExternalFilesDir(), but instead from Environment.getExternalStorageDirectory(). I created a StreamProvider subclass that offers support for getExternalFilesDir().

If you use my StreamProvider, replacing your <external-path> with <external-files-path name="myexternalimages" path="Pictures/SpCars_album/" />, you should have better luck.

Solution 2

This is an old question now, but I've just had a similar issue.

A lazy solution I found is to just use:

<external-path name="myexternalimages" path="Android/" />

Because getExternalStorageDirectory returns "/storage/emulated/0/", sharing /Android (the next level down) will allow any files that are in your app to be accessed.

Alternatively, if you want to be more precise, you can do this:

<external-path name="myexternalimages" path="Android/data/YOUR_BUNDLE_ID/files/SpCars_album" />

This doesn't feel quite right, but it does seem to work!

Share:
22,555
Hummus
Author by

Hummus

Updated on December 11, 2020

Comments

  • Hummus
    Hummus over 3 years

    I have been trying to follow the Android tutorial on sharing files. I set up the FileProvider like this:

    On the main manifest xml:

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

    the res/xml/filpaths.xml file:

    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path name="myexternalimages" path="SpCars_album/" />
    </paths>
    

    And in my code I am trying the following:

    File requestFile = new File(mImageFilenames[position]);
                    try {
                        fileUri = FileProvider.getUriForFile(
                                SeventhActivity.this,
                                "com.example.mysecondapp.fileprovider",
                                requestFile);
                    } catch (IllegalArgumentException e) {
                        Log.e("File Selector",
                                "The selected file can't be shared: " +
                                        mImageFilenames[position]);
                    }
    

    The requestFile is instantiated with a proper working path for a file. The path of that file begins exactly with what getExternalFilesDir(Environment.DIRECTORY_PICTURES) returns. I just cant understand what raises the error because everything seems to fit. Thanks in advance.

  • Hummus
    Hummus over 10 years
    Then assuming that the files I would like to serve are in the following path: '/storage/emulated/0/Android/data/com.example.mysecondapp/fi‌​les/Pictures/SpCars_‌​Album/' What should I specify in my filepaths xml file?
  • Chepech
    Chepech almost 8 years
    @eAj: Worked like a charm - IOU a beer
  • IgorGanapolsky
    IgorGanapolsky almost 8 years
    Wait a second, the user clearly wants to use getExternalFilesDirs, NOT getExternalStorageDirectory. So why should we specify <external-path> instead of <files-path>?
  • xAqweRx
    xAqweRx over 7 years
    @IgorGanapolsky cause <files-path> is serves internal memory files directory /data/data/BUNDLE/files not external storeage