bitmap compress 是官方给出的图片质量压缩,经过试验学习了这个压缩的特性以下:学习
它是图片质量压缩,不会改变图片的分辨率测试
bitmap.compress(Bitmap.CompressFormat.JPEG, option, bos);
三个参数说明,1.图片压缩后的格式 2.图片压缩比例 3.压缩后获得的数据code
这个方法会使图片压缩可是,因为是质量压缩,bitmap不会变小,也就是内存依然大,压缩的数据确实变小使用的时候得注意了内存溢出问题orm
测试方法以下:图片
System.out.println("bitmap=="+bitmap.getByteCount()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); //经过这里改变压缩类型,其有不一样的结果 int option = 100; while (option > 0) { bitmap.compress(Bitmap.CompressFormat.JPEG, option, bos); System.out.println("bos=====" + bos.size()); option-=10; bos.reset(); } System.out.println("bitmap==" + bitmap.getByteCount()); bitmap.recycle(); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); System.out.println("bis====="+bis.available()); bitmap = BitmapFactory.decodeStream(bis); System.out.println("bitmap=="+bitmap.getByteCount()); imageView.setImageBitmap(bitmap);
若是确实要节约内存内存
就是用以下方法:get
ByteArrayOutputStream out = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, out); BitmapFactory.Options newOpts = new BitmapFactory.Options(); int be = 2;//压缩比例,能够本身经过分辨率去计算须要的比例值 newOpts.inSampleSize = be; ByteArrayInputStream isBm = new ByteArrayInputStream(out.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(isBm, null , null );