Skip to content

Instantly share code, notes, and snippets.

@anilkumar416
Created October 25, 2019 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anilkumar416/a0b6236bffb6c974aed69f63d70f630c to your computer and use it in GitHub Desktop.
Save anilkumar416/a0b6236bffb6c974aed69f63d70f630c to your computer and use it in GitHub Desktop.
//file download after permission check
private void fileDownload(String url, String fileName, String fileExtension) {
fileName = fileName + "." + fileExtension;
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
url = url.replace(" ","%20");
System.out.println("URL=> "+url);
DownloadManager downloadManager = (DownloadManager) ((Activity) baseActivity).getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(fileName)
.setDescription("Downloading "+fileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setMimeType(mimeType)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
downloadManager.enqueue(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment