设置自定义文件夹Android Download Manager

时间:2013-05-27 12:34:52

标签: android download download-manager

我对下载管理器有疑问。 我要从网站下载文件。当我设置下载的默认目录(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);

3 个答案:

答案 0 :(得分:86)

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

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

    }

答案 1 :(得分:10)

您可以使用两种选择。

1)首先setDestinationInExternalPublicDir这将让你下载任何基于媒体类型的orroids标准下载文件夹,例如DIRECTORY_DOWNLOADS,DIRECTORY_MUSIC。这些文件将在卸载后保留。

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

第一个参数应该是一个标准的下载目录,以使其正常工作,并且不能是其他任何目的。

2)第二个是setDestinationInExternalFilesDir,这与上一个方法相同,不同之处在于卸载app后这些文件将被删除。

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

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

答案 2 :(得分: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();
                }