项目中需要拷贝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());    }}`

更多相关文章

  1. 使用Eclipse运行项目的时候报Conversion to Dalvik format faile
  2. NDK入门
  3. Android之 自定义属性 的使用
  4. Android(安卓)欢迎全屏图片详解及实例代码
  5. Android开发之连接MySQL数据库(一)
  6. Android培训班(55)Dalvik虚拟机的目录结构
  7. android 文件读取(assets、raw)
  8. Gradle 安装以及查看jar文件本地的存储位置
  9. android图片文件的路径地址与Uri的相互转换

随机推荐

  1. 《android framework常用api源码分析》之
  2. 重看android(二) android启动
  3. android之单元测试——上
  4. 利用Android回调机制对Dialog进行简单封
  5. android 基础概述
  6. Android程序员必看之Android六大优势
  7. android makefile(android.mk)分析(序)
  8. Android架构组件四 Android(安卓)Archite
  9. android,内存优化详解
  10. Android电话监听器