6

This is not working on Some Devices. In Samsung Device they not allow to download file using Download manager. I have already define permission in the manifest and also get RunTime Permission.

DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setTitle("");
request.setDescription("");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(imageFile));
request.setMimeType("*/*");
downloadManager.enqueue(request);
Bhavin Parghi
  • 71
  • 2
  • 4
  • 1
    Did you check all your permissions ? Write exteranal storage, read external storage etc.. – JDevoloper Feb 06 '20 at 09:58
  • 1
    yes, I did check that. not able to download in some devices.this and throws this exception. – Bhavin Parghi Feb 06 '20 at 10:04
  • can you post your path that you want to download? – JDevoloper Feb 06 '20 at 10:07
  • 1
    This is my path where i want to download image : java.lang.SecurityException: Unsupported path /storage/emulated/0/GoldalEngineering/JPEG_5e3bbe8ed6c75_24741_.jpg – Bhavin Parghi Feb 06 '20 at 10:14
  • 1
    if you download this path from another devices, if your problem is only about Samsung devices, I am not sure about it. At the moment, DownloadManager request accepts only paths on the "standard" external storage, as retrieved by Environment.getExternalStoragePublicDirectory(yourDirOfChoice) or Environment.getExternalStorageDirectory(); otherwise, it throws this exception. – JDevoloper Feb 06 '20 at 10:19
  • 1
    Same issue I am getting in Emulator with Android Version Q[10]. – Bhavin Parghi Feb 06 '20 at 11:01

2 Answers2

5

Use this

setDestinationInExternalFilesDir(
            context.applicationContext,
            Environment.DIRECTORY_DOWNLOADS,
            ""
        )
Shaon
  • 2,496
  • 26
  • 27
1

I guess the problem with runtime permission if you are running the download manager on Marshamellow+, So you should request runtime permission, it's not adequate to add permission in Manifest file. Check runtime permission from official documentation it would guide you to solve the problem.

Note: we should request storage permission as it considered as dangerous permission. on the other hand, we don't have to request Internet permission because it's a normal one. I hope this suffices.

MustafaKhaled
  • 1,343
  • 9
  • 25