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
Related
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 have html file with following content which will allow an image to download, I have tested it from browser and the download working fine.
<a href="logo.png" id="downlink" style="display:inline-block;" download> download</a>
And in Android Java I have the following code to download
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setAppCacheEnabled(false);
myWebView.clearCache(true);
myWebView.loadUrl("http://192.168.1.3:8075/DB_1/html/LiveMain.html");
myWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
if (url.startsWith("data:")&&url.contains("/")&&url.contains(";")) { //when url is base64 encoded data
String path = createAndSaveFileFromBase64Url(url);
return;
}
Toast.makeText(getApplicationContext(),"Failed to download snap", Toast.LENGTH_LONG).show();
}
});
But when I click the download link nothing happens. The onDownloadStart method not get called. What could be the issue.
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 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'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