【问题标题】:How to read a pdf in android如何在android中阅读pdf
【发布时间】:2011-04-19 08:43:25
【问题描述】:

我想在 android 中阅读 PDF 文件。 我将我的 PDF 文件放在 assets 文件夹中。

如何从那里读取 PDF 文件?

PDF Reader Link

我已经检查了上面的链接,但它对我不起作用。 它给了我一个错误,说找不到活动。

我还想在 WebView 中打开一个 PDF 文件。那么是否可以在 WebView 中阅读 PDF 呢?

【问题讨论】:

  • 为什么它不起作用?你得到什么错误?请粘贴堆栈跟踪和相关的sn-p代码。
  • 我不想从另一个应用程序打开应用程序。我想在 webview 中阅读 pdf 文件。
  • 应该关闭,与编程/开发无关。这个问题属于 android.stackexchange.com
  • 如果它说 Activity not found... 您是否在 AndroidManifest.xml 中列出了您的 Activity?

标签: android


【解决方案1】:

你可以下载 PDFbox API 以在 android 中阅读 PDF 试试这个链接:http://pdfbox.apache.org/

【讨论】:

  • 据我了解,由于引用了 awt 和 swing,PDFBox 不支持 Android 平台。
【解决方案2】:

您需要有一个可以支持该 mimetype 并打开它的应用程序。 在您的设备/模拟器中,该应用程序未安装,因此引发错误

【讨论】:

  • 感谢您的快速回复。但是如果我想通过 WebView 打开一个 PDF 文件,我该如何阅读那个 PDF 文件。
  • 您无法通过 WebView 读取 PDF 文件。自己试试看,打开浏览器,用谷歌搜索pdf。尝试打开它...会提示您下载它并使用另一个应用程序打开它...
  • 是的,你是对的。我发现另一种选择是将 pdf 页面转换为 png 并在 webview 中显示。
【解决方案3】:

另一个使用外部安装的应用程序(如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();
    }
}

【讨论】:

  • 我认为您的代码运行良好。但是你能告诉我那个 mRealPath 是什么吗?我想要 mRealPath 的值。
  • @iDroid mRealPath 是您的 Pdf 文件的路径,“application_type”是应用程序。
  • @Azharahmed:你的设备应该已经安装了“ThinkOffice”。
  • 如果我想为 PDF 阅读器或查看器制作一个单独的应用程序,那么我必须如何使用哪个库......??
  • 您的 sn-p 是否调用外部应用程序?如果是,则不是很好的解决方案。我正在寻找一个真正直接读取 pdf 的 sn-p。
【解决方案4】:

Google 缓存显示格式,如 pdf、ppt、docx 等。您根本不需要安装任何应用程序。 在 google 中搜索链接并转到缓存。

【讨论】:

    【解决方案5】:
     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();
            }
        }
    

    【讨论】:

    • 使用 Android Manifext 中添加的文件提供程序并使用必须添加的 android 读写权限,上述代码工作正常 android:grantUriPermissions="true"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2011-07-07
    相关资源
    最近更新 更多