Android Fileprovider: IllegalArgumentException: Failed to find configured root that contains

依然范特西╮ 提交于 2020-01-01 04:12:05

问题


I have a question about the android FileProvider. I want to save a pdf document and open it with a default program. I don´t want to save it in external Storage.

After I´ve successfully saved the pdf to the FilesDirectory/export/temp.pdf,
I´ve tried to generate an URI by using FileProvider.getUriForFile().

File path = new File(getFilesDir(), "export");
File pdf = new File(path + File.separator + "temp.pdf");
pdf.getParentFile().mkdirs();

if (!pdf.exists())
    pdf.createNewFile();

Uri uri = FileProvider.getUriForFile(getApplicationContext(), "?", pdf);

Question: What do I have to pass as the second parameter "Authority" - the location of my Files, the class which can grant the URI-Permissions or something other? Whatever I´ve tried leaded to an IllegalArgumentException or a NullPointerException. My FileProvider (XML):

<provider         
    android:name="android.support.v4.content.FileProvider"           
        android:authorities="com.example.myApp.myActivity"
        android:exported="false"
        android:grantUriPermissions="true">

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

referenced File:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="pdfTemplates" path="export/" />
</paths>

回答1:


I´ve got it. There were two different Problems

  1. The first Issue was answered by CodeDiving. I had to use the Authority from the provider-declaration for the getUriForFile call. Using an other class caused the NullPointerException.

  2. I´ve tried to get a file from filesDirectory, but in my file_path I declared only a path to cache Directory. I changed it to 'files-path' and it worked. This error caused the IllegalArgumentException.




回答2:


According to your FileProvider file(XML), the second parameter is com.example.myApp.myActivity. That is

Uri uri = FileProvider.getUriForFile(getApplicationContext(),
                                     "com.example.myApp.myActivity", pdf);


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

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