2012-05-27 87 views
0

我想縮小圖像的大小,使其適合我的圖像按鈕的大小。在這個代碼中,我打開了畫廊,然後可以選擇一個圖像。問題是圖像不縮小,儘管我在代碼中這樣說。 爲什麼這不起作用?爲什麼我的Android圖庫的圖像不能縮小?

private static final int NEW_WIDTH = 10; 
private static final int NEW_HEIGHT = 10; 

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      cursor.close(); 


      Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 
      Bitmap.createScaledBitmap(yourSelectedImage, NEW_WIDTH, NEW_HEIGHT, false); 
      mImageButton.setImageBitmap(yourSelectedImage); 
     } 
    } 
} 
+0

如何使用ImageButton.setScaleType(INT)(請參見http:/ /developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType)? – caspase

回答

1

好吧,我自己解決了這個問題。

相反的:

Bitmap.createScaledBitmap(yourSelectedImage, NEW_WIDTH, NEW_HEIGHT, false); 
mImageButton.setImageBitmap(yourSelectedImage); 

式中:

Bitmap bMapScaled = Bitmap.createScaledBitmap(yourSelectedImage, NEW_WIDTH, NEW_HEIGHT, true); 
mImageButton.setImageBitmap(bMapScaled); 

我發現這篇文章非常有幫助: http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/3/