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.
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");
Suppose I'm using a URL in webview http://demo.io and this URL has a button of camera and when I click on that camera button then my URL became http://demo.io/captureImage. Then I have an issue when I check the url of webview then it shows only http://demo.io not showing http://demo.io/captureImage so how I can get this URL in Android. Because this URL has an image and I wanna save this image. When this URL (http://demo.io/captureImage) runs on chrome and I capture the image then captured image downloaded but in Android webview image after capturing not downloading. Please suggest me appropriate answer or code
You can add a download listner to webivew and download the image from the url as shown below
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("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();
}
If I get your issue correctly,
First you have to reset WebView with new url. Try below to do this,
cameraButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
UrActivity.this.mWebView.loadUrl("http://demo.io/captureImage");
}});
Second is to download image you can use setDownloadListener(...) as suggested by #Hasif Seyd
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.
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