试试这个代码:
/**
* reduces the size of the image
* @param image
* @param maxSize
* @return
*/
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 0) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
像这样使用它:
Bitmap scaledImage = getResizedBitmap(photo, 300); //here 300 is maxsize
来自Wikipedia
对于最高质量的图像 (Q=100),每个颜色像素需要大约 8.25 位
因此,对于 300x300 图像上的 Q=100,这将导致 (300 * 300) px * 8.25 bits/px = 741,500 bits = ~ 91 kB,这肯定小于 200KB
你也可以尝试其他维度..
您也可以尝试使用位图制作图像并比较图像的实际大小..
代码如下:
//create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
f.createNewFile();
//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();