关于android自带的图片压缩

来源:互联网 发布:twap和vwap 算法代码 编辑:程序博客网 时间:2024/05/09 10:49

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;    }
0 0
原创粉丝点击