2013-05-21 54 views
0

出於某種原因,我需要加入一些與我的apk文件的defaut文件,但我需要寫入SD卡,因爲我的應用程序試圖讀取我需要第一次存儲的SD卡我的SD卡的默認文件在APK中加入文件將它們存儲在SD卡中

如何做到這一點,我不能粘貼我的數據在我的根文件夾android拒絕它,如果我把它粘貼在一個可繪製的文件夾它似乎確定編譯,但我仍然duno如何獲得內容和我的SD卡上寫

我看到很多政黨成員TU得到從SD卡的可繪製,但我wan't相反,我會從默認的文件我已經加入寫SD卡上的文件到APK

有什麼想法?我沒有代碼,因爲我沒有任何提示可以做到這一點

+1

您可以嘗試使用完整的句子編寫嗎?我無法理解問題是什麼。 –

+2

這不應該被關閉 - 對那些熟悉android的人來說,這是一個真正的問題,正如接受的答案所指出的那樣,這是一個可以回答的問題。 –

回答

0

在Eclipse中壓縮文件並將壓縮文件(將其命名爲my_raw_files.zip)存儲到文件夾res/raw中,並將其與您的應用程序。然後,您可以在第一次啓動應用程序時將其複製到外部存儲設備(「sdcard」):

private static final File USER_DIR = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator 
     + "myfolder"); 
... 
boolean dirCreated = USER_DIR.mkdirs(); 
if (dirCreated) 
{ 
    FilesUtil.unzipFiles(this.getResources().openRawResource(R.raw.my_raw_files), 
        USER_DIR.getAbsolutePath()); 
} 
... 
static public void unzipFiles(InputStream zipIS, String outputPath) 
{ 
    try 
    { 
     // unzip files into existing folder structure 
     ZipInputStream zin = new ZipInputStream(zipIS); 
     try 
     { 
      ZipEntry ze; 
      while ((ze = zin.getNextEntry()) != null) 
      { 
       if (!ze.isDirectory()) 
       { 
        Log.d(TAG, "unzip " + ze.getName()); 

        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        byte[] buffer = new byte[1024]; 
        int count; 
        FileOutputStream fout = new FileOutputStream(outputPath + File.separator + ze.getName()); 
        while ((count = zin.read(buffer)) != -1) 
        { 
         baos.write(buffer, 0, count); 
         byte[] bytes = baos.toByteArray(); 
         fout.write(bytes); 
         baos.reset(); 
        } 
        fout.close(); 
       } 
      } 
     } finally 
     { 
      zin.close(); 
     } 

    } catch (Exception e) 
    { 
     Log.e(TAG, "unzip", e); 
    } 

}