android - 在 webview 中显示 PDF

标签 android

我想在我的应用程序中显示一个 pdf 文件。我的网络服务给了我 pdf,我想将它下载到 SD 卡,然后我想通过 WebView 在我的应用程序中显示该 pdf。

最佳答案

一段时间以来,我也一直在为 Android PDF 显示问题而苦苦挣扎。我采用了与您提到的将 PDF 下载到 SD 卡时相同的方法,但除了使用可以打开 PDF 的预装 Android 应用程序(例如 Adob​​e Reader 或类似软件)之外,我没有设法打开它。我认为不可能在 WebView 小部件中查看 PDF。原因很简单,就是自带的webview不支持adobe reader之类的插件(见:http://osdir.com/ml/Android-Developers/2010-09/msg03331.html)

您可以使用 Intent 调用轻松打开下载的 PDF:

   File file = new File("/sdcard/filename.pdf");
   Uri path = Uri.fromFile(file);
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setDataAndType(path, "application/pdf");
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
   startActivity(intent);

由于这项技术在我的应用程序发出此调用后会直接打开 PDF 查看器,后退按钮会将用户直接带回应用程序,所以感觉 pdf 查看是我的应用程序的一部分。

希望对你有帮助,祝好

关于android - 在 webview 中显示 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4278422/

相关文章:

android - 在 Android 应用程序中为蓝牙脉搏率传感器创建实时图表

android - 使用 Android Jelly Bean 的 Nexus 7 内存消耗非常高

android - 具有两个等宽子项的 LinearLayout

android - ARCore 兼容设备

android - 我应该在不同的 Activity 中使用不同的 AdMob 广告单元 ID 吗?

android - Corona SDK 允许设备音量控制

android - 带有 https loadUrl 的 webView 显示空白页面

android - Base64 字符串到位图的转换返回 null

android - 为 Unity 3D 5.3 构建 Android 库时出错 - Android Studio

android - 如何以编程方式验证 Play 商店上的应用程序版本?