Открыть PDF Nougat+ (Каталог файлов)
Я не могу открыть PDF-файлы на Ver. Нуга +, ошибка после кодов ниже.
Любой другой способ или лучший способ открыть PDF, или как изменить путь к внешнему каталогу.
Коды ниже:
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
Uri path = Uri.fromFile(files.get(position));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setDataAndType(path, "application/pdf");
} else {
Uri uri = Uri.parse(String.valueOf(files.get(position)));
File file = new File(uri.getPath());
if (file.exists()) {
//File file1 = new File(getActivity().getFilesDir() + File.separator + "PDF" + File.separator + "eFSIS" + File.separator + "InspectionOrder.pdf");
Toast.makeText(mainActivity, file.toString(), Toast.LENGTH_SHORT).show();
uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
intent = Intent.createChooser(intent, "Open With");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
getActivity().startActivity(intent);
} catch (ActivityNotFoundException e) { }
Провайдер: (Манифест)
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
XML:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal_files" path="."/>
</paths>
Ошибка: эта ошибка возникает при использовании пути к файлу, java.lang.IllegalArgumentException: не удалось найти настроенный корень, который содержит / storage / emulated / 0 / **** / **** / **** / * **. PDF
и когда я использую путь 'file1' (тот, что в комментарии), он не падает, но дает неправильный каталог / путь. который находится в 0 / Android / данные / *** / и т. д.
Также нельзя изменить путь getUriForFile на: Environment.getExternalStorageDirectory (). ToString (), где мои PDF-файлы сохранены. Даст мне первую ошибку.
ТИА
1 ответ
<files-path
для /data/data/<packagename>/files/.....
За /storage/emulated/0/.....
изменить на
<external-path