How do I determine if Android can handle PDF

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

Leave a Comment