I have a webview and i want to download files to the sdcard custom folder when user clicking download link from webview.
I'm using the following code for downloading file from webview:
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
strurl = url;
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
It works properly. But it directly downloads to the /sdcard/download/ folder. But i want it to download to some other folder on the sdcard. How can I accomplish this?
Related
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 convert my website into an android webview app. Everything is working fine but when I am trying to download any file from my website the file is downloading in HTML format. But the files were downloading in correct format when I tried to download from a pc or mobile browser.
webView.setDownloadListener(new DownloadListener() {
#Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
DownloadManager.Request myRequest = new DownloadManager.Request(Uri.parse(url));
myRequest.allowScanningByMediaScanner();
myRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager myManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Objects.requireNonNull(myManager).enqueue(myRequest);
Toast.makeText(MainActivity.this,"Download Started....", Toast.LENGTH_SHORT).show();
}
}
I am working with an android application where i need to open a form URL in my webview. I am able to open the form in webview, there is a submit button with form, click on this button will download the file within webview but its not happening. I am able to download the file with normal browser on mobile like google chrome. How can i download the file within my application webview.
i don't know how the you are trying, but you can use this code to download the file inside the webview
mWebView.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.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Name of your downloadble file goes here, example: Mathematics II ");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});
and add this permission to your android manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
hope it will help.!
I'm using webview android.
I want to do basically is to download a file, when the server offers it to me.
Whatever happens, that until just the last post, "in a browser" does not give you the option to save the file.
Therefore, when I try to save the file in android, I never get save.
As you can see, it does not end until the last post, the browser does not have the file. It seems a method asynchronous
webView.setDownloadListener(new DownloadListener()
{
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
{
//for downloading directly through download manager
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
And these code, but can't open in browser because have Get and Post request to download PDF.
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
I can not think of other ways
I have a webview in my app. One of my pages has a link to a mp3 which I should download directly from my app, so using a downloadlistener like this:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
is not good for me, because it launches the default browser before actually donwloading the file.
Is there any way I can manage the download myself, to the OS download folder, so that it appears when user goes to the "Downloads" option in the Android menu?
try to do it with an async task like this
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
new MyDowloadTask().execute(url);
}
});
Where the doInBackground method of your MyDownload task is something like that:
see http://www.androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification
You may do that by not using DownloadListener. You just have to override onPageFinished of WebViewClient. Check content type if it is "application/octet-stream" .. Then handle downloading thru InputStream. Create the file, save then open. :)
You can download it yourself to the downloads directory requesting the file yourself and storing it into Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
Here's some non-functional pseudocode that should help you on your way (but will probably ask you to catch Exceptions and give errors that no network activity may take place on the UI Thread; so you have to embed it in a AsyncTask like Matthew Fisher suggested).
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength)
{
// Define where we want the output file to go
File fileOut = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"Filename.zip"
);
// Request it from the interwebz.
HttpClient android = AndroidHttpClient.newInstance(userAgent);
HttpResponse fileResponse = android.execute(new HttpGet(url));
// Write the response to the file
fileResponse.getEntity().writeTo(new FileOutputStream(fileOut));
}