I have tried with downloading with DownloadManager apis of Android but failed to succeed.
Here is my sample code which returns me Download UnSuccessful.
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
String filename = URLUtil.guessFileName(url, contentDisposition, mimetype);
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, filename);
DownloadManager dm = (DownloadManager) MainActivity.this.getSystemService(MainActivity.this.DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
As for me, Your code is working well. Here is my code and no changes happen. Since I wan t to help you, you should post your download server IP if you can. So I can test your server.
My server is apache web server and Since it is one of our production server, forgive me as I cannot express my server. May be the problem is your web server configuration or credentials or security certificates.
WebView webview = new WebView(FileDownloadActivity.this);
webview.loadUrl("http://167.172.70.x/xxxxxxxxx-version-1.9.apk");
webview.setDownloadListener((url, userAgent, contentDisposition, mimeType, contentLength) ->
{
String fileName = URLUtil.guessFileName(url, contentDisposition, mimeType);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager downloadmanager = (DownloadManager) getApplicationContext().getSystemService(FileDownloadActivity.this.DOWNLOAD_SERVICE);
downloadmanager.enqueue(request);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With