2011-11-27 61 views
1

我有異步任務,必須將文件夾「文件」從資產寫入SD卡上的文件夾。但沒有任何工作。java android複製文件資產

final String sdDir = "/sdcard/izuchaika/"; 

new Thread(new Runnable() { 
     public void run() { 
      try { 

       InputStream in = getAssets().open("Files"); 
       OutputStream out = new FileOutputStream(new File(sdDir)); 
       try { 
        byte[] bucket = new byte[32 * 1024]; 
        int bytesRead = 0; 
        while (bytesRead != -1) { 
         bytesRead = in.read(bucket); 
         out.write(bucket, 0, bytesRead); 
        } 
       } catch (IOException ex) { 
        ex.printStackTrace(); 
       } finally { 
        try { 
         if (in != null) 
          in.close(); 
         if (out != null) 
          out.close(); 
        } catch (IOException ex) { 
         ex.printStackTrace(); 
        } 

       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


     } 
    }).start(); 

Screen of folder tree

+0

一般提示:*不要*硬編碼到外部存儲/ SD卡*路徑(如你在你的代碼的1號線一樣)*。請改用['Environment.getExternalStorageDirectory()'](http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()),因爲實際路徑和存儲類型因設備而異* (有些內置閃存代替SD卡)* – 2011-11-27 21:49:37

回答

1

你寫入和讀取目錄,文件。

更換

InputStream in = getAssets().open("Files"); 
OutputStream out = new FileOutputStream(new File(sdDir)); 

InputStream in = getAssets().open("Files/exit.png"); 
OutputStream out = new FileOutputStream(new File(sdDir+"/exit.png")); 
+0

,並且記得要求在清單中寫入SD卡的許可 –

+0

InputStream in = getAssets()。open(「Files/exit.png」); 但是這樣我就可以只讀文件? 我需要閱讀所有文件夾,因爲有很多文件。 – Val

+0

然後列出資產目錄中的文件並進行循環。問題是什麼? –

相關問題