从相册显示缩放大小的图像到imageView上

3

我希望能够从相册中显示图像到ImageView。 当我从相册调用图片时,我想要像从相机拍摄的照片一样固定图像大小。 你能帮帮我吗? 如果是从相机选择的图片,可以使用createScaledBitmap进行缩放。 如果你有其他建议而不是使用setImageURI,能否告诉我一下? 我能否使用setImageBitmap代替URI? 我希望将这些图片与相关列表保存,例如保存到SQLite中作为Blob类型。 请给我建议。谢谢。

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode != RESULT_OK) return;
       switch (requestCode)
       {
        case PICK_FROM_CAMERA:
            Bundle extras = data.getExtras();
            Bitmap selectedImage = (Bitmap) extras.get("data");
            selectedImage = Bitmap.createScaledBitmap(selectedImage, 200, 250, false);
            mImageView.setImageBitmap(selectedImage);
            break;

        case PICK_FROM_GALLERY:
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            mImageView.setImageURI(selectedImageUri);
        break;
       }
}

1
为什么不使用Bitmap.createScaledBitmap()来调整大小呢? - Abhi
你的意思是和PICK_FROM_CAMERA一样吗?我该怎么使用它? - wholee1
4个回答

3

在CASE PICK_FROM_GALLERY中尝试这个

Uri selectedImageUri = data.getData();
   selectedImagePath = getPath(selectedImageUri);

Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);

  Bitmap bt=Bitmap.createScaledBitmap(bitmap, 150, 150, false);  

  photo_image.setImageBitmap(bt)

非常感谢!正在加载图像。干杯! - wholee1
使用这种方法我的图片被拉伸了。 - Surender Kumar

1
Bitmap photobitmap = BitmapFactory.decodeFile(selectedImagePath);


if(photoBitmap!=null)
                            {   
                                // Compressing large image to small one 

                                Display display = getWindowManager().getDefaultDisplay(); 

                                int width = display.getWidth();
                                int height = display.getHeight();


                             int photo_width = photoBitmap.getWidth();
                             int photo_height = photoBitmap.getHeight();

                            if(photo_width >width)
                                photo_width = width;

                           if(photo_height > height)
                                photo_height = height;

                                    photoBitmap = Bitmap.createScaledBitmap (photoBitmap, photo_width , photo_height , false);

                                    photo.setImageBitmap(photoBitmap);
                             }

// 如果您的imageView高度和宽度是固定的,请指定它们。我已经使用设备的高度和宽度以便更好地理解。


imageView的id是photo_image,宽度和高度为150dp。你能设置imageView的固定大小吗? - wholee1

0
ImageView imageView = findViewById(R.id.photo_image);

Bitmap photobitmap = BitmapFactory.decodeFile(selectedImagePath);


if(photoBitmap!=null)
                            {   

                             int photo_width = photoBitmap.getWidth();
                             int photo_height = photoBitmap.getHeight();
   //1dp = 1.5 px 150dp = 225px 

                            if(photo_width >225)
                                photo_width = 225;

                           if(photo_height > 225)
                                photo_height = 225;

                                    photoBitmap = Bitmap.createScaledBitmap (photoBitmap, photo_width , photo_height , false);

                                    photo.setImageBitmap(photoBitmap);
                             }

0

如果需要的话,您可以使用位图工厂选项并调整图像的比例。

Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8; // your sample size 
Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath,options);  
mImageView.setImageBitmap(bitmap);

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