这是 FileUtils 工具类的方法
private static String SD_DIR = Environment.getExternalStorageDirectory().getPath() + File.separator;
public static void copyAll(Context cxt) {
File dir = new File(SD_DIR);
copySelf(cxt,"alivc_resource");
dir.mkdirs();
}
public static void copySelf(Context cxt, String root) {
try {
String[] files = cxt.getAssets().list(root);
if(files != null && files.length > 0) {
File subdir = new File(SD_DIR + root);
if (!subdir.exists()) {
subdir.mkdirs();
}
for (String fileName : files) {
File file = new File(SD_DIR + root + File.separator + fileName);
if (file.exists()&&!file.isDirectory()){
continue;
}
copySelf(cxt,root + "/" + fileName);
}
}else{
OutputStream myOutput = new FileOutputStream(SD_DIR+root);
InputStream myInput = cxt.getAssets().open(root);
byte[] buffer = new byte[1024 * 8];
int length = myInput.read(buffer);
while(length > 0)
{
myOutput.write(buffer, 0, length);
length = myInput.read(buffer);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
使用:可以放在application中执行
private AsyncTask copyTask=new AsyncTask() {
@Override
protected Object doInBackground(Object[] objects) {
FileUtils.copyAll(getApplicationContext());
return null;
}
};
//复制asset中的资源到内存卡
copyTask.execute();