Invoking Adobe Reader from within my Android application

限于喜欢 提交于 2019-11-29 02:09:11

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

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)

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

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!