S
S
Sergey Karbivnichy2022-01-22 11:44:41
Java
Sergey Karbivnichy, 2022-01-22 11:44:41

Why is the file not downloading in Android using DownloadManager?

Here is the code:

try {
                    DownloadManager dm;

                    dm = (DownloadManager)getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(DOWNLOAD_URL));
                    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                            .setAllowedOverRoaming(false)
                            .setTitle("Download")
                            .setDestinationInExternalPublicDir(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
                                    ,"file.jpg");

                    Log.d("DEBUG", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() );
                   dm.enqueue(request);


                }catch (Exception e){
                    Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
                }

All permissions are granted, but the file is not downloading:
61ebc29bd7d9c210047635.png
Logcat:
E/ActivityThread: Service com.android.providers.downloads.DownloadJobService has leaked IntentReceiver com.android.pr[email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Service com.android.providers.downloads.DownloadJobService has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1355)
        at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1134)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1421)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1394)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1382)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:609)
        at addon.sprd.downloadprovider.AddonDownloadsDRMUtils.notifyDownloadCompleted(AddonDownloadsDRMUtils.java:302)
        at com.android.providers.downloads.DownloadThread.run(DownloadThread.java:504)

Google searched all over, but everywhere the same examples. It works for everyone on SO, though the answers are 2017-2019, but I see that every year Google complicates application development.
I thought the DownloadManager was supposed to make it easier to download files, but apparently not.
So why is this example currently not working? Does anyone have a minimal working example? Or is it better to abandon this "manager", and use OkHttp?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2022-01-22
@hottabxp

More than 10 hours killed to find and solve the problem. It turned out to be - Are you missing a call to unregisterReceiver()? generally does not relate to the problem in any way. The problem was the presence of the ad blocker Blokada (works as a local vpn server). And what is most interesting, Blokada shows all the hosts that Android and other applications access (assuming the application accesses http/https), even if they are on the block list. But when I started my application, she just kept silent. Probably a blocker bug.

O
Oleg, 2022-01-22
@402d

... has leaked IntentReceiver ... Are you missing a call to unregisterReceiver()?
Did you hang up a broadcast receiver? And they forgot to take it off!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question