首页 > 解决方案 > 从 url 下载音频(文件)到 externalCache

问题描述

我有音频“网址”“https://my-server.com/555.mp3”

如何将其下载到“externalCacheDir”以便无需互联网即可轻松访问

我尝试使用 DownloadManager 但我认为有更好的方法来做到这一点

    fun downloadAudio(url:String){
        val request = DownloadManager.Request(url.toUri())
        val externalCachPath = getApplication<Application>().externalCacheDir!!.absolutePath
        val fileName = "${UUID.randomUUID()}.3gp"
        request.setDestinationInExternalPublicDir(externalCachPath,fileName)

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
        request.setDescription("Download")

        request.setTitle(fileName)

        download(request)

        return filename
        
    }

标签: android

解决方案


推荐阅读