圖片拍攝的大小會隨着硬件而變化,比如,像素高的相機拍出來的圖片要比像素低的圖片內存要大。
如此一來,針對機型可能調用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);