Search code examples
androidgoogle-cloud-storageandroid-download-manager

Download manager downloads a bin file if file name not specified (Google Cloud Storage URL)


I have a Google Cloud Storage download link and I am using Android DownloadManager to download the file, when I download the file without specifying the file name it ends up downloading a .bin file.

One more thing is: If I try to manually download the link from chrome browser, it works perfectly fine.

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)
}

Solution

  • In general with HTTP, for a file to be downloaded with a desired filename, the server must set the Content-Disposition header to something like attachment; filename="filename.jpg". This can be set in GCS with metadata. After you set content-disposition in this way, DownloadManager should respect it when picking filenames.