Created
January 7, 2020 04:19
-
-
Save bharathraj-e/9fbb40d5d070e6a4ced778bc8895a41c to your computer and use it in GitHub Desktop.
webview file download code for android studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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