2013-03-19 6 views
5

Bitte schlagen Sie mir die beste Möglichkeit vor, einen Ordner von Assets nach/data/data/my_app_pkg/files zu kopieren.Kopieren des gesamten Ordners mit seinem Inhalt von Assets in interne App-Dateien/

Der Ordner von Assets (www) enthält Dateien und Unterordner. welches ich komplett in die files meines internen apppfads kopieren möchte.

Ich bin erfolgreich in der Lage, eine Datei von Assets in interne app Dateien/Pfad zu kopieren, aber nicht im Falle des Kopierens Ordner, auch Assetmanager.list hilft mir nicht aus, da es nur das Kopieren kopiert Dateien, aber nicht die Verzeichnisse/Unterordner.

empfehlen Sie mir bitte freundlich die bessere Art und Weise zu tun, was ich unten Code Sie

+0

Bitte beschreiben Sie das Problem, auf das Sie stoßen. – greenapps

Antwort

5

Hoffnung verwenden voll wollen: -

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

Assets

  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

Das hilft nicht beim Kopieren von Assets, die Sie für die Verwendung von AssetsManager benötigen. – greenapps

+1

@greenapps ich habe edit die antwort und danke für die korrektur mir !!!! – duggu

+0

@NarendraDroidWorm welcome – duggu

0

Hope this wird helfen

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(); 
    } 
Verwandte Themen