Downloading to download folder in Android - android

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));
}

Related

Android webview downloading pdf, xlsm as html file

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();
}
}

Downloading file with JSF on Android's WebView

I have a Java Web Application that uses JSF and PrimeFaces. On a backing bean, I download a local pdf or document file:
public void download() throws FileNotFoundException, IOException {
File file = new File("C:/file.pdf");
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response =
(HttpServletResponse) facesContext.getExternalContext().getResponse();
response.reset();
response.setHeader("Content-Type", "application/pdf");
OutputStream responseOutputStream = response.getOutputStream();
InputStream fileInputStream = new FileInputStream(file);
byte[] bytesBuffer = new byte[2048];
int bytesRead;
while ((bytesRead = fileInputStream.read(bytesBuffer)) > 0)
{
responseOutputStream.write(bytesBuffer, 0, bytesRead);
}
responseOutputStream.flush();
fileInputStream.close();
responseOutputStream.close();
facesContext.responseComplete();
}
This is how I call the download method on the xhtml:
<p:commandButton action="#{mybean.download()}" value="Download File" ajax="false" >
And I created a simple Android application for my Java application just by calling it's url inside a WebView:
mWebView.loadUrl("http://myappurl.com");
Then I listen for downloads on the WebView using the DownloadListener event:
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);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, url);
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
Toast.makeText(getApplicationContext(), "Downloading File",
Toast.LENGTH_LONG).show();
}
});
The problem is, the DownloadListener event for WebView does not get called when I press the download button. It works fine by accessing the application URL from the phone's browser, but not in the android application that I created.
It also works on the android application if I put the pdf inside the application directory and call it using a simple <a href="/file.pdf" download>, but that is not what I want, because the pdf files will be on the server's directory and not in the application.
What I've tried:
Using PrimeFaces p:fileDownload - got the same result, nothing happens when button is pressed;
I looked into google docs online viewer, but I cant use that because my files are local;
I tried PDF.js, but this same download button will be used for all kinds of documents, not only pdf, so I don't really need to view the file, I just need to download and save it on the device.
Please help me. Thanks in advance.
EDIT
I tried Chrome Custom Tabs that replaces the Android WebView, it works great, my JSF download was recognized and the file was downloaded, I didn't have to do anything. The only problem is that there is no way of removing the toolbar from Chrome Custom Tabs, because I want my application to seem like it's a native application and not a webpage.

webview, download file on finish prepared document

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

WebView & DownloadManager

I'm makin' an app, which job is to load one specific website. Everything works fine except downloading files.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adviser = (WebView) findViewById(R.id.webView);
getActionBar().hide();
adviser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
adviser.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
customizeurl(contentDisposition));
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "downloading",
Toast.LENGTH_LONG).show();
}
});
return true;
}
});
adviser.getSettings().setJavaScriptEnabled(true);
adviser.getSettings().setSupportZoom(true);
adviser.getSettings().setAllowFileAccess(true);
adviser.getSettings().setAllowUniversalAccessFromFileURLs(true);
adviser.getSettings().setUseWideViewPort(true);
adviser.getSettings().setLoadWithOverviewMode(true);
adviser.setVerticalScrollBarEnabled(true);
adviser.setHorizontalScrollBarEnabled(false);
adviser.getSettings().setAppCacheEnabled(true);
adviser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
adviser.getSettings().setSupportMultipleWindows(true);
adviser.getSettings().setUserAgentString("Mozilla");
adviser.loadUrl("http://localhost/www/site/login");
}
url: localhost/www/site/download?action=newaction&dl=folder\subfolder\subsubfolder\1449822583856.jpg
uri: localhost/www/site/download?action=newaction&dl=folder\subfolder\subsubfolder\1449822583856.jpg
userAgent: Mozilla //whatever i use as agent it wont work
contentDisposition: attachment;filename=1449822583856.jpg
mimetype: image/jpeg
contentLength: -1
It downloads my homesite as phpfile, not my jpg.
Any ideas?
Thanks for helping :)
Edit: It might be problem with Content-Length? Or maybe some session problems?
It is obvious that it will download a php file because the url you provided does not contain the direct address of file
URL = localhost/www/site/download?action=newaction&dl=folder\subfolder\subsubfolder\1449822583856.jpg
so entering it in browser displays image but the address is not belongs to any file directly try to get that img element from webview that is being displayed in Webview and get its src attribute value pass that to DownloadManager and you will get your image downloaded

How to change default download location in android

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?

Categories

Resources