2013-04-30 76 views
2

我在我的應用程序中遇到了兩個問題。如何使用Android下載管理器將文件保存在SD卡上?

1 - 我使用的Android DownloadManager到網上下載文件,但它下載到系統目錄中的所有文件說Download目錄使用

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "fileName"); 

現在我想將文件下載到我自己的目錄說:「新建文件夾」。如何實現。請在這方面幫助我,我會非常感謝。

2-如何檢查SD卡是否可用?

回答

3

用這樣的方式:在這裏Dhaval_files是我的文件夾名稱

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

親愛的我有點迷惑約downloadManager obj in line long id = dow nloadManager.enque(.......) – user1703737 2013-04-30 08:46:25

+0

我用這種方式創建了obj請檢查它:DownloadManager downloadManager =(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE); – user1703737 2013-04-30 08:47:28

+0

Great Dhawal!非常感謝你解決了我的問題 – user1703737 2013-04-30 09:16:59

3

DownloadManager

1)

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("Image File Download") 
        .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png")); 

2)

Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); 

if(isSDPresent) 
{ 
    // yes download code 
} 
else 
{ 
// No sdcard 
} 
相關問題