Invoking Adobe Reader from within my Android application

孤人 提交于 2020-01-09 22:07:39

问题


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


回答1:


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



回答2:


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)




回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/5113435/invoking-adobe-reader-from-within-my-android-application

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