DownloadManager doesn't start to download file
Asked Answered
R

4

7

Imagine that i want to download this file (random file): http://www.analysis.im/uploads/seminar/pdf-sample.pdf

This is my code:

DownloadManager.Request req = new DownloadManager.Request(Uri.parse("http://www.analysis.im/uploads/seminar/pdf-sample.pdf"));

req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
   .setAllowedOverRoaming(false)
   .setTitle("Random title")
   .setDescription("Random description")
   .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "pdf-sample.pdf");

In debug mode i can see that all parameters are corrects so why the download doesn't start?

EDIT

My current permissions:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Railing answered 18/5, 2015 at 9:48 Comment(5)
What error are you getting?Lamasery
@DroidWormNarendra none, simply download doesn't start, i'm not getting any errorRailing
do you have the proper permissions added ?Alicealicea
doesn't setDestinationInExternalPublicDir() needs WRITE_EXTERNAL_STORAGE ?Alicealicea
@Alicealicea I had also that one, sorry if i didn't add it aboveRailing
D
6

You allowed to download in the network type of DownloadManager.Request.NETWORK_MOBILE, but why did you set setAllowedOverRoaming(false)?

I tried to use Downloadmanager to download a file, here is my code:

String url = "http://example.com/large.zip";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

// only download via WIFI
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setTitle("Example");
request.setDescription("Downloading a very large zip");

// we just want to download silently
request.setVisibleInDownloadsUi(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationInExternalFilesDir(context, null, "large.zip");

// enqueue this request
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadID = downloadManager.enqueue(request);

I hope you are inspired.

Dysgenics answered 18/5, 2015 at 9:57 Comment(2)
I didn't enqueue the request, so i added it and it worked well with my code, thanks!Railing
Will this work in offline mode? My application should work without internet. I tried with this code, it doesn't work offline. Do you have any idea is it is possible to download offline with DownloadManager?Winebaum
T
1

You have only prepared the DownloadManager.Request. Calling enqueue as shown in the snippet will actually start the download

DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
long downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.

Check out this complete example for DownloadManager here

Topper answered 29/8, 2018 at 9:19 Comment(0)
M
-1

Also make sure you don't have any VPN applications enabled on the phone. It seems that some of those apps can interfere with the DownloadManager.

Mace answered 22/2, 2018 at 15:15 Comment(0)
E
-2

Follow following steps to download random file. Create a class called DownloadFileAsync.java

  public class DownloadFileAsync extends AsyncTask<String, String, String> {
  public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
  private ProgressDialog mProgressDialog;

  private Context context;

  public DownloadFileAsync(Context context) 
 {
this.context = context;
 mProgressDialog = new ProgressDialog(context);
 mProgressDialog.setMessage("Downloading file..");
 mProgressDialog.setIndeterminate(false);
 mProgressDialog.setMax(100);
 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 mProgressDialog.setCancelable(true);

}

@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}

@Override
protected String doInBackground(String... aurl) {

try {
    File root = Environment.getExternalStorageDirectory();
    URL u = new URL(aurl[0]);
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();

    int lenghtOfFile = c.getContentLength();

    FileOutputStream f = new FileOutputStream(new File(root + "/", aurl[1]));

    InputStream in = c.getInputStream();

    byte[] buffer = new byte[1024];
    int len1 = 0;
    long total = 0;

    while ((len1 = in.read(buffer)) > 0) {
        total += len1; //total = total + len1
        publishProgress("" + (int)((total*100)/lenghtOfFile));
        f.write(buffer, 0, len1);
    }
    f.close();
} catch (Exception e) {
    Log.d("Downloader", e.getMessage());
}

return null;

}

protected void onProgressUpdate(String... progress) {
 Log.d("ANDRO_ASYNC",progress[0]);
 mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
}


    }

Call the above class in your activity as

  new DownloadFileAsync(TransformerActivity.this).execute(Constants.VIDEO_DOWNLOAD_LINK,"9T51B0108.mp4");
Extraction answered 18/5, 2015 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.