2013-03-19 11 views
5

資産から/ data/data/my_app_pkg/filesにフォルダをコピーする最善の方法を教えてください。その内容を持つフォルダ全体を資産から内部のアプリケーションファイルにコピーする/

アセットのフォルダ(www)には、ファイルとサブフォルダが含まれています。私は完全にファイルにコピーしたい/私の内部のアプリケーションパスの上記の。

ファイルを内部アプリケーションファイル/パスにコピーすることはできましたが、フォルダをコピーする場合は同じ操作を実行できませんでした。assetmanager.listは私を助けません。ファイルは削除できますが、ディレクトリ/サブフォルダは削除されません。

親切に私が

+0

遭遇する問題を説明してください。 – greenapps

答えて

5

ホープコードの下にあなたにフル使いたいものを行うには良い方法をお勧めしてください: -

Copy files from a folder of SD card into another folder of SD card

資産

  AssetManager am = con.getAssets("folder/file_name.xml"); 


public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation) 
    throws IOException { 

if (sourceLocation.isDirectory()) { 
    if (!targetLocation.exists()) { 
     targetLocation.mkdir(); 
    } 

    String[] children = sourceLocation.list(); 
    for (int i = 0; i < sourceLocation.listFiles().length; i++) { 

     copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]), 
       new File(targetLocation, children[i])); 
    } 
} else { 

    InputStream in = new FileInputStream(sourceLocation); 

    OutputStream out = new FileOutputStream(targetLocation); 

    // Copy the bits from instream to outstream 
    byte[] buf = new byte[1024]; 
    int len; 
    while ((len = in.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 
    in.close(); 
    out.close(); 
} 

} 
+1

これは、AssetsManagerを使用する必要がある資産からコピーするのに役立ちません。 – greenapps

+1

@greenapps私は答えを編集してくれてありがとうございました! – duggu

+0

@ NarendraDroidWormようこそ – duggu

0

希望これを助けてくれるでしょう

private void getAssetAppFolder(String dir) throws Exception{ 

     { 
      File f = new File(sdcardlocation + "/" + dir); 
      if (!f.exists() || !f.isDirectory()) 
       f.mkdirs(); 
     } 
     AssetManager am=getAssets(); 

     String [] aplist=am.list(dir); 

     for(String strf:aplist){ 
      try{ 
       InputStream is=am.open(dir+"/"+strf); 
       copyToDisk(dir,strf,is); 
      }catch(Exception ex){ 


       getAssetAppFolder(dir+"/"+strf); 
      } 
     } 



    } 


    public void copyToDisk(String dir,String name,InputStream is) throws IOException{ 
     int size; 
      byte[] buffer = new byte[2048]; 

      FileOutputStream fout = new FileOutputStream(sdcardlocation +"/"+dir+"/" +name); 
      BufferedOutputStream bufferOut = new BufferedOutputStream(fout, buffer.length); 

      while ((size = is.read(buffer, 0, buffer.length)) != -1) { 
       bufferOut.write(buffer, 0, size); 
      } 
      bufferOut.flush(); 
      bufferOut.close(); 
      is.close(); 
      fout.close(); 
    } 
関連する問題