If your website has a download option you really need to enable it from your Android WebView app. The Android java code below can be used to download any type of file to your Android device’s storage with the file’s original name, be it a .mp3, mp4, jpg, or pdf, the code will download any file that is downloadable. This code is for Android WebView, just paste it below in the oncreate method of WebView activity.
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.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();
}
});
And also we must request user permission for devices. In the next article, We will tell you How to request user permission for devices in Android Studio
This is what i want in webview or html app....
I have a similar problem to yours. The issue here is how WebView handles attachments (a real pain in the ass). When the WebView visits a web page that at some point returns an attachment, it says kinda like "Oh crap, what can I do with this non-HTML thingy?... Ey you! DownloadListener! do something with this URL that is saying some nonsense about an attachment". So, the DownloadListener takes over and here is the problem: it requests again the same URL to download the attachment, so, for downloading an attachment when visiting a page, the WebView perfoms 2 requests: the page itself and then another one to download the attachment, instead of just downloading it.
The second request performed by the DownloadListener will make another request to abc.com/xyz.php but this time it won't contain cookies or session information so it won't enter the "download" logic.
A possible solution would be to redirect to a temporal copy or the real path to the file that doesn't contains any logic so there is no problem"