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

我与影子孤独终老i 提交于 2019-12-05 07:15:24
duggu

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

}
Koushik

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();
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!