以下代碼是一個輔助類,如同在第1章定義的那樣,它用于通過URI加載一個位圖對象,并將其縮放到不大于屏幕的大小。
private Bitmap loadBitmap(Uri imageFileUri){
Display currentDisplay = getWindowManager().getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
// 期望是ARGB_4444
Bitmap returnBmp = Bitmap.createBitmap((int)dw, (int)dh,
Bitmap.Config.ARGB_4444);
try {
// 載入圖像的尺寸而非圖像本身
BitmapFactory.Options bmpFactoryOptions =
new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
returnBmp = BitmapFactory.decodeStream(getContentResolver().
openInputStream(imageFileUri), null, bmpFactoryOptions);
int heightRatio = (int)Math.ceil(bmpFactoryOptions.
outHeight/dh);
int widthRatio = (int)Math.ceil(bmpFactoryOptions.
outWidth/dw);