Jeni Jeni - 3 months ago 44
Android Question

FileProvider "Failed to find configured root" exception

One more of these FileProvider lost souls... I have been working on this problem more than a day now, and it seems I am missing something big. Any help would be appreciated!

I am trying to send email with attachment using FileProvider.

The part of my AndroidManifest.xml:

<meta-data android:name=""
android:resource="@xml/file_paths" />


<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="">
<external-files-path name="lists_to_send" path="export/"/>

Creating the attachment:

String content = "hello world";
File file;
FileOutputStream outputStream;
try {
File dir = context.getExternalFilesDir("export");
if(!dir.exists()) dir.mkdir();
file = new File(dir, "MyCache");
if (file.exists ()) file.delete ();
outputStream = new FileOutputStream(file);
return file.getAbsolutePath();
} catch (IOException e) {

And the Uri creation:

File readF = new File(fullFileName);
Uri uri = FileProvider.getUriForFile(this, "todolistj.todolist.fileprovider", readF);

is the value returned in the snippet for file creation.

On the Uri creation line I get exception:

Caused by: java.lang.IllegalArgumentException: Failed to find configured
root that contains /storage/emulated/0/Android/data/todolistj.todolist/files/export/MyCache

As the documentation here ( states:

< external-files-path name="name" path="path" />
Represents files in the root of your app's external storage area. The root path of this subdirectory is the same as the value returned
by Context#getExternalFilesDir(String)

So external-files-path from my xml seems to match the
method I am using.
Both places I have the "export" folder. Authorities seem to match... What could be my problem. I found no way to find and print FileProvider's "configured root"


It seems I found workaround or fix. Changing the root directory type I was using from external-files-path to cache-path in the xml and from context.getExternalFilesDir("export"); to File dir = new File(context.getCacheDir(), "export"); for getting the folder to create the file.

And I successfully attached the file. Note that in the for the FileProvider class I found the following code for constructing the Uris:

        if (TAG_ROOT_PATH.equals(tag)) {
            target = buildPath(DEVICE_ROOT, path);
        } else if (TAG_FILES_PATH.equals(tag)) {
            target = buildPath(context.getFilesDir(), path);
        } else if (TAG_CACHE_PATH.equals(tag)) {
            target = buildPath(context.getCacheDir(), path);
        } else if (TAG_EXTERNAL.equals(tag)) {
            target = buildPath(Environment.getExternalStorageDirectory(), path);

Which looks like only the following 4 folders are supported: TAG_ROOT_PATH, TAG_FILES_PATH, TAG_CACHE_PATH, TAG_EXTERNAL. There is no TAG_EXTERNAL_FILES or something like this, so this seems like a mismatch between the documentation and the implementation, and actually the external-files-path matching the getExternalFilesDir method is not supported.