17

I am accessing a website which has .pdf documents. If I open that document through a web browser, it starts downloading it. If I open it through webview, nothing happens. What setting should I need to apply to webview to make it start downloading?

I already have this.

wvA.setDownloadListener(new DownloadListener()
{
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType,
                    long size)
    {
        Intent viewIntent = new Intent(Intent.ACTION_VIEW);
        viewIntent.setDataAndType(Uri.parse(url), mimeType);

        try
        {
            startActivity(viewIntent);
        }
        catch (ActivityNotFoundException ex)
        {
        }
    }
});
Marty McVry
  • 2,818
  • 1
  • 14
  • 21
user200658
  • 1,497
  • 4
  • 12
  • 11
  • 1
    Take a look [here](http://stackoverflow.com/questions/8485416/how-to-download-a-pdf-from-an-android-webview) – silentw Jan 07 '13 at 17:46
  • Thanks for replying. where should add "view.getContext().startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(url)));" ? – user200658 Jan 07 '13 at 17:53
  • 1
    what is that accent of yours that express itself even in written language ? – njzk2 Jan 07 '13 at 18:09
  • Sorry about the confusion. I amm refering to the solution provided by "silentw". Just asking where should I add that piece of code. And also let me edit my question. – user200658 Jan 07 '13 at 18:13

2 Answers2

27

Implement a download listener to handle any kind of download:

webView.loadUrl(uriPath);

webView.setDownloadListener(new DownloadListener() {
    @Override
    public void onDownloadStart(String url, String userAgent,
            String contentDisposition, String mimetype,
            long contentLength) {

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    }
});
Prabs
  • 4,656
  • 6
  • 37
  • 55
Sileria
  • 14,760
  • 4
  • 47
  • 27
  • when do this setDownloadListener will be called @Mobistry Is there any other way to download the pdf file, [Question](http://stackoverflow.com/questions/30301170/how-to-hide-browser-when-it-is-about-to-be-shown-momentarily-android) – Prabs Jul 31 '15 at 10:16
  • This code is downloading the file but it is opening the web browser and coming back to the app,How to stop this behaviour.what to do if I don't want to get that browser pop-up – Prabs Jul 31 '15 at 10:25
  • 1
    This is the a great answer. It's simple - this way you can let browser handle the downloading. But if you want to do it inside webview, you can also - you have the URL passed. – dorsz Mar 24 '16 at 08:43
  • You may find this example useful for downloading inside the app without starting a new activity and stay inside the application. https://www.mytrendin.com/downloading-file-android-webview/ – Alex Oct 22 '18 at 19:52
19

You should create WebViewClient and set it to your webview. Every time you click a link WebViewClient's shouldOverrideUrlLoading method will be called. Check that url points to pdf file and do what you want. For example, you can view pdf.

webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading (WebView view, String url) {
        if (url.endsWith(".pdf")) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            // if want to download pdf manually create AsyncTask here
            // and download file
            return true;
        }
        return false;
    }
});
Leonidos
  • 10,358
  • 2
  • 27
  • 37
  • 1
    can you help me i had open many html5 pages in webview in between one page downlod pdf file. i had used this code for it but it will not download pdf file – PankajAndroid Oct 03 '13 at 07:28