Android 复制 Asset 文件
private synchronized boolean copyAssetsData() {
// netkiller - https://www.netkiller.cn
AssetManager assetManager = MainApplication.getContext().getApplicationContext().getAssets();
try {
List<String> files = List.of(assetManager.list(""));
for (String file : files) {
Log.d(TAG, "coping " + file);
String target = workspace.concat("/").concat(file);
InputStream in = null;
OutputStream out = null;
try {
Log.i(TAG, "copying asset files from [" + file + "] to [" + target + "]");
in = assetManager.open(file);
(new File(target)).createNewFile();
out = new FileOutputStream(target);
byte[] data = new byte[1024];
int length = -1;
while ((length = in.read(data)) != -1) {
out.write(data, 0, length);
}
out.flush();
} catch (Exception e) {
Log.e(TAG, "skip asset files from [" + file + "] to [" + target + "]", e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, "" + e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
Log.d(TAG, "Copy assets files");
return true;
}