How to use an Intent to open a PDF file by the standard android application

1

I have an android application, and wanted to know how do I open a PDF file that will be in Firebase in my application. I do not need it to download the file, simply an attempt where I pass the file path (in this case the link) and when clicking the button the default android application open and render the file. What I have been doing now is a webview that opens the pdf link in google docs, but is no longer working and therefore I wanted the pdf to be opened through the standard mobile reader. Below is the Java code for the button that calls the webview:

public void onClick(View v) {
            //Intent intent = new Intent(PdfList.this, WebViewPDF.class);
            //startActivity(intent);
            String pdf = "AQUI ENTRA O LINK DO PDF NO FIREBASE";
            WebViewPDF.OpenPDF.setPdf(pdf);
            Intent intent = new Intent(PdfList.this, WebViewPDF.class);
            startActivity(intent);
        }

And below the webview code that opens the on-screen PDF within the application

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    String pdf = OpenPDF.getPdf();

    WebView mWebView = new WebView(WebViewPDF.this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url=" + pdf);
    setContentView(mWebView);
}
public abstract static class OpenPDF extends WebViewPDF{
    private static String pdf;

    public static void setPdf(String pdfLink){
        pdf = pdfLink;
    }
    public static String getPdf(){
        return pdf;
    }
}
    
asked by anonymous 29.07.2017 / 01:50

1 answer

1

The problem with trying to create a intent is that you may not have any PDF reader installed on your device. Some companies put readers as standard applications, but that does not always happen.

Since the issue of opening the Google Drive reader through WebView, you probably have problems because there is a limit to the display. So a second option would be the pdf.js , open-source project of Mozila , which is perhaps the most viable option because it has no use limit. Just download and copy the project to your assets . Take a look at Viewer options for more options beyond the basics. Here is an example:

String pdf = "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";

webview = (WebView) findViewById(R.id.webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowFileAccessFromFileURLs(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setBuiltInZoomControls(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/pdfjs/web/viewer.html?file=" 
    + pdf  + "#zoom=page-width");

I've shown this same option in this answer .

I tested it and ended up giving an error.

Tofix,justenterthefileviewer.jsandcommentthefollowingcode:

if(fileOrigin!==viewerOrigin){thrownewError('fileorigindoesnotmatchviewer\'s');}

NOTE:Theminimumversiontoworkis API Level: 16 Android 4.1 (JELLY_BEAN) because you must assign true to the methods setAllowFileAccessFromFileURLs() and setAllowUniversalAccessFromFileURLs() , to allow access the contents of a file from the URL and from any source. Internal files have not done any testing yet. As soon as I can, I'll create a complementation here.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    settings.setAllowFileAccessFromFileURLs(true);
    settings.setAllowUniversalAccessFromFileURLs(true);
}
    
29.07.2017 / 22:36