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

时间:2010-05-26 19:29:04

标签: android pdf workflow-activity

这是我最初的问题:

  

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

     

任何人都有线索?

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

9 个答案:

答案 0 :(得分:40)

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

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
}

答案 1 :(得分:16)

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

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

答案 2 :(得分: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;
    }
}

答案 3 :(得分: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);             

            }

答案 4 :(得分:2)

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

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

试试吧!到目前为止我找到的最好和最简单的解决方案。

答案 5 :(得分:1)

当我试图在Android设备上显示PDF并最终得到解决方案(第三方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/

答案 6 :(得分:1)

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

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

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

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

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

答案 7 :(得分: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 - &gt;处添加provider_paths.xml。 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>

答案 8 :(得分:-1)

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

**