如何确定 Android 是否可以处理 PDF [英] How do I determine if Android can handle PDF

查看:23
本文介绍了如何确定 Android 是否可以处理 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Android 本身无法处理 PDF.但是,Nexus One(可能还有其他手机)预装了 QuickOffice Viewer.我如何确定用户是否安装了 PDF 查看器?

I know Android cannot handle PDFs natively. However, the Nexus One (and possibly other phones) come pre-installed with QuickOffice Viewer. How would I determine whether the user has a PDF viewer installed?

目前,开始下载 PDF 的代码看起来很简单:

Currently, the code to start the PDF download looks pretty simple:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);

下载后,用户点击下载的文件来调用查看器.但是,如果没有PDF查看器,Android会报告无法下载.手机不支持该内容."我想确定用户是否会收到此消息,如果是,请将他们定向到 Android 电子市场中的 PDF 应用.

After the download, the user clicks on the downloaded file to invoke the viewer. However, if there is no PDF viewer, Android reports "Cannot download. The content is not supported on the phone." I want to determine if the user will get this message, and if so, direct them to PDF apps in the Android Market.

推荐答案

我一直在对此进行测试,发现以下方法有效.首先,您独立下载文件并将其存储在设备上,然后执行以下操作:

I have been testing this and found that the following works. First you download the file independently and store it on the device and then you go do this:

 File file = new File("/sdcard/download/somepdf.pdf");

 PackageManager packageManager = getPackageManager();
 Intent testIntent = new Intent(Intent.ACTION_VIEW);
 testIntent.setType("application/pdf");
 List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
 if (list.size() > 0 && file.isFile()) {
     Intent intent = new Intent();
     intent.setAction(Intent.ACTION_VIEW);
     Uri uri = Uri.fromFile(file);
     intent.setDataAndType(uri, "application/pdf");

     startActivity(intent);

我已经在各种模拟器和有根的氰基手机以及 HTC Magic 上对此进行了测试.如果没有可用的 pdf 渲染器,则列表将返回零并且什么也不会发生.

I have tested this on various emulator and a rooted cyanogen phone as well as a HTC Magic. If no pdf renderer is available the list will return zero and nothing will happen.

将数据类型设置为 pdf mime 类型以获得正确的行为似乎很重要.

It seems to be important to set the data type to the pdf mime type to get the correct behaviour.

如果你例如安装 droidreader 它将对意图做出反应并显示 pdf.

If you e.g. install droidreader it will react to the intent and display the pdf.

当然,您也可以在下载 pdf 之前进行检查,具体取决于您的用例,或者执行诸如弹出警报或重定向执行其他下载意图等操作.

Of course you could do the check before you download the pdf as well depending on your use case or do things like popping up alerts or redirecting do other intents for download or whatever.

我已经将其重构为一个单独的方法..

I have since refactored this out into a separate method ..

    public static final String MIME_TYPE_PDF = "application/pdf";

/**
 * Check if the supplied context can render PDF files via some installed application that reacts to a intent
 * with the pdf mime type and viewing action.
 *
 * @param context
 * @return
 */
public static boolean canDisplayPdf(Context context) {
    PackageManager packageManager = context.getPackageManager();
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType(MIME_TYPE_PDF);
    if (packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0) {
        return true;
    } else {
        return false;
    }
}

这篇关于如何确定 Android 是否可以处理 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