为什么我的Android图像图像不缩小?

时间:2012-05-27 11:46:40

标签: android image resize

我想缩小图像,使其符合图像按钮的大小。在这段代码中,我打开了画廊,然后可以选择一个图像。问题是虽然我在我的代码中这样说,但图像不会缩小。 为什么这不起作用?

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);
        }
    }
}

1 个答案:

答案 0 :(得分: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/