android - 打开 PDF Nougat+(文件目录)
问题描述
我无法在 Ver. 上打开 PDF。Nougat+,以下代码后出错。
打开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: Failed to find configured root that contains /storage/emulated/0/****/****/****/* **.pdf
当我使用“file1”路径(注释中的路径)时,它不会崩溃,但它给了我错误的目录/路径。位于 0/Android/data/***/etc。
也无法将 getUriForFile 路径更改为:保存我的 PDF 的 Environment.getExternalStorageDirectory().toString()。会给我第一个错误。
TIA
解决方案
<files-path
是为了/data/data/<packagename>/files/.....
对于/storage/emulated/0/.....
更改为
<external-path
推荐阅读
- python - 在 QThread 中创建 GStreamer 管道时未收到消息
- c# - 使用 Selenium javascript 错误选择下拉列表:a.tagName.toUpperCase 不是函数
- java - ListView 中 RadioGroups 中的其他按钮被选中
- javascript - 是否可以在视频标签 HTML 上附加 data:video
- javascript - 当我在“onbeforeunload”事件中进行异步 XMLHttpRequest 调用时,幕后会发生什么?
- c# - 无法将视觉隐藏的元素添加到 IList(Selenium,C#)
- authentication - 无法让 kubectl 对 dex 和 gangway 进行身份验证
- android - 材质编辑文本
- c++ - 特征垂直堆叠行成矩阵
- reactjs - ReactJS 使用 jest spyOn 测试辅助方法