将assets文件中内容复制到SDCard中
来源:互联网 发布:windows已出现关键问题 编辑:程序博客网 时间:2024/05/16 04:55
/** * 作者:Android绝世小菜鸟 *链接:http://www.jianshu.com/p/ec5792496e38 *來源:简书 *著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 */public class AssetsCopyer { private String asset_list_fileName; private final Context mContext; private final AssetManager mAssetManager; private File mAppDirectory; public AssetsCopyer(Context context, String asset_list_fileName) { mContext = context; mAssetManager = context.getAssets(); this.asset_list_fileName = asset_list_fileName; } /** * 将assets目录下指定的文件拷贝到sdcard中 * @return 是否拷贝成功,true 成功;false 失败 * @throws IOException */ public boolean copy() throws IOException { List<String> srcFiles = new ArrayList<>(); //获取系统在SDCard中为app分配的目录,eg:/sdcard/Android/data/$(app's package) //该目录存放app相关的各种文件(如cache,配置文件等),unstall app后该目录也会随之删除 mAppDirectory = mContext.getExternalFilesDir(null); if (null == mAppDirectory) { return false; } //读取assets/$(subDirectory)目录下的assets.lst文件,得到需要copy的文件列表 List<String> assets = getAssetsList(); for( String asset : assets ) { //如果不存在,则添加到copy列表 if( ! new File(mAppDirectory,asset).exists() ) { srcFiles.add(asset); } } //依次拷贝到App的安装目录下 for( String file : srcFiles ) { copy(file); } return true; } /** * 获取需要拷贝的文件列表(记录在assets/assets.lst文件中) * @return 文件列表 * @throws IOException */ protected List<String> getAssetsList() throws IOException { List<String> files = new ArrayList<>(); InputStream listFile = mAssetManager.open(new File(asset_list_fileName).getPath()); BufferedReader br = new BufferedReader(new InputStreamReader(listFile)); String path; while (null != (path = br.readLine())) { files.add(path); } return files; } /** * 执行拷贝任务 * @param asset 需要拷贝的assets文件路径 * @return 拷贝成功后的目标文件句柄 * @throws IOException */ protected File copy( String asset ) throws IOException { InputStream source = mAssetManager.open(new File(asset).getPath()); File destinationFile = new File(mAppDirectory, asset); if(destinationFile.exists()){ return destinationFile; } destinationFile.getParentFile().mkdirs(); OutputStream destination = new FileOutputStream(destinationFile); byte[] buffer = new byte[1024]; int nread; while ((nread = source.read(buffer)) != -1) { if (nread == 0) { nread = source.read(); if (nread < 0) break; destination.write(nread); continue; } destination.write(buffer, 0, nread); } destination.close(); return destinationFile; }}
另外,需要在assets文件夹中新建一个assets.lst文件,将需要复制到内存卡的文件名写到该文件中。如下:
阅读全文
0 0
- 将assets文件中内容复制到SDCard中
- 将assets文件夹中的文件复制到sd卡中
- 将文件保存到sdCard中去
- 将'文件1'的内容复制到'文件2'中
- android将raw或者assets中的文件复制到手机中
- 将android项目assets文件夹下的文件复制到SD卡中
- android 将Assets中的文件拷贝到sdcard上
- android 将Assets中的文件拷贝到sdcard上
- assets目录文件复制到SD卡中
- Android Assets下文件复制到sdcard
- 如何将文件上传到android模拟器sdcard中
- Android程序函数 将assets文件夹下的文件复制到手机的sd卡中(包括子文件夹)
- Android程序函数 将assets文件夹下的文件复制到手机的sd卡中(包括子文件夹)
- 文件流复制数据到文件 , 将文件1.txt的内容复制到2.txt 中
- Java-基础 文件操作IO 将文件内容复制到另外一个文件中去
- UltraEdit中Python语法高亮显示:将下面内容复制到wordfile.txt文件中
- 系统调用,将一个文件复=中的内容复制到另一个文件中去
- 将android工程中assets中的文本文件复制到指定的SD卡目录中
- 【优化】对偶上升法(Dual Ascent)超简说明
- 图论练习1:银河英雄传说解题报告
- Python学习笔记
- sealed
- 软件开发培训
- 将assets文件中内容复制到SDCard中
- 结构体与模板
- AlertDialog弹窗
- 10.7 数列 2391
- 正则表达式初探
- Lost My Music 凸包+可持久化栈+倍增
- Apache的IO工具类
- spring框架
- git使用介绍及基本操作