我有一个谷歌云存储下载链接,我使用Android DownloadManager下载文件,当我下载文件而不指定文件名时,它最终下载.bin文件。
还有一件事是:如果我尝试从Chrome浏览器手动下载链接,它的工作原理非常好。
fun downloadFile() {
val uri = Uri.parse("...") //link to the file
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs()
val request =
DownloadManager.Request(uri)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
//.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.jpg")
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadID = downloadManager.enqueue(request)
}
通常使用HTTP,对于要使用所需文件名下载的文件,服务器必须将Content-Disposition
标头设置为类似attachment; filename="filename.jpg"
的内容。这可以使用metadata在GCS中设置。以这种方式设置内容处理后,DownloadManager应该在选择文件名时尊重它。