Android WebView Download not working for HTML File - android

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.

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

Unable to download Video file in WebView

When I click the link in WebView then video downloading is started but the format of video is unknown, due to which Android is not playing the downloaded video file.
Also, when I download the video from the same link in chrome then video download in correct format and android is able to play the video file.
WebView webview = (WebView) findViewById(R.id.web1);
webview.setWebViewClient(new WebViewClient());
webview.loadUrl("my url");
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.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Game of thrones ");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
});

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