Created
February 15, 2016 13:19
-
-
Save opnchaudhary/a02c9326f98ec184bd83 to your computer and use it in GitHub Desktop.
Function to download file in android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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