android download file in download folder and get path

Android - How to download file in download folder and  get path

Code snippet to download file in default download directory


DownloadManager.Request dmr = new DownloadManager.Request(Uri.parse(url));


// If you know file name
String fileName = "filename.xyz"; 

//Alternative if you don't know filename
String fileName = URLUtil.guessFileName(url, null,MimeTypeMap.getFileExtensionFromUrl(url));

dmr.setTitle(fileName);
dmr.setDescription("Some descrition about file"); //optional
dmr.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
dmr.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
dmr.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(dmr);


Note For mContext.getSystemService

Activity= getSystemService();
Fragment= getActivity.getSystemService();
Adapter= mContext.getSystemService(); //pass context in adapter
UPDATE

As OP want to check file exist or not

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName); 

if(file.exists()){//File Exists};




No comments:

Post a Comment

Free Picture Puzzle Jigsaw Puzzle Game - best for kids learning

Picture Puzzle Jigsaw Puzzle Game for free