Android不错的图片紧缩办法
一、图片质量紧缩
/**
* 质量紧缩办法
*
* @param image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量紧缩办法,这里100表现不紧缩,把紧缩后的数据寄存到baos中
int options = 90;
while (baos.toByteArray().length / 1024 > 100) { // 重复断定如果紧缩后图片是不是大于100kb,大于持续紧缩
baos.reset(); // 重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里紧缩options%,把紧缩后的数据寄存到baos中
options -= 10;// 每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把紧缩后的数据baos寄存到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
return bitmap;
}/**
* 质量紧缩办法