How to use an Intent to open a PDF file by default android app

Asked

Viewed 1,909 times

1

I have an android app, and wanted to know how to open a PDF file that will be on Firebase in my application. I do not need him to download the file, simply an Intent where I pass the file path (in case the link) and when clicking the button the default android application open and render the file. What I have currently done is a webview that opens the pdf link by google Docs, however is not working anymore And that’s why I wanted the PDF to be opened through the standard cell phone reader. Below the Java code of 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 PDF on the screen 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;
    }
}
  • But that depends if the device has installed some PDF reader; If by chance you don’t have it, then it won’t work. But you can make a Webview call to view it. What have you already done?! You can insert it into the question?!

  • @acklay updated the question with the current webview code using.

  • I believe that there is no standard reader on Android. Generally if you do not have any app that reads pdf, the file will not be recognized. Look at that answer, in the second option. https://answall.com/a/218776/35406 It will probably help you.

1 answer

1


The problem of trying to create a intent is that there might not be any PDF reader installed on the device. Some companies put readers as standard apps, but this doesn’t always happen.

Already the issue of opening the Google Drive reader by Webview, you probably have faced problems because there is limit of visualization. So a second option would be pdf.js, open-source project mozila’s, which is perhaps the most viable option as it has no use limit. Just download and copy the project to your assets. Take a read on Viewer options to see more options beyond the basics. See below for 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 showed that same option in that reply.

I tested and ended up giving a mistake.

inserir a descrição da imagem aqui

To fix, just enter the file viewer.js and comment on the following code:

if (fileOrigin !== viewerOrigin) {
    throw new Error('file origin does not match viewer\'s');
}

OBS.: The minimum version for operation is API Level: 16 Android 4.1 (JELLY_BEAN), because it is necessary to allocate true for the methods setAllowFileAccessFromFileURLs() and setAllowUniversalAccessFromFileURLs(), to allow you to access the contents of a file from the URL and from any source. Internal files I haven’t run any tests yet. As soon as I can do it, I’ll create a compliment here.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    settings.setAllowFileAccessFromFileURLs(true);
    settings.setAllowUniversalAccessFromFileURLs(true);
}
  • I tried to use his code, but he could not open the PDF, only the following error message appeared: https://drive.google.com/file/d/0B6_1un0ahgDUTTdwekZnX1BuX1E/view?usp=sharing

  • @I edited Tulips. I found the error and the solution. Good luck!

  • 1

    It worked now, thank you!

  • What is the minimum API that can be used pdf.js considering that the example code posted above is using Webchromeclient?

  • 1

    @Henqsan edited the question. I did not test with internal but external pdf files only works with Jellybean.

  • 1

    @acklay thank you!

  • @acklay to open the local pdf file just inform the way String pdf = "file:///android_asset/arquivo.pdf";. A question, there is the possibility of an interaction between javascript and webview so that it is possible for example to capture the page number of the pdf file?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.