AssetUtil工具类


public class AssetsUtil
{
    public static byte[] getAssertsFileInBytes(String assetsFileName) {
        InputStream is;
        try {
            is = DpApp.getContext().getResources().getAssets().open(assetsFileName);
            int ch = 0;
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((ch = is.read()) != -1) {
                out.write(ch);
            }
            out.close();
            is.close();
            byte[] buff = out.toByteArray();
            return buff;
            
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    /**
     * 
     * @title: getAllAssetsList
     * @description: TODO
     * @param assetsPath
     *            注意一定不是"/" 如果读取assets根目录下的东西 传入""即可
     * @return
     * @return: String[]
     */
    public static String[] getAllAssetsList(String assetsPath) {
        AssetManager asserter = DpApp.getContext().getAssets();
        String[] fileNames = null;
        try {
            fileNames = asserter.list(assetsPath);
            for (String fileName : fileNames) {
                LogUtil.d(fileName);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return fileNames;
    }
    
    /**
     * 从assets目录中复制整个文件夹内容
     * 
     * @param context
     *            Context 使用CopyFiles类的Activity
     * @param assetsPath
     *            String 原文件路径 如:/aa , 如果读取assets根目录下的东西 请传入""
     * @param newPath
     *            String 复制后路径 如:xx:/bb/cc
     */
    public static boolean copyFilesFassets(String assetsPath, String newPath) {
        Context context = DpApp.getContext();
        try {
            String fileNames[] = context.getAssets().list(assetsPath);//获取assets目录下的所有文件及目录名
            if (fileNames.length > 0) {
                File file = new File(newPath);
                file.mkdirs();
                for (String fileName : fileNames) {
                    if (!TextUtils.isEmpty(assetsPath)) {
                        assetsPath += "/";
                    }
                    copyFilesFassets(assetsPath + fileName, newPath + "/" + fileName);
                }
            }
            else {//如果是文件
                InputStream is = context.getAssets().open(assetsPath);
                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();
            }
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}



转载于:https://my.oschina.net/sfshine/blog/497924

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值