Я столкнулся эту ошибку:Попытка открыть PDF-файл для Android
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' не может обслуживать активы, а только локальные файлы. Скопировали ли вы эти файлы из активов в подкаталог 'assets /' под 'getFilesDir()'? – CommonsWare
@CommonsWare У меня есть googled. Вы имеете в виду что-то вроде ответа на следующий вопрос? ссылка: http://stackoverflow.com/questions/8474821/how-to-get-the-android-path-string-to-a-file-on-assets-folder – superkytoz