Android DownloadManager example
This method will download the file from the url and save it with the filename of outputFileName. When the download is completed, there will be a notification show up in the status bar.
public void downloadByDownloadManager(String url, String outputFileName) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("A zip package with some files");
request.setTitle("Zip package");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, outputFileName);
Log.d("MainActivity: ", "download folder>>>>" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
Make sure the INTERNET and WRITE_EXTERNAL_STORAGE permissions are granted in the manifest file.
Sample usage of the above method using the DownloadManager.
downloadByDownloadManager("https://github.com/username/projectname/archive/hello.zip", "hello.zip");
Official Documentation for the DownloadManager.
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts