android:使用内置的 pdf 查看器从我的应用程序打开 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2916108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:53:34  来源:igfitidea点击:

android: open a pdf from my app using the built in pdf viewer

androidpdfworkflow-activity

提问by mtmurdock

This was my original question:

这是我原来的问题:

I want to be able to open a pdf file in my app using the android's built in pdf viewer app, but i dont know how to start other apps. I'm sure i have to call start activity, i just dont know how to identify the app im opening and how to pass the file to that specific app.

Anyone have a clue?

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

有人有线索吗?

I just learned that the pdf viewer i have on my phone is actually made by HTC and that Adobe just barely released their android pdf viewer (which is great). So the new question is this: how do i verify that the user has installed adobe's viewer, and then how do i open the file in that app from my app?

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

采纳答案by CommonsWare

AFAIK, Adobe has not documented any public Intentsit wants developers to use.

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

You can try an ACTION_VIEWIntentwith a Uripointing to the file (either on the SD card or MODE_WORLD_READABLEin your app-local file store) and a MIME type of "application/pdf".

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

回答by Karakuri

You can programmatically determine whether a suitable application exists on the user's device, without catching exceptions.

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

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
}

回答by SALMAN

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;
    }
}

回答by Karan Mavadhiya

            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);             

            }

回答by david.schreiber

Although this is a pretty old topic, here is a solution for opening a PDF that is in the asset/ folder with an external PDF reader app. It uses a custom content provider: https://github.com/commonsguy/cwac-provider

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

Using this you can define any file to be provided from the assets/ or res/raw/ folder.

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

Try it! Best and easiest solution I found so far.

尝试一下!迄今为止我找到的最好和最简单的解决方案。

回答by paxbat14

I was also faced same issue when was trying to display PDF on android device and finally end up with the solution (3rd party PDF library integration)

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

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

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

while I have tested multiple libraries for this listed below which are also working,

虽然我已经测试了下面列出的多个库,它们也可以正常工作,

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

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

& mupdf which comes with the ndk flavour (https://code.google.com/p/mupdf/downloads/detail?name=mupdf-1.2-source.zip&can=2&q=) and need to extract with NDK and then use it in application as a jar or java etc. nice article to explain the use of this library @ http://dixitpatel.com/integrating-pdf-in-android-application/

& 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/

回答by JosephH

Android has a built in framework from Android 5.0 / Lollipop, it's called PDFRenderer. If can you make the assumption that your users have Android 5.0, it's probably the best solution.

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

There's an official example on Google's developer site:

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

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

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

It doesn't support annotation or other more advanced features; for those your really back to either using an Intent to open a full app, or embedding an SDK like mupdf.

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

(Disclaimer: I very occasionally do work on mupdf.)

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

回答by Rezaul Karim

Add FLAG_GRANT_READ_URI_PERMISSION

添加 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);

also add provider_paths.xml at res -> xml folder and need to add below code at manifests

还要在 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>

回答by Sundara Prabu

In addition to the ones marked as answer you would need these permissions in the manifest.xml

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

**

**