Download file inside WebView

Jay Mayu picture Jay Mayu · Apr 9, 2012 · Viewed 138k times · Source

I have a webview in my Android Application. When user goes to webview and click a link to download a file nothing happens.

URL = "my url";
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);
mWebView.loadUrl(URL);
Log.v("TheURL", URL);

How to enable download inside a webview? If I disable webview and enable the intent to load the URL on browser from application then download works seamlessly.

String url = "my url";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Can someone help me out here? The page loads without issue but the link to a image file in the HTML page is not working...

Answer

user370305 picture user370305 · Apr 9, 2012

Have you tried?

mWebView.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});

Example Link: Webview File Download - Thanks @c49