【发布时间】:2016-01-31 23:50:33
【问题描述】:
我遇到了这个错误:
E/AndroidRuntime: java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.mobgen.androidinterviewtest/files/LaFerrari.pdf
E/AndroidRuntime: at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
E/AndroidRuntime: at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
E/AndroidRuntime: at com.mobgen.interview.SingleCarActivity$1.onClick(SingleCarActivity.java:92)
是因为这行代码:
Uri uri = FileProvider.getUriForFile(SingleCarActivity.this, "be.myapplication", file);
我已经关注了我找到的资源: http://developer.android.com/reference/android/support/v4/content/FileProvider.html
Open file in cache directory with ACTION_VIEW
How to use support FileProvider for sharing content to other apps?
但是我仍然无法解决我遇到的错误。
下面你可以看到我的代码:
SingleCarActivity.java:
File file = new File(getFilesDir(), pdf); //pdf contains "LaFerrari.pdf"
Uri uri = FileProvider.getUriForFile(SingleCarActivity.this, "be.myapplication", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="be.myapplication"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_pdf" path="assets/"/>
</paths>
您可以在下面看到我的项目结构的屏幕截图: 链接:http://i.imgur.com/4dbhcKF.png
【问题讨论】:
-
assets/是你的开发机器上的一个目录。它不是设备上的目录。FileProvider不能提供资产,只能提供本地文件。您是否已将这些文件从资产复制到getFilesDir()下的assets/子目录? -
@CommonsWare 我已经用谷歌搜索过了。您的意思是类似于以下问题的答案吗?链接:stackoverflow.com/questions/8474821/…
标签: java android xml android-fileprovider