pmb
pmb

Reputation: 2327

Image resize not working in Android

I have 1024 x 900 image coming from server. And I want to get the half of this image.

I use this code

img.setImageBitmap(Bitmap.createBitmap(bitmap,0,bitmap.getWidth()/2, bitmap.getWidth(), bitmap.getHeight()/2));

But when I do this I have this error

y+height must be <=bitmap.getHeight

What should I do here for not getting the exception.

Upvotes: 2

Views: 306

Answers (2)

Carlos Robles
Carlos Robles

Reputation: 10947

But with that values you can just get an unexpected part of the image, or more likely, an error.

if you check the docs of the function you can see this signature:

createBitmap(Bitmap source, int x, int y, int width, int height)

so, you have to indicate the x and y of the starting point, and the width and height of the rectangle you want.

if you want the top half, you use:

img.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth()  , bitmap.getHeight() / 2));

if you want the second half, you use:

img.setImageBitmap(Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() / 2, bitmap.getWidth()  , bitmap.getHeight() / 2));

Upvotes: 1

Alessandro Roaro
Alessandro Roaro

Reputation: 4733

The params should be the bitmap, the starting x and y, the width and the height. Then try:

img.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() / 2, bitmap.getHeight() / 2));

Upvotes: 1

Related Questions