How to get width and height of the image?

37,542

Solution 1

try

BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int height=bd.getBitmap().getHeight();
int width=bd.getBitmap().getWidth();

Solution 2

I have follow this link :-

Drawable d = getResources().getDrawable(R.drawable.yourimage);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();
Share:
37,542

Related videos on Youtube

ramsbh
Author by

ramsbh

Updated on May 13, 2022

Comments

  • ramsbh
    ramsbh 15 days

    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 ?

  • hackbod
    hackbod over 11 years
    I would prefer to do this without casts, by using Drawable.getIntrinsicWidth/Height().
  • Avisek Chakraborty
    Avisek Chakraborty over 10 years
    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?
  • David T.
    David T. over 9 years
    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/…
  • Navid
    Navid over 7 years
    this is more applicable for me, because gives you the size of View independent of weather it is shown on screen or not!
  • Sanket990
    Sanket990 almost 7 years
    getDrawable method is deprected . which method used instead of "getDrawable" method
  • GeordieMatt
    GeordieMatt about 6 years
    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/r‌​es/…).