如何确定 Android 是否可以处理 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2784847/
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:21:11  来源:igfitidea点击:

How do I determine if Android can handle PDF

androidpdf

提问by Jason Shah

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?

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

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

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

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

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.

下载后,用户单击下载的文件以调用查看器。但是,如果没有PDF查看器,Android会报告“无法下载。手机不支持该内容。” 我想确定用户是否会收到此消息,如果是,请将他们定向到 Android Market 中的 PDF 应用程序。

回答by Manfred Moser

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

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.

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

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

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

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

例如,如果您安装 droidreader,它将对意图做出反应并显示 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.

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

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

回答by Romain Guy

You can query the PackageManager to see if there's a package that can handle your Intent. Here's an example: http://www.curious-creature.org/2008/12/15/android-can-i-use-this-intent/

您可以查询 PackageManager 以查看是否有可以处理您的 Intent 的包。这是一个例子:http: //www.curious-creature.org/2008/12/15/android-can-i-use-this-intent/

回答by Deepa MG

You will probably use asynch task to download any file.so simple way is Just add below code in post execute() method of asynch task.it will ask for choice.

您可能会使用异步任务来下载任何文件。所以简单的方法是在异步任务的 post execute() 方法中添加以下代码。它会要求选择。

FileOpener.open(context, file);

and file should contain info about filepath and filename.Example

和文件应包含有关文件路径和文件名的信息。示例

File file = new File(Environment
        .getExternalStoragePublicDirectory("/MDroid")
        + filepath + fileName );

Hope it helps

希望能帮助到你