关于android自带的图片压缩

android自带的压缩图片是用bitmap.compress。需要注意的是这里压缩的只是文件大小,因为载入到bitmap里面时还会解压,所以在打印大小会发现压缩前后bitmap的大小是没变的。
bitmap存在一个很大的问题就是oom,这个问题同样在使用bitmap压缩时存在。这里网上普遍使用的一个方法是,先读取文件的配置属性,然后根据需求载入一张符合需求的缩略图,然后对缩略图进行压缩。这里带来的一个问题是,缩略图改变了图片的大小,所以会导致图片出现模糊现象。

public Bitmap getSmallBitmap(String filePath) {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // 获取当前最大可用内存
        options.inSampleSize = calculateInSampleSize(options, 1080, 1920);
        options.inJustDecodeBounds = false;

        Bitmap bm = BitmapFactory.decodeFile(filePath, options);
        if (bm == null) {
            return null;
        }
        int degree = readPictureDegree(filePath);
        bm = rotateBitmap(bm, degree);
        ByteArrayOutputStream baos = null;

        try {
            baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);

        } catch (Exception e) {
            Log.i("", e.toString());
        } finally {
            try {
                if (baos != null)
                    baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {

            }
        }
        return bm;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值