NavinC
NavinC

Reputation: 243

Invoking Adobe Reader from within my Android application

I am writing an Android application to display pdf files on the device. And I need to use the current versioncode (35498) of the Adobe Reader to display the pdf files.I have with code to display list of files on the screen. Now I need to invoke the Adobe reader (not any other pdf reader installed on the device) onclick of each document. I am not sure how I code that. I am an Android newbie. Any help will be greatly appreciated.

Thanks in Advance, Navin

Upvotes: 7

Views: 11436

Answers (4)

Pier Betos
Pier Betos

Reputation: 1048

This works, setDataAndType method cannot seem to correctly recognize the PDF type if used via URL.

private static Intent newPDFLinkIntent(String url) {
    Uri pdfURL = Uri.parse(url);
    Intent pdfDownloadIntent = new Intent(Intent.ACTION_VIEW, pdfURL);
    pdfDownloadIntent.setType("application/pdf");
    pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    return pdfDownloadIntent ;
}

Unfortunately, the PDF applications I'm using don't anticipate downloading and caching the online content (some will have memory leak error, some will reject link downloading), so you'll eventually end up invoking an intent that downloads the PDF first, before opening the downloaded content via the notification link. I eventually used the solution below:

private static Intent newPDFLinkIntent(String url) {
    Intent pdfDownloadIntent = null;
    try {
        pdfDownloadIntent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
        pdfDownloadIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    } catch (URISyntaxException e) {
        Log.e("PDF Link Tag", e.getMessage());
    }
    return pdfDownloadIntent;
}

Upvotes: 2

Gabriel Klein
Gabriel Klein

Reputation: 544

If you are in "online mode", here is an interesting alternate solution using Google docs.

String myPDFURL = "http://{link of your pdf file}";

String link;
try {
    link = "http://docs.google.com/viewer?url="
    + URLEncoder.encode(myPDFURL, "UTF-8")
    + "&embedded=true";
} catch (Exception e) {
    e.printStackTrace();
}

Uri uri = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

Upvotes: 6

jakebasile
jakebasile

Reputation: 8132

I see that you want to open Adobe specifically, but you may want to consider doing it the more Android-like way of opening a general intent and allowing the user to choose how it opens. For your reference, you'd do that with the following code:

private void openFile(File f, String mimeType)
{
    Intent viewIntent = new Intent();
    viewIntent.setAction(Intent.ACTION_VIEW);
    viewIntent.setDataAndType(Uri.fromFile(file), mimeType);
    // using the packagemanager to query is faster than trying startActivity
    // and catching the activity not found exception, which causes a stack unwind.
    List<ResolveInfo> resolved = getPackageManager().queryIntentActivities(viewIntent, 0);
    if(resolved != null && resolved.size() > 0)
    {
        startActivity(viewIntent);
    }
    else
    {
        // notify the user they can't open it.
    }
}

If you really need to use both Abode Reader specifically, and a specific version, you would need to query for it using PackageManager.getPackageInfo(String, int)

Upvotes: 9

Mudassir
Mudassir

Reputation: 13174

Try the following code

private void loadDocInReader(String doc)
     throws ActivityNotFoundException, Exception {

    try {
                Intent intent = new Intent();

                intent.setPackage("com.adobe.reader");
                intent.setDataAndType(Uri.parse(doc), "application/pdf");

                startActivity(intent);

    } catch (ActivityNotFoundException activityNotFoundException) {
                activityNotFoundException.printStackTrace();

                throw activityNotFoundException;
    } catch (Exception otherException) {
                otherException.printStackTrace();

                throw otherException;
    }
}

Upvotes: 8

Related Questions