android复制assets文件夹下文件到手机目录中

/**
     *  从assets目录中复制整个文件夹内容到新的路径下
     *  @param  context  Context 使用CopyFiles类的Activity
     *  @param  oldPath  String  原文件路径  如:Data(assets文件夹下文件夹名称)
     *  @param  newPath  String  复制后路径  如:data/data/(手机内部存储路径名称)
     */
    public void copyFilesFromAssets(Context context,String oldPath,String newPath) {
        try {
            String fileNames[] = context.getAssets().list(oldPath);//获取assets目录下的所有文件及目录名
            if (fileNames.length > 0) {//如果是目录
                File file = new File(newPath);
                file.mkdirs();//如果文件夹不存在,则递归
                for (String fileName : fileNames) {
                    copyFilesFromAssets(context,oldPath + "/" + fileName,newPath+"/"+fileName);
                }
            } else {//如果是文件
                InputStream is = context.getAssets().open(oldPath);
                FileOutputStream fos = new FileOutputStream(new File(newPath));
                byte[] buffer = new byte[1024];
                int byteCount=0;
                while((byteCount=is.read(buffer))!=-1) {//循环从输入流读取 buffer字节
                    fos.write(buffer, 0, byteCount);//将读取的输入流写入到输出流
                }
                fos.flush();//刷新缓冲区
                is.close();
                fos.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ////如果捕捉到错误则通知UI线程
            //MainActivity.handler.sendEmptyMessage(COPY_FALSE);
        }
    }

    /**
     * 复制assets文件夹下的文件夹到apk安装后的files文件夹中
     * @param context
     * @param folder 要复制的assets文件夹下的文件夹或文件的名字,如assets文件夹下有个文件夹是Data,则folder的值为Data
     */
    void copyFileFromAssets(Context context,String folder){
        String filesDir = context.getFilesDir().getPath();
        filesDir = filesDir + "/assets/" + folder;
        copyFilesFromAssets(context,folder,filesDir);
    }

仅做记录,使用了以下链接中一些相应代码段
https://github.com/Blankj/AndroidUtilCode

发布了9 篇原创文章 · 获赞 0 · 访问量 109

猜你喜欢

转载自blog.csdn.net/qichunhao/article/details/103985133