【发布时间】:2017-05-17 05:17:26
【问题描述】:
我正在创建一个 PDF 文件并将其保存在本地存储中。尝试打开它时,它在除 Android N 之外的所有设备中都能完美运行。我可以使用 FileProvider 在 Android N 中打开 PDF 文件,但它显示为空白。
这是我的 URI
content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf
这是我的代码
Uri path;
File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
+ "Android" + "/" + "data" + "/" + "com.products" + "/" + file);
if (Build.VERSION.SDK_INT >= 24) {
path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
} else {
path = Uri.fromFile(pdfFile);
}
// Setting the intent for pdf reader
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(pdfIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
}
【问题讨论】:
-
它可能处于缩放状态。您是否尝试缩小并查看?你能看到页数吗?
-
它没有处于缩放状态。仅文件名显示在顶部,屏幕的其余部分为空白。当通过资源管理器打开同一个文件时,我可以看到数据。
-
我可以知道您使用什么应用程序来查看 PDF 文件吗?以及更多疑难解答问题:) 1. 您是否尝试打开任何其他 PDF 文件? 2. 有没有试过其他的 PDF 应用?
-
尝试使用 Drive PDF Viewer 和 Google PDF Viwer 时,PDF 为空白。尝试使用 Adobe Acrobat Reader 时,我收到消息“无法访问此文件。请检查位置并重试”
-
尝试自己打开 uri 的输入流。并从中读一读。
标签: android android-fileprovider android-pdf-api