如何在Android中调整从图库选择的图片大小?

13
我正在构建一个Android应用程序。在一个Activity中,我有一个图像按钮。当我单击它时,画廊打开,我可以选择一张图像。然后,我将该图像设置为图像按钮的新图像。 问题是图像在我的Activity中显得太大了。如何使它适合我的图像按钮?

我正在构建一个Android应用程序。在一个Activity中,我有一个图像按钮。当我单击它时,画廊打开,我可以选择一张图像。然后,我将该图像设置为图像按钮的新图像。 问题是图像在我的Activity中显得太大了。如何使它适合我的图像按钮?

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

            mImageButton.setImageBitmap(yourSelectedImage);
        }
    }
}
2个回答

40

你可以使用这种方法来获取调整大小后的图片。这样你就可以避免OutOfMemoryError错误。

public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize) 
            throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);

        int width_tmp = o.outWidth
                , height_tmp = o.outHeight;
        int scale = 1;

        while(true) {
            if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
    }   

requiredSize指的是高度或宽度中的任意一个。根据这段代码:

if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)

这意味着,如果照片是横向的1000x800,并且您将所需大小设置为500,则生成的图像将为500x400。如果照片是纵向的800x1000,并且指定所需大小为500,则生成的图像将为400x500。


9
requiredSize参数的含义是什么? - Tony Dinh
为什么你有两个 BitmapFactory.Options - Dr4ke the b4dass

4

参考此链接

使用:Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)

或者使用以下方法:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;
}

我应该在这之后调用那个方法吗:Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); - user1420042

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接