Skip to content

Instantly share code, notes, and snippets.

@bharathraj-e
Created January 7, 2020 04:19
Show Gist options
  • Save bharathraj-e/9fbb40d5d070e6a4ced778bc8895a41c to your computer and use it in GitHub Desktop.
Save bharathraj-e/9fbb40d5d070e6a4ced778bc8895a41c to your computer and use it in GitHub Desktop.
webview file download code for android studio
webView.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.setMimeType(mimeType);
//------------------------COOKIE!!------------------------
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
//------------------------COOKIE!!------------------------
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloaded from <ANY NAME>");
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(context.getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment