圖片的等比例縮放,第一個參數是圖片路徑,第二個是最終所需要圖片的(寬高裡取值最大的)的最大值
[java] // 限制值MaxSize*(2/3)=實際使用值的比較值IMAGE_MAX_SIZE
// 例如:限制圖片大小為400,則實際使用的比較值應為400*(2/3)
// 260*2/3=390
public static Bitmap decodeFile(String path, int MaxSize) {
File f = new File(path);
int IMAGE_MAX_SIZE = MaxSize * 2 / 3;
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
//FileInputStream fis = new FileInputStream(f);
//BitmapFactory.decodeStream(fis, null, o);
//fis.close();
double scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = (int) scale;
FileInputStream fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
摘自 agods–足跡