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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…