我正在使用DownloadManager下载音频文件
val token = getAccessToken()
val request = DownloadManager.Request(uri)
.addRequestHeader("Authorization", "OAuth $token")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, fileName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.allowScanningByMediaScanner()
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
但是下载的文件不会反映在MediaStore中。我知道在Android 10中不建议使用allowScanningByMediaScanner()。是否有其他选择?
下载完成后尝试:
val toBeScanned = arrayOf<String>(musicPath)
MediaScannerConnection.scanFile(
this, toBeScanned, null
) { path, uri -> println("SCAN COMPLETED: $path") }