从DownloadManager下载的音频文件未反映在Android Q的MediaStore中

时间:2019-11-20 07:33:48

标签: android mediastore android-10.0

我正在使用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()。有没有其他选择?

1 个答案:

答案 0 :(得分:0)

下载完成后,请尝试以下操作:

val toBeScanned = arrayOf<String>(musicPath) MediaScannerConnection.scanFile( this, toBeScanned, null ) { path, uri -> println("SCAN COMPLETED: $path") }