我正面临这个错误:
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
答案 0 :(得分:1)
FileProvider
无法直接投放资源,因为资产不是文件。您的主要选择是:
将资产复制到您为FileProvider
配置的位置的文件。我在this sample app中演示了这种FileProvider
配置,但the question from your comment的答案执行了将资产复制到文件的基础知识。
使用my StreamProvider
,可以直接提供资源,而无需复制。