【发布时间】:2020-07-26 11:29:23
【问题描述】:
从 url 下载文件的最佳方式是什么。我尝试使用下载管理器。但我不明白如何获取下载文件的 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。请帮帮我
【问题讨论】:
标签: android kotlin uri android-download-manager