ARTICLE AD BOX
When I download a song from webview, it downloads in the folder, but its unsuccessful and I can't listen to it. I don't understand what is wrong, I've set URL by myself to make sure its correct API is 36
@RequiresApi(Build.VERSION_CODES.Q) @Composable fun DownloadMusicScreen( innerPadding: PaddingValues, applicationContext: Context, musicViewModel: MusicViewModel = hiltViewModel() ) { val url = "https://rus.hitmotop.com/" AndroidView(factory = { WebView(it).apply { layoutParams = ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT ) webViewClient = WebViewClient() loadUrl(url) setDownloadListener { url, _, contentDisposition, mimeType, _ -> setupDownloadListener( "https://eu.hitmo-top.com/get/music/20251124/Kjenni_MS_Dymka_-_Vorona_80311772.mp3", contentDisposition, mimeType, applicationContext ) musicViewModel.fetchAudioFromDevice(true) } } }, update = { it.loadUrl(url) }) } private fun setupDownloadListener( url: String, contentDisposition: String, mimeType: String, context: Context ) { val filename = getFileName(contentDisposition, url) val request = DownloadManager.Request(url.toUri()) .setMimeType(mimeType) .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE) .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) .setTitle(filename) .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, filename) val downloadManager = context.getSystemService(DownloadManager::class.java) downloadManager.enqueue(request) Toast.makeText(context, "Download started", Toast.LENGTH_SHORT).show() } private fun getFileName(contentDisposition: String?, url: String): String { return if (contentDisposition != null && contentDisposition.contains("filename=")) { contentDisposition.substringAfter("filename=").replace("\"", "") } else { url.substringAfterLast("/") } } package com.example.coroutineapp.domain class DownloadCompletedReciever: BroadcastReceiver() { private lateinit var downloadManager: DownloadManager override fun onReceive(context: Context?, intent: Intent?) { downloadManager = context?.getSystemService(DownloadManager::class.java)!! if(intent?.action == "android.intent.action.DOWNLOAD_COMPLETE"){ val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L) val query = DownloadManager.Query().setFilterByStatus(DownloadManager.STATUS_FAILED) if(id != -1L){ println("download with id $id finished!") } } } } <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" tools:ignore="ScopedStorage" /> <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" /> <!-- For Android 13+ --> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- Optional: For better download management --> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <application android:name=".app.CoroutineApplication" android:networkSecurityConfig="@xml/network_security_config" android:usesCleartextTraffic="true" <receiver android:name=".domain.DownloadCompletedReciever" android:exported="true"> <intent-filter> <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/> </intent-filter> </receiver> </application> <?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">rus.hitmotop.com</domain> <domain includeSubdomains="true">eu.hitmo-top.com</domain> <domain includeSubdomains="true">cdn1.deliciousoranges.com</domain> </domain-config> </network-security-config>