I need to download file when user clicks on link in WebView. The action works properly when I do the same action in chrome app, but it doesn't work in webview. I have referred multiple questions and tried implementing solutions provided but it doesn't work.
Stackoverflow questions referred and tried solutions mentioned.
Android Webview not triggering a href download file
How to download files from webview Android?
WebView code that I used,
wv_main.getSettings().setJavaScriptEnabled(true);
wv_main.getSettings().setDomStorageEnabled(true);
wv_main.getSettings().setAllowFileAccess(true);
wv_main.getSettings().setAllowFileAccessFromFileURLs(true);
wv_main.getSettings().setAllowUniversalAccessFromFileURLs(true);
wv_main.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wv_main.getSettings().setBlockNetworkLoads(false);
wv_main.getSettings().setAllowContentAccess(true);
wv_main.getSettings().setPluginState(WebSettings.PluginState.ON);
wv_main.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Download file...");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
});
wv_main.setWebViewClient(new WebViewClient() {
private int webViewPreviousState;
private final int PAGE_STARTED = 0x1;
private final int PAGE_REDIRECTED = 0x2;
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
// Get cert from SslError
SslCertificate sslCertificate = error.getCertificate();
Certificate cert = getX509Certificate(sslCertificate);
if (cert != null && certificate != null) {
try {
// Reference: https://developer.android.com/reference/java/security/cert/Certificate.html#verify(java.security.PublicKey)
cert.verify(certificate.getPublicKey()); // Verify here...
handler.proceed();
} catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException e) {
super.onReceivedSslError(view, handler, error);
e.printStackTrace();
}
} else {
super.onReceivedSslError(view, handler, error);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
webViewPreviousState = PAGE_REDIRECTED;
wv_main.loadUrl(urlNewString);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
webViewPreviousState = PAGE_STARTED;
Log.e("Start Loading", "Start Loading.........");
/*if (dialog == null || !dialog.isShowing())
dialog = ProgressDialog.show(MainActivity.this, "", "Loading", true, true);*/
}
@Override
public void onPageFinished(WebView view, String url) {
if (webViewPreviousState == PAGE_STARTED) {
//dialog.dismiss();
Log.e("Loading Done", "Loading Done.........");
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request,
WebResourceError error) {
super.onReceivedError(view, request, error);
Toast.makeText(SecretLinkBrowserActivity.this, "Error:" + error.toString(), Toast.LENGTH_SHORT).show();
}
});
wv_main.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
request.grant(request.getResources());
}
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d("LogTag", message);
result.confirm();
return true;
}
});
While debugging I noticed that when I click on link the shouldOverrideUrlLoading()
is not being called. The way website is doing the download is they have used doPostBack()
that generated the url dynamically and downloads the file.