assets文件夹下文件目录复制到SD卡下

本文介绍了一个用于将Android应用的资源文件从assets目录复制到设备SD卡的方法。该方法通过递归遍历assets目录来处理文件夹及文件的复制,并提供错误处理机制。
 private void copyDir(String path) {
	
	 String[] files;
	 try {
	 files = this.getAssets().list(path);
	 if (files.length == 0) {
	 copyFile(path);
	 } else {
	 String filePath = "mnt/sdcard" + "/" + path;
	 File file = new File(filePath);
	 file.mkdirs();
	 for (int i = 0; i < files.length; i++) {
	 String[] files2 = this.getAssets().list(path+"/"+files[i]);
	 if (files2.length == 0) {
	 copyFile(path + "/" + files[i]);
	 } else {
	 copyDir(path + "/" + files[i]);
	 }
	 }
	
	 }
	
	 } catch (IOException e) {
	 // TODO Auto-generated catch block
	 e.printStackTrace();
	 }
	
	 }

	private void copyFile(String path) {
	 Log.v("file", path);
	 InputStream in = null;
	 FileOutputStream out = null;
	 try {
	
	 String filePath = "mnt/sdcard" + "/" + path;
	
	 File file = new File(filePath);
	
	 if (!file.exists()) {
	 in = this.getAssets().open(path);
	 out = new FileOutputStream(file);
	 int length = -1;
	 byte[] buf = new byte[1024];
	 while ((length = in.read(buf)) != -1) {
	 out.write(buf, 0, length);
	 }
	 out.flush();
	 in.close();
	 out.close();
	 }
	 } catch (IOException e) {
	 e.printStackTrace();
	} finally {
	 try {
	 if (in != null) {
	 in.close();
	 }
	 if (out != null) {
	 out.close();
	 }
	 } catch (IOException e) {
	 e.printStackTrace();
	 }
	 }
	
	 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值