2016-03-19 47 views
0

我的資產文件夾中有一個.zip文件,我想將1-1複製到設備上的.zip文件夾中。從/ assets複製ZIP到SD卡

這種作品,但輸出zip也包含我不明白的東西。

我有什麼:

輸入:

資產: Map.mp3

輸出:

Map.zip

資產+ META-INF +組織+資源+的AndroidManifest.xml + classes.dex + resources.arsc(所有APK文件)

而在Map.zip/assets有一個Map.mp3最初的內容。但我只需要有一個1-1副本,只是文件擴展名已更改。

我的代碼:

//taken from: http://stackoverflow.com/questions/4447477/android-how-to-copy-files-from-assets-folder-to-sdcard 
     AssetManager assetManager = getAssets(); 
     AssetFileDescriptor assetFileDescriptor = null; 

     try 
     { 
      assetFileDescriptor = assetManager.openFd("Map.mp3"); 
      // Create new file to copy into. 
      File output= new File(_FILEPATH_ + java.io.File.separator + "Map.zip"); 
      output.createNewFile(); 
      copyFdToFile(assetFileDescriptor.getFileDescriptor(), output); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 


public static void copyFdToFile(FileDescriptor src, File dst) throws IOException { 
    FileChannel inChannel = new FileInputStream(src).getChannel(); 
    FileChannel outChannel = new FileOutputStream(dst).getChannel(); 
    try { 
     inChannel.transferTo(0, inChannel.size(), outChannel); 
    } finally { 
     if (inChannel != null) 
      inChannel.close(); 
     if (outChannel != null) 
      outChannel.close(); 
    } 
} 

_FILEPATH_ = Environment.getExternalStorageDirectory()getAbsolutePath()+ 「/ osmdroid」

回答

0

您可以隨時使用簡單的文件複製過程:

private void copyFile(InputStream in, OutputStream out) throws IOException { 
    byte[] buffer = new byte[1024]; 
    int read; 
    while((read = in.read(buffer)) != -1){ 
     out.write(buffer, 0, read); 
    } 
} 

使用FileDescriptor的方法不適用於壓縮文件。 據我所知,你從that answer得到這樣的解決方案。 下面的評論,

我的解決辦法是改擴建爲.mp3,然後將其刪除,一旦 複製

暗示重命名擴展.MP3由12月19日在'12日2:07。 Google可能會更改FileDescriptor的使用情況並通過重命名關閉該thrick。

我在我的Android項目中使用簡單的過程,所有工作正常,沒有性能問題。