将整个文件夹及其内容从资产复制到内部应用程序文件/

Nar*_*ngh 6 android assets copy file

请建议我将文件夹从资产复制到/ data/data/my_app_pkg/files的最佳方法.

资产(www)中的文件夹包含文件和子文件夹.我想完全复制到我提到的内部应用程序路径的文件/.

我成功地将文件从资产复制到内部应用程序文件/路径,但是在复制文件夹时无法执行相同操作,即使是assetmanager.list也没有帮助我,因为它只复制文件,但不能目录/子文件夹.

请建议我做我想做的更好的方法

dug*_*ggu 5

希望在下面的代码中使用完整的代码: -

将SD卡文件夹中的文件复制到另一个SD卡文件夹中

资产

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

}
Run Code Online (Sandbox Code Playgroud)