Android: FileProvider IllegalArgumentException Failed to find configured root that contains /data/data/**/files/Videos/final.mp4

断了今生、忘了曾经 提交于 2019-11-27 15:28:49

问题


I am trying to use FileProvider to play a video from private path.Facing

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/XXXXX(Package)/files/Videos/final.mp4

Code:

<paths>
    <files-path path="my_docs" name="Videos/" />
</paths>

Java code:

File imagePath = new File(getFilesDir(), "Videos");
File newFile = new File(imagePath, "final.mp4");
Log.d(TAG, "-------------newFile:"+newFile.exists());//True here
//Exception in below line
Uri contentUri = FileProvider.getUriForFile(this,"com.wow.fileprovider", newFile);

Manifest.xml

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.wow.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">

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

Any clues on this?

Thanks Nitz


回答1:


You have your name and your path flipped. name is what goes in the Uri, and path is the relative location within the root on the filesystem.

Go with:

<paths>
    <files-path name="my_docs" path="Videos/" />
</paths>



回答2:


I had the very same basic situation. I defined everything correctly (files-path in xml) but there is one more thing which leads to the same exception. I add another answer just as an addition and a comment would not be well readable.

I created/read the directory where i store the files like:

context.getDir("Documents", Context.MODE_PRIVATE) 

This leads to a path like:

/data/user/0/ch.myapp/app_Documents/6c3c70d5-af66-48ef-8dfc-f4341de4e1bd.docx

Then i changed creating the directory to:

File directory = new File(context.getFilesDir(), "Documents");
if (!directory.exists()) {
  directory.mkdir();
}

This leads to a path like:

/data/user/0/ch.myapp/files/Documents/6c3c70d5-af66-48ef-8dfc-f4341de4e1bd.docx

According to the documentation Open a directory the two methods should be equivalent as far as i understand it. But it creates a different path... Maybe the formulation is just not clear for me in the documentation, but for me its wrongly written.

getDir(name, mode)

Creates a new directory (or opens an existing directory) within your app's unique file system directory. This new directory appears inside the directory provided by getFilesDir().



来源:https://stackoverflow.com/questions/31345275/android-fileprovider-illegalargumentexception-failed-to-find-configured-root-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!