Download file using download link in webView android
I want to download a file using a direct download link in my webview. my links are " https://drive.google.com/uc?id=0Bw6vr2LNxB3iUFJrTk5oZDljaTA&export=download ". if I open this link using my default browser then the browser downloads the file automatically. I don't want to open the default web browser, so I want to use the webview. I tried this code:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "YES", Toast.LENGTH_SHORT).show();
Request request = new Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
I put a toast inside setDownloadListener but no toast appears, which means setDownloadListener doesn't work here? Can anyone tell me how to fix the problem? I have a problem with this issue for a long time but cannot solve it.
+3
source to share
No one has answered this question yet
Check out similar questions: