31

这是我最初的问题:

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

有人有线索吗?

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

4

9 回答 9

42

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

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
}
于 2012-05-21T16:37:45.523 回答
16

AFAIK,Adobe 没有记录Intents它希望开发人员使用的任何公众。

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

于 2010-05-26T20:52:24.343 回答
5
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;
    }
}
于 2011-10-31T11:27:18.480 回答
3
            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);             

            }
于 2013-04-04T05:42:40.823 回答
2

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

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

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

于 2014-03-03T15:34:42.347 回答
2

当我试图在 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/

于 2015-08-17T10:25:52.130 回答
2

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

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

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

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

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

于 2016-04-11T18:37:09.113 回答
0

添加 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>
于 2018-06-07T08:23:42.973 回答
0

除了标记为答案的那些之外,您还需要 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" />

**

于 2016-12-21T12:38:03.110 回答