Skip to content

Instantly share code, notes, and snippets.

@opnchaudhary
Created February 15, 2016 13:19
Show Gist options
  • Save opnchaudhary/a02c9326f98ec184bd83 to your computer and use it in GitHub Desktop.
Save opnchaudhary/a02c9326f98ec184bd83 to your computer and use it in GitHub Desktop.
Function to download file in android
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void downloadFile(Context context, String file, String title, String subtitle) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(file));
request.setTitle(title);
request.setDescription(subtitle);
//request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); //to allow download while using WIFI only
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String nameOfFile = URLUtil.guessFileName(file, null, MimeTypeMap.getFileExtensionFromUrl(file));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, nameOfFile);
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment