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 ?

ramsbh
  • 656
  • 4
  • 14
  • 26
  • 1
    This is very good answer, i used this answer to set same with message status ImageView[one checked icon, two checked icon] : http://stackoverflow.com/questions/9655534/android-obtaining-image-size-from-its-resource-id – SBotirov Apr 26 '15 at 18:46

2 Answers2

62

try

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int height=bd.getBitmap().getHeight();
int width=bd.getBitmap().getWidth();
Jett Hsieh
  • 3,159
  • 27
  • 33
  • 11
    I would prefer to do this without casts, by using Drawable.getIntrinsicWidth/Height(). – hackbod Dec 29 '10 at 22:30
  • 1
    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? – Avisek Chakraborty Nov 12 '11 at 14:36
  • 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 http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html – David T. Aug 30 '12 at 23:59
  • 1
    getDrawable method is deprected . which method used instead of "getDrawable" method – Sanket990 Jun 01 '15 at 20:09
  • 2
    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: http://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html). – GeordieMatt Apr 05 '16 at 10:59
15

I have follow this link :-

Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
  • 1
    this is more applicable for me, because gives you the size of View independent of weather it is shown on screen or not! – Navid Oct 22 '14 at 10:58