28

I want to download a binary file from a url. Is it possible to use the Android download manager class that I found here DownloadManager class?

Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47
Kris
  • 3,709
  • 15
  • 50
  • 66
  • Yes its possible. Infact that's the reason this class was created. You can check this tutorial https://androidclarified.com/android-downloadmanager-example/ – Irshad Kumail Aug 29 '18 at 09:01

4 Answers4

48

Is it possible to use the android download manager class that i found here

Yes, though that is only available since Android API Level 9 (version 2.3). Here is a sample project demonstrating the use of DownloadManager.

yydl
  • 24,284
  • 16
  • 65
  • 104
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    @CommonsWare..I went through your all code .all are great. I want to make use of DownloadManager with wifi means ..I have to use SFTP and download file from router ..Is it possible? will pls guide me for this.. – NovusMobile Jun 07 '12 at 12:55
  • 2
    @Satyam: `DownloadManager` does not support SFTP. Nothing in Android supports SFTP AFAIK. You will need to find a third-party JAR for that. – CommonsWare Jun 07 '12 at 12:58
  • I found that API that I used in java name "JSCH". @ CommonsWare In my java Project its run perfect but in here Android its not.. is it possible ? or may I facing other issue? pls reply.. – NovusMobile Jun 07 '12 at 13:05
  • 2
    @Satyam: If you look in the upper-right corner of this page, you should see an "Ask Question" link. That is used for asking questions. Comments in StackOverflow are fine for clarifications of existing answers, but not for unrelated topics such as this. – CommonsWare Jun 07 '12 at 13:12
  • I am extremely sorry for this. I already asked the question :http://stackoverflow.com/questions/10927102/android-sftp-resumable-download-file In hasty forgot to submit link to you... Its true nobody replied till...for the same.. – NovusMobile Jun 07 '12 at 13:20
  • Very usefull repo! – Calin May 16 '17 at 12:37
23

Use DownloadManager class (GingerBread and newer only)

GingerBread brought a new feature, DownloadManager, which allows you to download files easily and delegate the hard work of handling threads, streams, etc. to the system.

First, let's see a utility method:

/**
 * @param context used to check the device version and DownloadManager information
 * @return true if the download manager is available
 */
public static boolean isDownloadManagerAvailable(Context context) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
        return true;
    }
    return false;
}

Method's name explains it all. Once you are sure DownloadManager is available, you can do something like this:

String url = "url you want to download";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");

// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);

Download progress will be showing in the notification bar.

m.chorakchi
  • 331
  • 2
  • 5
9

Make sure the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions is in your Manifest.xml file:

And then include this code in your download function

if(ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
    || ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
    
    // this will request for permission when user has not granted permission for the app
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}

else{
    //Download Script
    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    Uri uri = Uri.parse("URL of file to download");
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setVisibleInDownloadsUi(true);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
    downloadManager.enqueue(request);
}
Gary Chen
  • 248
  • 2
  • 14
ABODE
  • 958
  • 2
  • 15
  • 13
8
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://www.example.com/myfile.mp3");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle("My File");
request.setDescription("Downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.parse("file://" + folderName + "/myfile.mp3"));
downloadmanager.enqueue(request);
SMR
  • 6,628
  • 2
  • 35
  • 56
Dmarp
  • 208
  • 1
  • 3
  • 13