我如何确定的Andr​​oid可以处理PDF [英] How do I determine if Android can handle PDF

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

问题描述

我知道Android无法处理PDF文件本身。然而,Nexus的一个(以及可能的其它电话)来$ P $对安装用的QuickOffice查看器。我将如何判断用户是否安装了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?

目前,在code键启动PDF下载看起来pretty的简单:

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 Market中的应用程序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的魔术测试这一点。如果没有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;
    }
}

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

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