23

HI,

I want to get width and height of the image which i was stored in the drawable folder. Is this possible, if so please let me know ?

1
  • 1
    This is very good answer, i used this answer to set same with message status ImageView[one checked icon, two checked icon] : stackoverflow.com/questions/9655534/… Commented Apr 26, 2015 at 18:46

2 Answers 2

62

try

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon); int height=bd.getBitmap().getHeight(); int width=bd.getBitmap().getWidth(); 
Sign up to request clarification or add additional context in comments.

5 Comments

I would prefer to do this without casts, by using Drawable.getIntrinsicWidth/Height().
using the above code, getting two-third of the original size. For e.g- an image has 644 X 550 dimension; but am getting- 429 X 367 from code. What may be the issue?
do you have multiple density folders? hdpi and mdpi? sounds like BitmapDrawable is getting confused with which one to pick. set the right density in the Options, like developer.android.com/reference/android/graphics/…
getDrawable method is deprected . which method used instead of "getDrawable" method
The non-deprecated version of getDrawable is ResourcesCompat.getDrawable, which supports themes. If you don't need a theme, just pass null into it (docs here: developer.android.com/reference/android/support/v4/content/res/…).
15

I have follow this link :-

Drawable d = getResources().getDrawable(R.drawable.yourimage); int h = d.getIntrinsicHeight(); int w = d.getIntrinsicWidth(); 

1 Comment

this is more applicable for me, because gives you the size of View independent of weather it is shown on screen or not!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.