Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DownloadManager doesn't start to download file

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"/>
like image 414
Giorgio Antonioli Avatar asked May 18 '15 09:05

Giorgio Antonioli


2 Answers

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.

like image 95
SilentKnight Avatar answered Oct 20 '22 14:10

SilentKnight


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

like image 39
Irshad Kumail Avatar answered Oct 20 '22 13:10

Irshad Kumail