Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Download Manager "Download Unsuccessful"

This is my first time trying to implement DownloadManager and no matter what I try, I always get a notification saying "Download unsuccessful." I've looked at many other SO forums, a few tutorials, and what I have should work. Yes, I've set internet and external storage permissions in the manifest file. And yes, I've given storage permission in the app settings on the phone. I've tried this on both an Android emulator running API 28 and a real phone running the same. Here is the code I have:

        String url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";

        DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

        request.setTitle("title");

        request.setDescription("Your file is downloading");

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "" + System.currentTimeMillis());

        request.allowScanningByMediaScanner();
        request.setAllowedOverMetered(true);
        request.setAllowedOverRoaming(true);

        //Enqueue download and save the referenceId
        long downloadReference = downloadManager.enqueue(request);

        if (downloadReference != 0) {
            Toast.makeText(getApplicationContext(), "download started", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getApplicationContext(), "no download started", Toast.LENGTH_SHORT).show();
        }

Any help or suggestions are appreciated. Thanks.

like image 731
Ethan Shoe Avatar asked Dec 07 '22 11:12

Ethan Shoe


2 Answers

This issue occur due to network security. If You are using un-secure url in above pie API, then it can't execute your url. Check Official Documentation.

Reason for avoiding cleartext traffic is the lack of confidentiality, authenticity, and protections against tampering; a network attacker can eavesdrop on transmitted data and also modify it without being detected.

Add following in manifest to bypass all security.

<application
  android:name=".ApplicationClass"
  ....
  android:usesCleartextTraffic="true">
like image 199
Abhay Koradiya Avatar answered Dec 28 '22 08:12

Abhay Koradiya


My Experience on 1/11/2021, min SDK 19, Target SDK 30

I spent a day on using Download Service and finally it worked. To sum it up for anyone who wants to try for the first time:

  1. Dont't forget to add WRITE_EXTERNAL_STORAGE and INTERNET permissions in Manifest.
  2. use requestPermissions() to grant permission from user.
  3. use getExternalFilesDir() instead of getExternalStorageDirectory().
  4. If you're downloading from http:// so add usesCleartextTraffic="true" to manifest.
like image 30
Ehsan Avatar answered Dec 28 '22 06:12

Ehsan