2013-05-27 62 views
18

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

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

回答

75

查看下面的代码:它的保存文件在"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); 

    } 
+0

非常感谢!现在它的工作;) – bott91

+30

...所以标记为接受答案。 – Danation

+0

Illegalstate exeception无法创建目录 –

6

有两种选择可供您使用。

1)first 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下载目录。

0

试试以下代码:。

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