Is there a way to get the stock Android browser to auto-open a PDF, Word or other typical file without having to go through the process of downloading the file and then getting the user to open the file from the Downloads app or the Notification bar?
We have a web application that has a lot of documents that we'd like to include and not have to convert to HTML, but making the user download the file and manually open it is not easy to train users on.
On iOS, these files all display inline in the browser. I'd like a way to get the browser to auto-launch the files into Acrobat Reader or QuickOffice or whatever program the user has to display them.
Does anyone know a way to do that? I know that Google Docs has some PDF viewing support, but people using our web app may not have public Internet access in all cases, and may be hitting on a local web server.
Try this, worked for me.
WebView view = (WebView) findViewById(R.id.yourWebView);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setPluginState(WebSettings.PluginState.ON);
view.loadUrl("http://docs.google.com/gview?embedded=true&url="
+"your document link(pdf,doc,docx...etc)");
String format = "https://drive.google.com/viewerng/viewer?embedded=true&url=%s";
String fullPath = String.format(Locale.ENGLISH, format, "PDF_URL_HERE");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fullPath));
startActivity(browserIntent);
Unfortunately the native browser present on Android devices not support this type of file. Let's see if in the 4.0 we will be able to do that.
Specifically, to install the pdf.js plugin for firefox, you do not use the app store. Instead, go to addons.mozilla.org from inside mozilla and install it from there. Also, to see if it's installed properly, go to the menu Tools:Add-ons (not the "about:plugins" url as you might think from the desktop version).
(New account, otherwise I'd put this as a comment on the answer above)
I needed this too, and the links above stopped working so this is what I found to work with the New Google Drive:
Google has a service that creates the link for PDF's Not in GDrive: https://docs.google.com/viewer Just add your URL and it creates a link, and IFrame code (Look closely and you will see the pattern and create links without this web service)
Also, there is a way to do it for PDF's stored in Google Drive: https://docs.google.com/viewer?srcid=YOUR_GDRIVE_PDF_DOC_ID_HERE&pid=explorer&efh=false&a=v&chrome=false&embedded=true (this can be a link or the src URL of an iframe)
I've tested on Android and it brings up the PDF viewer nicely.
You can use this
webView.loadUrl("https://docs.google.com/viewer?url=" + "url of pdf file");
You can use this format as of 4/6/2017.
https://docs.google.com/viewerng/viewer?url=http://yourfile.pdf
Just replace http://yourfile.pdf with the link you use.
Use the snippet of code below
File file= new File("/sdcard/test.pdf");
Uri path=Uri.fromFile(file);
Intent i =new Intent(Intent.ACTION_VIEW);
i.setDataAndType(path,"application/pdf");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
You can open PDF in Google Docs Viewer by appending URL to:
http://docs.google.com/gview?embedded=true&url=<url of a supported doc>
This would open PDF in default browser or a WebView.
A list of supported formats is given here.
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openURL("http://docs.google.com/viewer?url=" + " your pdf link ");
}
});
}
private void openURL(String s) {
Uri uri = Uri.parse(s);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"text/html");
startActivity(intent);
}
}
Source: Stackoverflow.com