Updated 21 December 2018
Download Manager is a system service which optimizes the handling of long-running downloads in the background. If you want to download large files/streaming you can use Android Download Manager. The download manager handles HTTP connections, monitors connectivity changes, reboots, and ensures each download completes successfully.
Add the following permission to your AndroidManifest.xml file:
1 2 3 |
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
First, we will have to generate URI from URL.
1 |
Uri uri= Uri.parse("Your URL"); |
On click of the button, write the below mentioned code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Create request for android download manager DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(uri); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); // set title and description request.setTitle("Data Download"); request.setDescription("Android Data download using DownloadManager."); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //set the local destination for download file to a path within the application's external files directory request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"downloadfileName"); request.setMimeType("*/*"); downloadManager.enqueue(request); |
In the above code, We are passing a request to the Download Manager with the download file URL, Title and Description to be shown in notification bar etc. We can also specify whether the download should happen over wifi or cellular or both.
Thanks for reading.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
where did this come from not working