webview does not download file

android, android-layout

Solution

Set DownloadListener to your WebView. And use Brower app or DownloadManager to download the file.

    webview.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            // handle download, here we use brower to download, also you can try other approach.
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

Problem

Good day all, i have made an app but i have hit a wall. when you visit a website using a desktop browser like firefox or chrome and click on the download link it starts downloading the file, but when i open the same page in my android app that displays the page using webview and click on a download link nothing happens at all, it just sits there like it does not know what to do with that link. if you could help me out it would be a huge help and get me back on track again. this is the download link i use for a pdf file on my site: ``` <img src="images/Art1.jpg" width="80" height="166" /><a href="m2.pdf">Download</a> ``` this is my main.xml file for my android app: ``` <?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> ``` please i will be most gratefull if anyone can help point me on the right path. ``` package com.jeffonlinelibrary; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; public class WebPageLoader extends Activity { final Activity activity = this; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); WebView webView = (WebView) findViewById(R.id.webview); webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle(R.string.app_name); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); webView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { // handle download, here we use brower to download, also you can try other approach. Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } }); webView.loadUrl("http://jeffonlinelibrary.comuv.com/jeffOnlinelibraryApp"); } } ```

Original source