25

我有一个关于下载管理器的问题。我要从一个站点下载一个文件。当我设置默认下载目录(Environment.DIRECTORY_DOWNLOAD)时,一切正常,我的下载开始了。但是,如果我尝试更改目录,我的应用程序不会下载该文件。特别是,我希望我的文件进入下载中的文件夹,例如 /storage/sdcard/Download/myFolder。我该如何解决?

File mydownload = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ "/myFolder");

if (!mydownload.exists()){
    mydownload.mkdir();
}

String url = sUrl[0];
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}

request.setDestinationInExternalPublicDir(mydownload.getAbsolutePath(),"Myfile.extension");


DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
4

4 回答 4

94

检查下面的代码:它的保存文件在"sdcard/dhaval_files/". 只需替换您的文件夹名称并write_external_storage在 android 清单文件中授予权限。

public void file_download(String uRl) {
        File direct = new File(Environment.getExternalStorageDirectory()
                + "/dhaval_files");

        if (!direct.exists()) {
            direct.mkdirs();
        }

        DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);

        Uri downloadUri = Uri.parse(uRl);
        DownloadManager.Request request = new DownloadManager.Request(
                downloadUri);

        request.setAllowedNetworkTypes(
                DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false).setTitle("Demo")
                .setDescription("Something useful. No, really.")
                .setDestinationInExternalPublicDir("/dhaval_files", "test.jpg");

        mgr.enqueue(request);

    }
于 2013-05-27T13:07:39.687 回答
14

有两个选项可供您使用。

1) 首先 setDestinationInExternalPublicDir 这将允许您根据媒体类型下载任何 androids 标准下载文件夹,例如 DIRECTORY_DOWNLOADS、DIRECTORY_MUSIC。这些文件将在卸载后保留。

request.setDestinationInExternalPublicDir(DIRECTORY_DOWNLOADS,
        File.separator + folderName + File.separator + fileName);

第一个参数应该是一个标准的下载目录,这样才能正常工作,不能是其他任何东西。

2)第二个是setDestinationInExternalFilesDir 这和之前的方法一样,不同的是这些文件会在应用卸载后被删除。

request.setDestinationInExternalFilesDir(context, DIRECTORY_DOWNLOADS, 
        File.separator + folderName + File.separator + fileName);

这里第二个参数可以是 null 或任何 android 下载目录。

于 2014-10-13T02:56:33.707 回答
1

试试下面的代码:。

    String storagePath = Environment.getExternalStorageDirectory()
                        .getPath()
                        + "/Directory_name/";
                //Log.d("Strorgae in view",""+storagePath);
                File f = new File(storagePath);
                if (!f.exists()) {
                    f.mkdirs();
                }
                //storagePath.mkdirs();
                String pathname = f.toString();
                if (!f.exists()) {
                    f.mkdirs();
                }
//                Log.d("Storage ",""+pathname);
                dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
                Uri uri = Uri.parse(image);
                checkImage(uri.getLastPathSegment());
                if (!downloaded) {
                    DownloadManager.Request request = new DownloadManager.Request(uri);
                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                    request.setDestinationInExternalPublicDir("/Directory_name", uri.getLastPathSegment());
                    Long referese = dm.enqueue(request);

                    Toast.makeText(getApplicationContext(), "Downloading...", Toast.LENGTH_SHORT).show();
                }
于 2017-08-08T06:39:14.487 回答
0

为下载文件设置路径使用:为我工作 (Android 11)。

 File file = new File(Environment.getExternalStorageDirectory().getPath() + "/YOUR FOLDER/", "YOUR FILE.(mp3|mp4|pdf|...)");

        request.setDestinationUri(Uri.fromFile(file));

完整代码:

首先检查目录

  private boolean CreateDirectory() {
    boolean ret = false;
    File filepath = Environment.getExternalStorageDirectory();
    File dir = new File(filepath.getPath() + "/YOUR FOLDER/");
    if (!dir.exists()) {
        try {
            dir.mkdirs();
            ret = true;

        } catch (Exception e) {
            ret = false;
            e.printStackTrace();
        }
    }
    return ret;
}

然后:

        String URL = " YOUR URL ";

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL));
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
        request.setTitle("YOUR TITLE");
        request.setDescription("YOUR DESCRIPTION");
        request.allowScanningByMediaScanner();
   request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

        File file = new File(Environment.getExternalStorageDirectory().getPath() + "/YOUR FOLDER/", "YOUR FILE.(mp3|mp4|pdf|...)");

        request.setDestinationUri(Uri.fromFile(file));
        DownloadManager manager= (DownloadManager) 
        getSystemService(Context.DOWNLOAD_SERVICE);
        Long downloadId= manager.enqueue(request);

好的,完成

于 2021-09-10T19:31:55.847 回答