wholee1
wholee1

Reputation: 1501

Display image on scaled size imageView from gallery

I want to display iamge on imageView from gallery. When I call picture from gallery, I want to fix size the image as from camera. Could you give me some your hand? When I pick from camera, it can be scaled image as createScaledBitmap. If you have other idea not setImageURI, could you give me advice? Can I use setImageBitmap instead of URI? I want to save these picture with related list such as saving into sqlite for blob type. Please advice me. Thanks.

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

Upvotes: 3

Views: 6915

Answers (4)

Udaya Sri
Udaya Sri

Reputation: 2452

You can use Bitmap factory options & resize the scale of the image if you want ..

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

Upvotes: 0

KK_07k11A0585
KK_07k11A0585

Reputation: 2403

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

Upvotes: 0

Abhi
Abhi

Reputation: 9005

try this in 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)

Upvotes: 3

KK_07k11A0585
KK_07k11A0585

Reputation: 2403

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

// If your imageView height and widht are fixed specify them. I have taken device height and width for better understanding.

Upvotes: 1

Related Questions