2013-03-19 51 views
5

Vui lòng đề xuất cho tôi cách tốt nhất để sao chép thư mục từ nội dung sang/dữ liệu/dữ liệu/my_app_pkg/tệp.sao chép toàn bộ thư mục có nội dung từ nội dung sang tệp ứng dụng nội bộ/

Thư mục từ nội dung (www) chứa tệp và thư mục con. mà tôi muốn sao chép hoàn toàn vào các tệp/đường dẫn ứng dụng nội bộ của tôi được đề cập.

Tôi thành công có thể sao chép tệp từ nội dung sang tệp/đường dẫn ứng dụng nội bộ, nhưng không thể thực hiện tương tự trong trường hợp sao chép thư mục, ngay cả assetmanager.list cũng không giúp ích cho tôi, vì nó chỉ sao chép tệp, nhưng không phải thư mục/thư mục con.

Xin vui lòng đề nghị cho tôi những cách tốt hơn để làm những gì tôi muốn

+0

Vui lòng mô tả sự cố bạn gặp phải. – greenapps

Trả lời

5

Hope sử dụng đầy đủ cho bạn mã dưới đây: -

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

Tài sản

  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

Điều đó sẽ không giúp ích cho việc sao chép từ nội dung bạn cần sử dụng AssetsManager. – greenapps

+1

@greenapps tôi có chỉnh sửa câu trả lời và cảm ơn vì đã sửa đổi tôi !!!! – duggu

+0

@NarendraDroidWorm chào mừng – duggu

0

Hy vọng điều này sẽ giúp

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(); 
    } 
Các vấn đề liên quan