Android: How to use download manager class?

Posted on

Android: How to use download manager class?Error is an illegal operation performed by the user which results in the abnormal working of the program. By now, you’ve probably seen a few errors, either when compiling or running your code like Android: How to use download manager class?. It can be frustrating, but they can also give you a lot of information about exactly how you can fix the problems in your code about java and android. In this post covers the types of errors you’ll see when programming in Java, and how to fix them. Don’t pay any attention to the number of errors. Just read the first error message and work on fixing that error.

Problem :

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?

Solution :

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.

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) {

        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
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");

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

Download progress will be showing in the notification bar.

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);

    //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.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());

DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("");
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle("My File");
request.setDestinationUri(Uri.parse("file://" + folderName + "/myfile.mp3"));

Leave a Reply

Your email address will not be published. Required fields are marked *