How to write the code to download file in android studio with the original file name?
I am trying to create a very simple webview mobile android application as I am a beginner in this field. I am almost done but I am stuck at a problem. It is working perfectly with .pdf but it is saving .lrc files with .txt extension.
My code is:
MainActivity.java
webview.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String fileExtenstion, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
MimeTypeMap.getFileExtensionFromUrl(url);
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,fileExtenstion));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, fileExtenstion));
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(),"Downloading File",Toast.LENGTH_SHORT).show();
}
}); ```
**AndroidManiFest.xml**
```
Can you guys help me to make it right please.
Related
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 working on a project where use case is we have to load a web page url in webview which has a button when user clicks on button it will download pdf file. And we have to open that downloaded PDF. currently, i am not finding which path does the file got Downloaded.
You can use the webview download listener and set the path manually using DownloadManager.
An example where the file will be download to /Downloads folder would be:
myWebview.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);
request.allowScanningByMediaScanner();
String fileExtenstion = MimeTypeMap.getFileExtensionFromUrl(url);
String filename = URLUtil.guessFileName(url, contentDisposition, fileExtenstion);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
Also add the below permissions to AndroidManifest.xml .
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
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 have an android application which uses a single WebView which loads a website inside it.
setContentView(R.layout.activity_main);
WebView webView = (WebView) this.findViewById(R.id.webview);
Then i used download manager to Download my files from server
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
//for downloading directly through download manager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
Environment.getExternalStorageDirectory();
getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
when i try to download a file which is created dynamically by the website i get to download the websites HTML instead of the PDF.
webView.loadUrl("http://bookboon.com/");
Thanks in Advance
Actually that HTML page is showing the login page for authentication. So you need a session key which is inside the cookies of that website.
So you need to add three lines in onDownloadListener
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
//for downloading directly through download manager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
//This three lines will do your work
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie("<Your website url>"); // which is "http://bookboon.com"
request.addRequestHeader("Cookie", cookie);
//................................................
request.allowScanningByMediaScanner();
Environment.getExternalStorageDirectory();
getApplicationContext().getFilesDir().getPath(); //which returns the internal app files directory path
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
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