Android Fileprovider:IllegalArgumentException:无法找到包含以下内容的已配置根目录 [英] Android Fileprovider: IllegalArgumentException: Failed to find configured root that contains
问题描述
我对android FileProvider有疑问. 我想保存一个pdf文档,并使用默认程序打开它. 我不想将其保存在外部存储中.
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.
在我成功将pdf保存到FilesDirectory/export/temp.pdf后,
我试图使用FileProvider.getUriForFile()生成URI.
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);
问题:作为第二个参数"Authority",我必须传递什么-我的文件的位置,可以授予URI-Permissions的类或其他内容?无论我尝试过什么,都会导致IllegalArgumentException或NullPointerException. 我的FileProvider(XML):
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>
引用文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="pdfTemplates" path="export/" />
</paths>
推荐答案
我知道了.有两个不同的问题
I´ve got it. There were two different Problems
-
CodeDiving回答了第一个问题. 我必须使用提供者声明中的Authority来进行getUriForFile调用.使用其他类会导致NullPointerException.
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.
我尝试从filesDirectory获取文件,但是在我的file_path中,我仅声明了用于缓存Directory的路径.我将其更改为"files-path",并且有效.此错误导致IllegalArgumentException.
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.
这篇关于Android Fileprovider:IllegalArgumentException:无法找到包含以下内容的已配置根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!