How to get the height and width of an ImageView/Bitmap in Android
Asked Answered
U

2

31

I want to get the height and width of an image bitmap that is either in ImageView or a background image. Please help me, any help will be appreciated.

Unpin answered 16/1, 2012 at 12:59 Comment(2)
Please explain your question little more,it is not possible to understand what you need from the lines your have written here in question.Robichaud
That is OK you got an answer but please make sure that your question not only should be useful (that it is) but also should show that you have done some efforts in research in that direction. That makes it super useful. Enabling developers in engaging them in experiments and see that the issue can be reproduced. Those ways you get more upvotes, and hence increases your reputation very fast.Bazooka
F
97

You can get height and width of ImageView by using getWidth() and getHeight() through while this will not give you the exact width and height of the image, for getting the Image width height first you need to get the drawable as background then convert drawable to BitmapDrawable to get the image as Bitmap from that you can get the width and height like here

Bitmap b = ((BitmapDrawable)imageView.getBackground()).getBitmap();
int w = b.getWidth();
int h = b.getHeight();

or do like here

imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
int w = b.getWidth();
int h = b.getHeight();

the above code will give you current imageview sized bitmap like screen shot of device

for only ImageView size

imageView.getWidth(); 
imageView.getHeight(); 

If you have drawable image and you want that size you can get like this way

Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight(); 
int w = d.getIntrinsicWidth();      
Franklinfranklinite answered 16/1, 2012 at 13:15 Comment(2)
The first one gives: LayerDrawable cannot be cast to android.graphics.drawable.BitmapDrawable And people upvoted thisLauralauraceous
It gives me android.graphics.drawable.BitmapDrawable@38f9adfin the log. None of the methods worked for me.Epicrisis
E
3

For some reason accepted answer didn't work for me, instead I achieved the image dimension as per the target screen dpi like this.

Method 1

Context context = this; //If you are using a view, you'd have to use getContext();
Resources resources = this.getResources();
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeResource(resources, R.drawable.cake, bounds); //use your resource file name here.
Log.d("MainActivity", "Image Width: " + bounds.outWidth);

Here is the original link

http://upshots.org/android/android-get-dimensions-of-image-resource

Method 2

BitmapDrawable b = (BitmapDrawable)this.getResources().getDrawable(R.drawable.cake);
Log.d("MainActivity", "Image Width: " + b.getBitmap().getWidth());

It doesn't show the exact number of pixels in the image resource but a meaningful number perhaps someone can explain further.

Epicrisis answered 5/1, 2018 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.