Search code examples
androidpdf-viewer

How to download and Show a Pdf File in androd app with web view


I am Developing a android application where I am showing a pdf file in web view. code is as follows

pDialog = new ProgressDialog(Units.this);
    pDialog.setTitle("Please Wait");
    pDialog.setMessage("Loading...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    webview = (WebView) findViewById(R.id.webView1);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            pDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            pDialog.dismiss();
        }
    });
    String pdf = "http://XX.xxx.xx.xxx/webservice/testbook.pdf";
    webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdf);
}

It is working fine. But it download pdf each time when ever it is called. I want it should download only once in cache or temp memory(Not in External directory) and viewable in app until it is removed.


Solution

  • WebView is basically used for loading webpages in your app. For your question I think WebView is not an option. You can follow these steps

    1. Download the pdf file. Like a normal file.(Not using WebView)
    2. Save to storage.
    3. Display to the UI

    Next time when app loads, display if file exists, else download again.

    Also check for PDFRenderer.