我想在安卓设备上阅读一个PDF文件。我已经将PDF文件放置在assets文件夹中。
我该如何从那里读取PDF文件?
我查看了上面的链接,但它对我没有用。它给出一个错误,说找不到该Activity。
而且我还想在WebView中打开一个PDF文件。那么是否可能在WebView中阅读PDF呢?
我想在安卓设备上阅读一个PDF文件。我已经将PDF文件放置在assets文件夹中。
我该如何从那里读取PDF文件?
我查看了上面的链接,但它对我没有用。它给出一个错误,说找不到该Activity。
而且我还想在WebView中打开一个PDF文件。那么是否可能在WebView中阅读PDF呢?
您可以下载PDFbox API来在Android上阅读PDF文件,尝试使用此链接:http://pdfbox.apache.org/
您需要一个支持该MIME类型并能打开它的应用程序。在您的设备/模拟器上,该应用程序未安装,因此会出现错误。
另一个很好的解决方案是使用外部安装的应用程序(如Adobe Reader
)来阅读PDF文件。
private void openPDF(final String pathToPDF) {
File file = new File(pathToPDF);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}
private void openPDF(final String path) {
File file = new File(path);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
} else {
uri = Uri.fromFile(file);
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "Application not found", Toast.LENGTH_SHORT).show();
}
}
Google 缓存显示格式如 PDF、PPT、DOCX 等,而且您完全不需要安装任何应用程序。 在 Google 中搜索链接并转到缓存。