Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine if Android can handle PDF

Tags:

android

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?

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

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.

like image 623
Jason Shah Avatar asked May 06 '10 22:05

Jason Shah


People also ask

Is PDF compatible with Android?

Open and read PDFs on Android. Download and install Acrobat Reader from the Google Play Store. Launch the app. On the bottom menu bar, select Files. Locate your PDF file on your Android and select it.

Why can't I open PDF files on my Android phone?

To fix a PDF file not opening in Adobe reader, you will need to download the latest version of Adobe Reader. After which you will disable the protected mode that comes with it by default. Once this is changed, the issue of the PDF file not opening in Adobe reader will be resolved.

What PDF viewer does Android use?

1. Adobe Acrobat Reader. For many people, Adobe Acrobat Reader is the default choice PDF reader for Android. You can view, edit, sign, comment, and even export documents from your Android device.

Can all phones read PDF?

No, you need an app that can open pdf files. Some phones come with one, some don't. (Phones [and computers] don't open files, apps do.) If possible, send an email (if it's short enough).


2 Answers

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.

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

If you e.g. install droidreader it will react to the intent and display the 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.

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;     } } 
like image 86
Manfred Moser Avatar answered Oct 02 '22 04:10

Manfred Moser


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/

like image 26
Romain Guy Avatar answered Oct 02 '22 05:10

Romain Guy