【问题标题】:android: open a pdf from my app using the built in pdf viewerandroid:使用内置的 pdf 查看器从我的应用程序中打开一个 pdf
【发布时间】:2011-02-24 08:35:59
【问题描述】:

这是我最初的问题:

我希望能够打开一个 pdf 文件 在我的应用程序中使用 android 的内置 pdf查看器应用程序,但我不知道如何 启动其他应用程序。我确定我必须 打电话开始活动,我只是不知道 如何识别正在打开的应用程序和 如何将文件传递给特定的 应用程序。

有人知道吗?

我刚刚得知我手机上的 pdf 查看器实际上是由 HTC 制造的,而 Adob​​e 刚刚发布了他们的 android pdf 查看器(这很棒)。所以新的问题是:如何验证用户是否安装了 adobe 的查看器,然后如何从我的应用程序中打开该应用程序中的文件?

【问题讨论】:

  • AFAIK Android 中没有内置的 PDF 查看器。几天前,Adobe 推出了 Android 的官方 Reader 端口:techie-buzz.com/mobile-news/…,但您仍需要从 Market 获取。
  • 哦,事实证明,我的手机配备了一个 HTC 制作的 pdf 查看器......看中了。好吧,假设用户已经在他们的设备上安装了这个 adobe pdf 查看器,我将如何使用它?
  • 我没有答案,但我想知道您是否对此进行了排序,如果是,您是如何做到的?这与我需要做的事情非常相关。
  • Karakuri 的回答非常适合用任何查看器打开 pdf,而不仅仅是 Adob​​e 的
  • 这怎么可能是重复的?这个问题比那个问题早一年,而且这个问题不是关于 pdf 库,而是关于在其他应用程序中打开 pdf 文件。

标签: android pdf workflow-activity


【解决方案1】:

您可以通过编程方式确定用户设备上是否存在合适的应用程序,而不会捕获异常。

Intent intent = new Intent(Intent.ACTION_VIEW,
        Uri.parse("path-to-document"));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
    startActivity(intent);
} else {
    // Do something else here. Maybe pop up a Dialog or Toast
}

【讨论】:

  • 谢谢你。很有帮助。
【解决方案2】:

据我所知,Adobe 没有记录任何它希望开发人员使用的公开Intents

您可以尝试使用ACTION_VIEW Intent 和指向文件的Uri(在SD 卡上或在应用程序本地文件存储中的MODE_WORLD_READABLE)和"application/pdf" 的MIME 类型。

【讨论】:

  • 好吧,这听起来很酷......我将如何实现它?这对我来说很新鲜。
  • 尝试了这个,因为我不希望将文件存储在 sd 卡上。不幸的是,Adobe Reader 不会从应用程序目录(使用 MODE_WORLD_READABLE 创建)打开文件。到目前为止有人解决了这个问题吗?
  • 它总是说文件不存在或无法读取。我尝试了 8 种不同的 PDF 文件,这些文件可以在我的设备和 PC 中读取。
  • 如果我有网址(http:xyx.com/a.pdf)怎么办?
  • 最好使用 FileProvider,因为 MODE_WORLD_READABLE 在 API 级别 17 中已被弃用
【解决方案3】:
private void loadDocInReader(String doc)
     throws ActivityNotFoundException, Exception {

    try {
                Intent intent = new Intent();

                intent.setPackage("com.adobe.reader");
                intent.setDataAndType(Uri.parse(doc), "application/pdf");

                startActivity(intent);

    } catch (ActivityNotFoundException activityNotFoundException) {
                activityNotFoundException.printStackTrace();

                throw activityNotFoundException;
    } catch (Exception otherException) {
                otherException.printStackTrace();

                throw otherException;
    }
}

【讨论】:

  • 如果 com.adobe.reader 改变路径你就搞砸了,不建议使用 setPackage()。
  • 如果你想使用安卓的默认操作系统阅读器,替换intent.setPackage("com.adobe.reader"); intent.setPackage("com.google.android.apps.pdfviewer");
