如何在 android 应用程序上更改文件下载目标

How to change file download destination on an android application

我正在开发一个 android 应用程序,用户可以在上面下载文件,下载文件后,它会自动传输到 [=17] 上的 "downloads" 文件夹=].有没有办法更改下载文件的目的地?

谢谢

这对我有用:

        Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
        DownloadManager.Request request = new DownloadManager.Request(downloadUri);
        request.setDescription("Downloading a file");
        long id =  downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false)
                .setTitle("File Downloading...")
                .setDescription("File Download Description")
                .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.png"));

您可以使用函数设置下载请求的目的地:

http://developer.android.com/reference/android/app/DownloadManager.Request.html#setDestinationUri(android.net.Uri)

Set the local destination for the downloaded file. Must be a file URI to a path on external storage, and the calling application must have the WRITE_EXTERNAL_STORAGE permission.

The downloaded file is not scanned by MediaScanner. But it can be made scannable by calling allowScanningByMediaScanner().

By default, downloads are saved to a generated filename in the shared download cache and may be deleted by the system at any time to reclaim space.