Android中assert中文件的操作

前言

访问原始文件
尽管并不常见,但您的确有可能需要访问原始文件和目录。如果确有需要,则将文件保存在 res/ 中并没有用,因为从 res/ 读取资源的唯一方法是使用资源 ID。您可以改为将资源保存在 assets/ 目录中。

保存在 assets/ 目录中的文件没有资源 ID,因此您无法通过 R 类或在 XML 资源中引用它们。您可以改为采用类似普通文件系统的方式查询 assets/ 目录中的文件,并利用 AssetManager 读取原始数据。
https://developer.android.google.cn/guide/topics/resources/providing-resources

一、将assets里面的json文件读到字符串里面

1. 将json文件放在assets里面,例如:papers.json

2. 读取方法

/**
     * 读取json文件,将整个json文件作为字符串形式返回
     * @param fileName json文件名(带后缀)
     * @param context 上下文
     * @return json文件内容的字符串形式
     */
    public static String getJsonFile(String fileName, Context context) {
//将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
//获取assets资源管理器
            AssetManager assetManager = context.getAssets();
//通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

3. 调用

String response= ReadAssetsString.getJsonFile(“papers.json”,mContext);

二、将asset里面的文件复制到 /data/user/0/包名/files 中

1. 方法

   /**
     * 将asset里面的文件复制到
     * /data/user/0/报名/files 中
     * @param context
     * @param assertName
     */
    private void loadAssetsToCache(Context context, String assertName){
        String filePath =this.getFilesDir().getAbsolutePath();
        AssetManager assetManager = context.getAssets();
        try {
            InputStream inputStream=assetManager.open(assertName);
            File file = new File(filePath);
            if (!file.exists()) {
                file.mkdirs();
            }
            //保存到本地的文件夹下的文件
            FileOutputStream fileOutputStream = new FileOutputStream(filePath + "/" + assertName);
            byte[] buffer = new byte[1024];
            int count = 0;
            while ((count = inputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, count);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

2. 调用

loadAssetsToCache(this,“china.vtpk”);

三、将asset里面的文件夹复制到 /data/user/0/包名/files 中

1. 方法

    private void copyAssertDirToData(Context context, String assetDir, String dir) {
        String[] files;
        try {
            files = context.getResources().getAssets().list(assetDir);
        } catch (IOException e1) {
            return;
        }
        File mWorkingPath = new File(dir);
        if (!mWorkingPath.exists()) {
            mWorkingPath.mkdirs();
        }
        for (int i = 0; i < files.length; i++) {
            try {
                String fileName = files[i];
                if (!fileName.contains(".")) {
                    if (0 == assetDir.length()) {
                        copyAssertDirToData(context, fileName, dir + fileName + "/");
                    } else {
                        copyAssertDirToData(context, assetDir + "/" + fileName, dir +"/"+fileName);
                    }
                    continue;
                }
                File outFile = new File(mWorkingPath, fileName);
                if (outFile.exists()) {
                    outFile.delete();
                }
                InputStream in = null;
                if (0 != assetDir.length()){
                    in = context.getAssets().open(assetDir + "/" + fileName);
                }else{
                    in = context.getAssets().open(fileName);
                }
                OutputStream out = new FileOutputStream(outFile);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3. 调用

        /**
         * RuntimeViewer3D  assert中的文件夹
         * tempPath  要复制到的文件夹的名称(全路径)
         */
        String tempPath=context.getFilesDir().getAbsolutePath()+"/"+"RuntimeViewer3D";
        copyAssertDirToData(this,"RuntimeViewer3D",tempPath);

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值