什么是从URL下载文件的最佳方法?我尝试使用DownloadManager。但是我不知道如何获取已下载文件的Uri。这是我的代码:
file?.let {
val uri = Uri.parse(it)
val downloadManager = getSystemService<Any>(Context.DOWNLOAD_SERVICE) as DownloadManager?
val request = DownloadManager.Request(uri)
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or
DownloadManager.Request.NETWORK_MOBILE)
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "downloadfileName")
request.setMimeType("*/*")
downloadManager?.enqueue(request)
}
也许现在有更好的方式来下载文件并获取它的Uri,请帮助我。