I'm trying to download the file from the server using DownloadManager but It fail's to download and say's Download unsuccessful, I have read that there is a issue with Download Manager in the below link android-chrome-browser-unnecessarily-renames-names-types-of-downloaded-files so I tried to download the file by adding the RequestHeader to the DownloadManagerRequest using below method but nothing happened. please find the below code part of my Downloader class anyone please suggest me the right way to download the file from url using DownloadManager.
url:
http://-storage..net/95-****/95-3194.pdf?AWSAccessKeyId=T9YG9HWQC1LHE5G5VF38&Expires=1483443248&Signature=Z%2BkWzfEI2VawbCx%2F2Yto1kPcJKA%3D
Thanks
public void download(Uri uri) {
if (!isDownloading()) {
String fileName=getName(uri);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(fileName);
request.setNotificationVisibility(1);
request.allowScanningByMediaScanner();
request.addRequestHeader("Content-Type", "application/octet-stream");
request.addRequestHeader("Content-Disposition", "attachment; filename=\""+fileName.split("\\.")[0]+"."+fileName.split("\\.")[1].toUpperCase()+"\"");
String cookieContent = getCookieFromAppCookieManager(uri.getHost());
request.addRequestHeader("Cookie", cookieContent);
downloadId = downloadManager.enqueue(request);
register();
}
}
and below block returns the File name
private String getName(Uri uri) {
String name=uri.toString().split("jabord/")[1].split("\\?")[0];
return name;
}
returns the appcookie manager
public String getCookieFromAppCookieManager(String url) {
android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
if (cookieManager == null) {
return null;
}
String rawCookieHeader = null;
// Extract Set-Cookie header value from Android app CookieManager for this URL
rawCookieHeader = cookieManager.getCookie(url);
if (rawCookieHeader == null) {
return null;
}
return rawCookieHeader;
};
Please remove the headers and set the mimetype and then it will work. For example
public void download(Uri uri) {
if (!isDownloading()) {
String fileName=getName(uri);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(fileName);
request.setNotificationVisibility(1);
request.allowScanningByMediaScanner();
request.setMimeType("application/pdf");
Log.e("Extension with ","UpperCase-->"+"\""+fileName.split("\\.")[0]+"."+fileName.split("\\.")[1].toUpperCase()+"\"");
downloadId = downloadManager.enqueue(request);
register();
}
}