Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

DownloadManager throw android.os.NetworkOnMainThreadException

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?

question from:https://stackoverflow.com/questions/66050509/downloadmanager-throw-android-os-networkonmainthreadexception

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...