【解决方案4】:
            FileFinalpath = SdCardpath + "/" + Filepath + Filename;
            File file = new File(FileFinalpath);
            if (file.exists()) {
                Uri filepath = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(filepath, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try {
                    startActivity(intent);
                } catch (Exception e) {
                    alert.showAlertDialog(PDF_Activity.this, "File Not Started...","File Not Started From SdCard ", false);             
                    Log.e("error", "" + e);
                }

            } else {
                alert.showAlertDialog(PDF_Activity.this, "File Not Found...","File Not Found From SdCard ", false);             

            }

【讨论】:

    【解决方案5】:

    虽然这是一个相当老的话题,但这里有一个解决方案,用于使用外部 PDF 阅读器应用程序打开 assets/ 文件夹中的 PDF。它使用自定义内容提供程序:https://github.com/commonsguy/cwac-provider

    使用它,您可以定义要从 assets/ 或 res/raw/ 文件夹中提供的任何文件。

    试试吧!迄今为止我发现的最佳和最简单的解决方案。

    【讨论】:

      【解决方案6】:

      我在尝试在 Android 设备上显示 PDF 并最终得到解决方案(第 3 方 PDF 库集成)时也遇到了同样的问题

      https://github.com/JoanZapata/android-pdfview

      虽然我已经针对下面列出的多个库进行了测试,这些库也可以正常工作,

      https://github.com/jblough/Android-Pdf-Viewer-Library

      & mupdf 自带ndk风格(https://code.google.com/p/mupdf/downloads/detail?name=mupdf-1.2-source.zip&can=2&q=),需要用NDK解压,然后在应用程序中作为jar或java等使用。很好的文章来解释这个库的使用@http://dixitpatel.com/integrating-pdf-in-android-application/

      【讨论】:

        【解决方案7】:

        Android 有一个来自 Android 5.0 / Lollipop 的内置框架,称为PDFRenderer。如果您可以假设您的用户使用的是 Android 5.0,那么这可能是最好的解决方案。

        Google 的开发者网站上有一个官方示例:

        http://developer.android.com/samples/PdfRendererBasic/index.html

        它不支持注释或其他更高级的功能;对于那些您真正回到使用 Intent 打开完整应用程序或嵌入像 mupdf 这样的 SDK 的人。

        (免责声明:我偶尔会在 mupdf 上工作。)

        【讨论】:

        • 哇哦!我对此感到非常兴奋!我仍然必须支持早期版本,而且我早就离开了引发我最初问题的项目。不过,我很高兴知道这一点。
        【解决方案8】:

        除了标记为答案的那些之外,您还需要 manifest.xml 中的这些权限

        **

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        

        **

        【讨论】:

          【解决方案9】:

          添加 FLAG_GRANT_READ_URI_PERMISSION

          Intent intent = new Intent(Intent.ACTION_VIEW)
          Uri outputFileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file); 
          intent.setDataAndType(outputFileUri, "application/pdf"); 
          intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
          intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          Intent in = Intent.createChooser(intent, "Open File");
          startActivity(in);
          

          还在 res -> xml 文件夹中添加 provider_paths.xml 并需要在清单中添加以下代码

          <application>
             <provider
                      android:name="android.support.v4.content.FileProvider"
                      android:authorities="${applicationId}.provider"
                      android:exported="false"
                      android:grantUriPermissions="true"
                      tools:replace="android:authorities">
                      <meta-data
                          tools:replace="android:resource"
                          android:name="android.support.FILE_PROVIDER_PATHS"
                          android:resource="@xml/provider_paths" />
                  </provider>
           </application>
          

          【讨论】:

            猜你喜欢
            • 2012-03-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-26
            • 2020-10-28
            • 1970-01-01
            相关资源
            最近更新 更多