18

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,838
  • 1
  • 17
  • 23
user200658
  • 1,547
  • 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

3 Answers3

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,923
  • 6
  • 38
  • 59
Sileria
  • 15,223
  • 4
  • 49
  • 28
  • 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,482
  • 2
  • 28
  • 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
0

Activity.java

 WebView webView = (WebView) findViewById(R.id.webView);
   
        webView.loadUrl(url);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSaveFormData(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.setClickable(true);

  webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressDialog.dismiss();
            }
        });

          webView.setDownloadListener(new DownloadListener() {
                @Override
                public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
    
                    Toast.makeText(MainActivity.this, "Downloading...", Toast.LENGTH_SHORT).show();

                    //All file format .pdf, .jpg, etc.
                    mimeType = MimeTypeMap.getFileExtensionFromUrl(url);

                    File file = new File(url);
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                    request.setMimeType(mimeType);
                    request.addRequestHeader("User-Agent", userAgent);
                    request.setDescription("Downloading...");
                    request.setTitle(Uri.fromFile(file).getLastPathSegment());
                    request.allowScanningByMediaScanner();
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, Uri.fromFile(file).getLastPathSegment());
                    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                    downloadManager.enqueue(request);
    
                }
            });

AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Ashfaque
  • 179
  • 4