I am creating an application for pdf download which is working fine with some links. I got a download listener in my code which works fine
DownloadManager.Request 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(mContext.getString(R.string.downloading_file));
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(mContext, "/Dir", URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) mContext.getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
dm.enqueue(request);
but when it comes to downloading e-statement from ICICI bank or some other secured bank, download listener or shouldOverrideUrlLoading never get called.
Related
I have a problem with the download manager in android. I am trying to download a .mp4 video file from a webview but no matter what I did, it always download the file with .bin extenstion. it is not related to the filename, I know because I tried both guessFileName() function and manually entering a name but the result is same. In other browsers, there is no problem downloading the content(.mp4). One issue may be about mimetype, it returns "application/octet-stream" therefore I tried request.setMimeType("video/mp4"); but still I get the mimetype unchanged.
onCreate:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.setMimeType(mimetype);
request.addRequestHeader("User-Agent", userAgent);
request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype));
request.setDescription("Downloading File...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype));
request.allowScanningByMediaScanner();
Log.i("TAG1", url);
Log.i("TAG1", contentDisposition);
Log.i("TAG1", mimetype);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
MyWebViewClient class:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
Logs:
I/TAG1: http://someurl.com/someurl
I/TAG1: attachment; filename="filename.mp4"
I/TAG1: application/octet-stream
One additional note: Downloading happens after redirecting the URL to other URL. But I have tried downloading without redirecting the URL, and still got the .bin file.
Removing
ForceType application/octet-stream
from .htaccess solves the issue
I'm Using Webview setDownloadListener for downloading files, All things are working fine but when I downloaded the files its name gets changes like abc.pdf to hc7sgcgdscbsjajncagv.pdf How to solve this problem. I want exact same name + suffix my_project_name Please help me to do that. Thanks In Advance
My Code
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);
String cookies = CookieManager.getInstance().getCookie(url);
String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
request.addRequestHeader("cookie",cookies);
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);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),"Downloading File...",Toast.LENGTH_LONG).show();
}
});
Image
FYI, from official docs
guessFileName guesses canonical filename that a download would have, using the URL and contentDisposition. File extension, if not defined, is added based on the mimetype
There is no automatic way to set the name for your downloaded file. You need set the name manually for your file like below.
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "abc" + "your_suffix" + ".pdf");
I am trying to download a png image from a website which requires authentication. I am using this code:
mWebView.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);
String cookies = CookieManager.getInstance().getCookie(url);
//request.addRequestHeader("cookie", cookies);
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_PICTURES, URLUtil.guessFileName(
url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}});
It shows that the download was complete but I can't open the image. I tried to download the image from browser and I compared a image sizes. The image downloaded from the webview was 5KB, but the one from the browser (which can be opened) was 100KB.
When I add this line request.addRequestHeader("cookie", cookies); to the DownloadListener the download won't start. At least it won't show as a notafication, however after about 5 minutes a notafication pops up that the download was unsuccessful.
I am trying to download fonts using WebView and DoanloadManager. I set the download folder directory as "Fonts" folder, using Environment.getExternalStorageDirectory() + "/Fonts/" but I is creating a different unwanted directory instead, sdcard/storage/emulated/0/Fonts/. What is wrong with my code? Here is the full snippet:
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
String filename = URLUtil.guessFileName(url, contentDisposition, mimeType);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie", cookies);
request.addRequestHeader("User-Agent", userAgent);
request.setDescription("Downloading file...");
request.setTitle(filename);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() + "/Fonts/", filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
sdcard/storage/emulated/0/ is path that is normally returned by Environment.getExternalStorageDirectory() when you are using an emulator.
Environment.getExternalStorageDirectory() can return any folder and you can't really anticipate the path. Today we have multiple profiles on the same device. Also, this can change based on the android API version.
I load my website in a WebView, and I use Download Manager to download photo file from my website. The file downloaded successfully and I can found the file on Internal Storage/Download.
I store the file to Download Directory using Environment.DIRECTORY_DOWNLOAD, here's my full code for download the file:
webView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long contentLength) {
if(isStoragePermissionGranted()) {
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_DOWNLOAD, URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show();
}
}
});
But when I open my gallery photo, I unable to open the photo. It says "Can't Open" when I tap the photo. I check file details, and the file path refer to /storage/emulated/0/Download.
Any ideas why the image file path in my gallery showing different path? and how to solve this?
There is no constant Environment.DIRECTORY_DOWNLOAD
You should set Environment.DIRECTORY_DOWNLOADS instead.
Also there is more specific option like: Environment.DIRECTORY_PICTURES. It is suitable for image downloads.
Full list is described in documentation: https://developer.android.com/reference/android/os/Environment.html