Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Viewing a PDF file in Android

Tags:

android

pdf

I am developing an application where one of the buttons displays a PDFfile when pressed.

What is the best way to display this PDF file?

I am able to view it by copying it to the SD card and launching it via intent using adobe that is already installed on the device.

However I can't assume that every device has adobe installed so what is my alternative option?

Would I be able to convert it into an image and display it this way?

like image 968
DMC Avatar asked Nov 13 '22 16:11

DMC


1 Answers

I doubt you want to do the pdf conversion yourself. You can check to see if there is an application to handle the intent like this:

/**
* Indicates whether the specified action can be used as an intent. This
* method queries the package manager for installed packages that can
* respond to an intent with the specified action. If no suitable package is
* found, this method returns false.
*
* @param context The application's environment.
* @param action The Intent action to check for availability.
*
* @return True if an Intent with the specified action can be sent and
*         responded to, false otherwise.
*/
public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

Taken from the android developers blog here.

If they don't have an application to handle pdfs then show a dialog box that offers to send them to the Play Store. You can create intents to launch the play store like this:

try {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details? id="+appName)));
} catch (android.content.ActivityNotFoundException anfe) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="+appName)));
}

Taken from this stackoverflow link.

like image 116
Sam Burba Avatar answered Nov 15 '22 05:11

Sam Burba