关于Obb的使用

obb文件在Android -- obb -- 对应的项目包名下。此文件用于Assets压缩使用。由于谷歌市场发行的包必须小于100M。所以我们可以将需要的资源再此打包。

obb格式为 main.版本号.包名。

将Assets文件进行打包,然后通过zip压缩,改后缀名即可。

    //获取Obb包
    public  String getObbFilePath(Context context) {
        try {
            return Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/Android/obb/"
                    + context.getPackageName()
                    + File.separator
                    + "main."
                    + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode
                    + "."
                    + context.getPackageName()
                    + ".obb";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
    //解压Obb包
    public void unZipObb(Context context) {
     
        String obbFilePath = getObbFilePath(context);
        if (obbFilePath == null) {
            return;
        } else {
            File obbFile = new File(obbFilePath);

            //解压路径为    Android/data/包名/files下(后续路径为main.版本号.包名)
            File outputFolder = context.getExternalFilesDir(null);
            if (!outputFolder.exists()) {
                outputFolder.mkdirs();
            }

            File obbfile = context.getExternalFilesDir("main." + Constant.getVersionCode(context) + "." + Constant.PageName);
            if(obbfile.exists()){
                /**
                 * 判断是否解压完毕已经该文件是否是解压的
                 * 判断条件为该文件夹下是否具有该文件
                 */
                if(!FileUtil.isNullFile(obbFile)){
                    try {
                        unZip(obbFile, outputFolder.getAbsolutePath());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                try {
                    unZip(obbFile, outputFolder.getAbsolutePath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    //解压资源
    private  void unZip(File zipFile, String outPathString) throws IOException {
        CreateDirectoryIfNeeded(outPathString);
        ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry zipEntry;
        String szName;
        while ((zipEntry = inZip.getNextEntry()) != null) {
            szName = zipEntry.getName();
            if (zipEntry.isDirectory()) {
                szName = szName.substring(0, szName.length() - 1);
                File folder = new File(outPathString + File.separator + szName);
                folder.mkdirs();
            } else {
                File file = new File(outPathString + File.separator + szName);
                CreateDirectoryIfNeeded(file.getParent());
                file.createNewFile();
                FileOutputStream out = new FileOutputStream(file);
                int len;
                byte[] buffer = new byte[1024];
                while ((len = inZip.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                    out.flush();
                }
                out.close();
            }
        }
        inZip.close();
    }
   //obb内解压资源拷贝
    private void copyMusicFile(String fromFile, String toFile){
        try {
            FileInputStream inputStream = new FileInputStream(new File(fromFile));
            byte[] data = new byte[1024];
            FileOutputStream outputStream =new FileOutputStream(new File(toFile));
            while (inputStream.read(data) != -1) {
                outputStream.write(data);
            }
            inputStream.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //判断该文件夹是否是空文件夹
    public static boolean isNullFile(File file) {
        if(file.exists()){
            String[] list = file.list();
            if (list != null) {
                if(list.length>0){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
        return false;
    }

原文链接:https://www.jianshu.com/p/ff1f13ba7577?tdsourcetag=s_pcqq_aiomsg

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值