指定图片文件大小压缩
实现
public Bitmap compressAndGenImage(Bitmap image, String outPath, int maxSize)
throws IOException {
Bitmap bitmap;
ByteArrayOutputStream os = new ByteArrayOutputStream();
int options = 100;
image.compress(Bitmap.CompressFormat.JPEG, options, os);
while (os.toByteArray().length / 1024 > maxSize) {
os.reset();
options -= 10;
image.compress(Bitmap.CompressFormat.JPEG, options, os);
}
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
bitmap = BitmapFactory.decodeStream(is);
FileOutputStream fos = new FileOutputStream(outPath);
fos.write(os.toByteArray());
fos.flush();
fos.close();
is.close();
return bitmap;