What intent would open a pdf from a url? [duplicat

2019-02-01 00:47发布

问题:

This question already has an answer here:

  • How to render PDF in Android 6 answers

I am trying to create an intent that would open a pdf file on the web (a url) in a pdf reader. It seems to only work if the file is local.

I know this is going to depend on whether or not any of the installed apps can handle urls, but since I have several pdf readers installed (some of them claim they can read pdfs on the web) and none of them are responding I wanted to see if there was something wrong with my intent.

Here is what I am currently using:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");

Thanks

回答1:

Build a url to the pdf using google docs viewer. This ends up loading much faster as they don't have to download the file in its entirety before you can display it.

private static final String googleDocsUrl = "http://docs.google.com/viewer?url=";

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(googleDocsUrl + url), "text/html");

We recently did something similar except we used our own activity with an embedded webview to display the pdf rather than going to the browser.



回答2:

You can simply load the following URL in the Webview

webView.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + url);

Google docs will take care to load the PDF URL.