I want to be able to download a file through Android's webview but I cannot do it.
The url I have to download from is in this format:
http://{server_path}/Download?mimetype=application/octet-stream
I am using this code:
myWebView.setDownloadListener(new DownloadListener() {
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("Downloading file...");
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) getSystemService(DOWNLOAD_SERVICE);
long donwloadedFileId = dm.enqueue(request);
String downloadedMimeType = dm.getMimeTypeForDownloadedFile(donwloadedFileId)
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
});
When I try to download all I get is a "download.bin" file.
As I check the function URLUtil.guessFileName(url, contentDisposition, mimetype)
returns this as a file name.
Could you help me?
EDIT
If I rename the downloaded file from Android File manager app, it opens succefully. So maybe the answer is how can I set the correctly mimeType of the downloaded file?
EDIT 2 (13/4/20)
Also I forgot to mention that in the above code mimeType is "application/octet-stream"