在图库中显示缩放尺寸imageView上的图像

时间:2011-12-03 12:56:03

标签: android imageview gallery

我想在图库中的imageView上显示iamge。 当我从画廊调用图片时,我想从相机中确定图像的大小。 你能帮我个忙吗? 当我从相机中挑选时,它可以缩放图像为createScaledBitmap。 如果你有其他想法没有setImageURI,你能给我建议吗? 我可以使用setImageBitmap而不是URI吗? 我想用相关列表保存这些图片,例如保存为blob类型的sqlite。 请指教。感谢。

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

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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高度和宽度是固定的,请指定它们。我已经采用了设备的高度和宽度来更好地理解。

答案 2 :(得分: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);
                             }

答案 3 :(得分:0)

您可以使用Bitmap工厂选项&如果你愿意,请调整图像的比例..

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