How to determine if Android supports PDF?

I know that Android cannot process PDF files natively. However, the Nexus One (and possibly other phones) comes preloaded with QuickOffice Viewer. How to determine if a user is installed in a PDF viewer?

Currently, the code for starting a PDF download looks pretty simple:

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

After downloading, the user clicks on the downloaded file to invoke the viewer. However, if there is no viewing of PDF files, the Android reports "Unable to load. Content is not supported on the phone." I want to determine if the user will receive this message, and if so, forward them to PDF applications in the Android Market.

+28
android pdf
May 6 '10 at
source share
3 answers

I tested this and found that the following works. First you download the file yourself and save it on the device, and then you 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 tested it on different emulators and on a cyanogenic root phone, as well as on HTC Magic. If PDF rendering is not available, the list will return zero and nothing will happen.

It seems important to set the data type to the PDF mime type in order to get the correct behavior.

If you, for example, install droidreader, it will respond to the intention and display pdf.

Of course, you can check before downloading the PDF, and depending on your use case, either do something like pop-up warnings or redirect other intentions to download or something else.

Edit: I have since edited this in 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; } } 
+52
May 7, '10 at 17:21
source share

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

+10
May 07 '10 at a.m.
source share

You are likely to use the asynch task to download any file. Easy way: just add the below code to the post execute () method of asynch task.it, which will ask for a choice.

 FileOpener.open(context, file); 

and the file should contain information about the file path and the file name .Example

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

Hope this helps

0
Nov 03 '16 at 7:33
source share



All Articles