【发布时间】:2019-11-13 19:50:59
【问题描述】:
我尝试使用嵌入式谷歌文档打开 PDF,如下所示:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://docs.google.com/gview?embedded=true&url=https://aip.xyz.org/Accident.pdf"));
startActivity(browserIntent);
这可行,但不支持/打开大尺寸 pdf。然后从几个堆栈溢出建议中,我尝试使用如下意图选择器:
private void openPDF(int position) {
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(Uri.parse("https://aip.xyz.org/Accident.pdf"), "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(pdfIntent, "Open PDF using");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
Toast.makeText(context, "No Applications found to open pdf",Toast.LENGTH_SHORT).show();
}
}
但这也会引发错误“无法显示 pdf(无法打开 Pdf)”。我不明白这有什么问题。
【问题讨论】:
-
Android 没有嵌入式 PDF 查看 API。您可以搜索 PDF 库,或者我们的外部应用程序。