Android拍照保存圖片內存大小


圖片拍攝的大小會隨着硬件而變化,比如,像素高的相機拍出來的圖片要比像素低的圖片內存要大。

如此一來,針對機型可能調用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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM