If you don't want to use a download manager then you can use this code
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);
try {
String address = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
+ Environment.DIRECTORY_DOWNLOADS + "/" +
fileName;
File file = new File(address);
boolean a = file.createNewFile();
URL link = new URL(url);
downloadFile(link, address);
} catch (Exception e) {
e.printStackTrace();
}
}
});
public void downloadFile(URL url, String outputFileName) throws IOException {
try (InputStream in = url.openStream();
ReadableByteChannel rbc = Channels.newChannel(in);
FileOutputStream fos = new FileOutputStream(outputFileName)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
// do your work here
}
This will download files in the downloads folder in phone storage. You can use threads if you want to download that in the background (use thread.alive() and timer class to know the download is complete or not). This is useful when we download small files, as you can do the next task just after the download.