在网上看到一些方法都是文件的单个写入,如果assets文件夹内有复杂的文件夹目录结构,怎么把里面的所有文件夹及其子文件一起写入到SD卡中呢?
1条回答 默认 最新
- springchunye 2019-05-28 10:08关注
public static void CopyAssets(Context context, String assetDir, String dir) {
String[] files;
try {
files = context.getResources().getAssets().list(assetDir);
} catch (IOException e1) {
return;
}
File mWorkingPath = new File(dir);
// if this directory does not exists, make one.
if (!mWorkingPath.exists()) {
if (!mWorkingPath.mkdirs()) {} } for (int i = 0; i < files.length; i++) { try { String fileName = files[i]; // we make sure file name not contains '.' to be a folder. if (!fileName.contains(".")) { if (0 == assetDir.length()) { CopyAssets(context, fileName, dir + fileName + "/"); } else { CopyAssets(context, assetDir + "/" + fileName, dir+ fileName + "/"); } continue; } File outFile = new File(mWorkingPath, fileName); if (outFile.exists()) outFile.delete(); InputStream in = null; if (0 != assetDir.length()) in = context.getAssets().open(assetDir + "/" + fileName); else in = context.getAssets().open(fileName); OutputStream out = new FileOutputStream(outFile); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 visual studio2022中文乱码无法解决
- ¥15 关于华为5g模块mh5000-31接线问题
- ¥15 keil L6007U报错
- ¥15 webapi 发布到iis后无法访问
- ¥15 初学者如何快速上手学习stm32?
- ¥15 如何自动更换布娃娃图片上的衣服
- ¥15 心理学eprime编程
- ¥15 arduino esp8266开发
- ¥15 stm32单片机通过485发送命令给驱动器控制电机转动,同样的代码f103可以控制电机转动,换到f407不能动了,但是用串口助手调试407显示发送的命令都是正确的,卡了好久了这是发送规则
- ¥15 stm32f103c8t6最小系统板+2.8寸TFTLCD板子