1. 程式人生 > >安卓中Bitmap的處理

安卓中Bitmap的處理

呼叫安卓手機系統照相機進行拍照

系統照相機得到的相片解析度太大,如果不處理直接使用就會導致OutOfMemery異常,我採用的是下面這段程式碼:
// 計算圖片的縮放值
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;


if (height > reqHeight || width > reqWidth) {

final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}


// 根據路徑獲得圖片並壓縮,返回bitmap用於顯示
public static Bitmap getSmallBitmap(String filePath, int width, int height) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);


// Calculate inSampleSize
if(width < height){
options.inSampleSize = calculateInSampleSize(options, width, height);
}else{
options.inSampleSize = calculateInSampleSize(options, height, width);
}


// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;


return BitmapFactory.decodeFile(filePath, options);
}

這段程式碼可以按照View的大小對圖片進行縮放,當一個Bitmap 不使用時,一定要使用recycle進行釋放。

今天在呼叫照相機時還出現了一個onActivityR esult無法執行的問題,網上說是因為傳入檔案URI路徑的許可權問題,具體我也沒搞懂..蛋疼

現在的問題是橫屏拍攝縮放問題,照相機橫屏拍攝時縮放方式不正確。