问题
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:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="todolistj.todolist.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="lists_to_send" path="export/"/>
</paths>
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);
outputStream.write(content.getBytes());
outputStream.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
And the Uri creation:
File readF = new File(fullFileName);
Uri uri = FileProvider.getUriForFile(this, "todolistj.todolist.fileprovider", readF);
where fullFileName
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 (https://developer.android.com/reference/android/support/v4/content/FileProvider.html) 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) Context.getExternalFilesDir(null).
So external-files-path from my xml seems to match the context.getExternalFilesDir
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"
回答1:
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 FileProvider.java 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.
回答2:
File photoFile =new File(fullFileName);
if (photoFile != null) {
Uri photoURI =FileProvider.getUriForFile(MainActivity.this(your activity name),
BuildConfig.APPLICATION_ID + ".provider", photoFile);
}
if you create Uri like this may be it work for you.
回答3:
There is some workaound:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="logs" path="../logs/" />
<!-- use ".." to get files from app's root data dir -->
</paths>
to provide files from /data/data/xxx.xxx.xxx/
in my case it was /data/data/xxx.xxx.xxx/logs/
来源:https://stackoverflow.com/questions/39293114/fileprovider-failed-to-find-configured-root-exception