Narendra Singh
Narendra Singh

Reputation: 4001

copying the entire folder with its contents from assets to internal app files/

Please, suggest me the best way of copying a folder from assets to /data/data/my_app_pkg/files.

The folder from assets (www) contains files and subfolders. which I want to completely copy to the files/ of my internal app path mentioned.

I am successfully able to copy a file from assets to internal app files/ path, but unable to do the same in case of copying folder, even assetmanager.list isn't helping me out, as it is copying only the files, but not the directories / subfolders.

Please kindly suggest me the better way to do what I want

Upvotes: 8

Views: 8145

Answers (2)

Koushik
Koushik

Reputation: 352

Hope this will help

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

Upvotes: 0

duggu
duggu

Reputation: 38439

Hope use full to you below code:-

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

}

Upvotes: 6

Related Questions