图片拍摄的大小会随着硬件而变化,比如,像素高的相机拍出来的图片要比像素低的图片内存要大。
如此一来,针对机型可能调用camera app保存照片的时候,图片大小会不一样。
为了缩小图片大小,我们需要把临时图片再另存为。
BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
bitmapFactoryOptions.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapFactoryOptions);
if (bm != null) {
//
if (bm.getHeight() > bm.getWidth()) {
Matrix matrix = new Matrix();
matrix.setRotate(90);
bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
}
bm = Bitmap.createScaledBitmap(bm, 640, 480, true);
}
//
FileOutputStream fos = openFileOutput(tempFileName, MODE_PRIVATE);
BufferedOutputStream os = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.JPEG, 90, os);
// 这里正常情况下是设置成100的,把它改小一点,比如这里改成90,照片大小是70-100KB,而在100的时候,它的大小是200KB-300KB。
bm.recycle();
bm = null;
fos.close();
os.flush();
os.close();
return new File(getFilesDir(), tempFileName);