【问题标题】:How to resize an image i picked from the gallery in android?如何调整我从 android 的画廊中挑选的图像的大小?
【发布时间】:2012-06-02 03:55:14
【问题描述】:

我正在哪里构建一个 android。在一项活动中,我有一个图像按钮。当我点击它时,画廊会打开,我可以选择一张图片。然后我将该图像设置为图像按钮的新图像。 问题是图像在我的活动中显得太大了。如何使它适合我的图像按钮?

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

【问题讨论】:

    标签: android image resize gallery


    【解决方案1】:

    您可以使用此方法获取调整大小的图像。这样可以避免 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参数是什么意思吗?
    【解决方案2】:

    参考这个LINK

    使用: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);
    猜你喜欢
    • 2011-10-27
    • 1970-01-01
    • 2016-05-12
    • 2011-07-05
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2019-11-29
    • 2018-07-11
    相关资源
    最近更新 更多