2

DownloadManager throw android.os.NetworkOnMainThreadException only on Samsung devices(Android 10).

            val request = DownloadManager.Request(Uri.parse(url))
            val fileName = parseFileName(url)
            request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url))
            request.allowScanningByMediaScanner()
            request.setNotificationVisibility(
                DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
            ) 
            request.setDestinationInExternalPublicDir(
                Environment.DIRECTORY_DOWNLOADS,
                fileName
            )
            val dm = activity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
            dm?.enqueue(request)

Stacktrace:

Fatal Exception: android.os.NetworkOnMainThreadException
       at android.os.Parcel.createException(Parcel.java:2098)
       at android.os.Parcel.readException(Parcel.java:2056)
       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:188)
       at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
       at android.content.ContentProviderProxy.insert(ContentProviderProxy.java:481)
       at android.content.ContentResolver.insert(ContentResolver.java:1835)
       at android.app.DownloadManager.enqueue(DownloadManager.java:1544)

dm?.enqueue(request) throw android.os.NetworkOnMainThreadException. Although it can be called in the main thread.

How can I fix this issue?

Rage TF
  • 66
  • 1
  • 4

3 Answers3

1

I faced the same problem on my Samsung devices (s9 and a71). I use .setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, fileName) instead of .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName) And it works on Samsung devices also.

Wenceslaus
  • 71
  • 1
  • 2
1

Just got this error, and it turns out: I didn't include the File Extension!!

Ex: xxx.png

Sam Chen
  • 7,597
  • 2
  • 40
  • 73
0

@RageTF Don't know why you thank me but a NetworkOnMainThreadException is thrown when an application attempts to perform a networking operation on its main thread. This is only thrown for applications targeting the Honeycomb SDK 11 or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged. So if you are attempting to perform any of these operations on the UI thread, you must wrap them in a worker thread. The majority of the time the error stems from placing expensive operations directly on the UI thread. To ensure you don’t disrupt the user experience, it is very important to execute Socket connections, HTTP requests, file downloads, and other long-term operations on a separate Thread class.

Darkman
  • 2,941
  • 2
  • 9
  • 14
  • Yes, I know it, but I try to download image via DownloadManager. And DownloadManager.eneque() throw NetworkOnMainThreadException – Rage TF Feb 10 '21 at 11:08
  • The download manager will conduct the download in the background. https://developer.android.com/reference/android/app/DownloadManager – Rage TF Feb 16 '21 at 18:41