Android 拷贝Asset目录下文件或者文件夹
来源:互联网 发布:java开发语言 编辑:程序博客网 时间:2024/05/22 07:08
项目中需要拷贝Asset目录下的所有文件,因为Asset目录是只读的,操作起来不是很方便,上网搜了一些方法并不是很有效,记录一下最后的解决方案:
//path - asset下文件(夹)名称 destinationPath - 目的路径`private void copyAssetFile(String path,String destinationPath) { AssetManager assetManager = mContext.getAssets(); String assets[] = null; try { Log.i("tag", "copyFileOrDir() "+path); assets = assetManager.list(path); if (assets.length == 0) { copyFile(path,destinationPath); } else { String fullPath = destinationPath + path; Log.i("tag", "path="+fullPath); File dir = new File(fullPath); if (!dir.exists() && !path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit")) if (!dir.mkdirs()) Log.i("tag", "could not create dir "+fullPath); for (int i = 0; i < assets.length; ++i) { String p; if (path.equals("")) p = ""; else p = path + "/"; if (!path.startsWith("images") && !path.startsWith("sounds") && !path.startsWith("webkit")) copyAssetFileOrDir( p + assets[i],destinationPath); } } } catch (IOException ex) { Log.e("tag", "I/O Exception", ex); }}``private void copyFile(String filename,String destinationPath) { AssetManager assetManager = mContext.getAssets(); InputStream in = null; OutputStream out = null; String newFileName = null; try { Log.i("tag", "copyFile() "+filename); in = assetManager.open(filename); newFileName = destinationPath + filename; out = new FileOutputStream(newFileName); byte[] buffer = new byte[1024]; int read; while ((read = in.read(buffer)) != -1) { out.write(buffer, 0, read); } in.close(); in = null; out.flush(); out.close(); out = null; } catch (Exception e) { Log.e("tag", "Exception in copyFile() of "+newFileName); Log.e("tag", "Exception in copyFile() "+e.toString()); }}`
0 0
- Android 拷贝Asset目录下文件或者文件夹
- android Raw和Asset文件夹下文件的区别
- Android之asset目录下文件的使用
- Dealing with Asset Compression in Android Apps(避免asset目录下文件被压缩)
- 拷贝件或者文件夹
- android拷贝assets目录下文件到其他目录
- android 对于asset下文件的操作
- mysql 直接拷贝data 目录下文件
- mysql 直接拷贝data 目录下文件
- android遍历文件夹下文件
- 列出指定目录下文件或者文件夹,包含子目录的内容。
- treeview显示目录下文件和文件夹!
- 如何指定文件夹处理目录下文件
- 遍历目录下文件或文件夹
- c++ 遍历目录下文件和文件夹
- [linux]统计目录下文件、文件夹个数
- 获取tomcat 目录指定文件夹并删除文件夹下文件
- 获取tomcat 目录指定文件夹并删除文件夹下文件
- Android studio 配置Git (第一次提交代码)
- Mysql非安装版的安装使用相关问题
- marathon参考(9):健康检查
- 使用keepalived实现Redis双机热备、高可用
- Could not find class 'org.apache.http.entity.mime.MultipartEntity'
- Android 拷贝Asset目录下文件或者文件夹
- 翻译 DBFlow Getting Started 入门指南
- 《Spark MLlib机器学习》已正式上市发售
- 懒人工具3 -- 远程访问
- JAVA使用JDBC连接oracle数据库
- android动画入门,进阶,提高----自定义控件绘图篇和Path之贝赛尔曲线
- ands TFTP Ping ICMP FTP ARP DNS HTTP
- Android中的sqlite简单示例
- 22. Generate Parentheses My Submissions QuestionEditorial Solution