Search code examples
androidandroid-download-managerdownload-manager

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"/>

Solution

  • 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.