Solek
Solek

Reputation: 51

Getting image dimensions in pixels in ImageView

While I try to get image dimensions in pixels in an ImageView, I found that its width is 3 times more than the original jpg file width.

I put a jpg file which dimensions are 800 x 600, but the code below displays 2400 as its width.

Bitmap bitmap = ((BitmapDrawable)imgv.getDrawable()).getBitmap();
float fwidth = bitmap.getWidth();
Log.d("width0", Float.toString(fwidth));

I checked the jpg file size again but it was not changed (800 x 600), I also searched for a solution but the code above displays the correct dimensions of the bitmap on other user's experience.

What have I done incorrectly? Can anyone give me some advice?

Thanks for your help.

Upvotes: 1

Views: 3959

Answers (2)

Lubomir Babev
Lubomir Babev

Reputation: 1908

Make sure, that your ImageView is set to: height : wrap_content width : wrap_content scaleType: none

Upvotes: 0

Solek
Solek

Reputation: 51

This is the solution found in a web site.

I needed to change the Option value when I decode the resource not to scale the original image. I had to use the three parameters for the decodeResource function, not two.

Of course, the third parameter was Options specifying the original bitmap not to be scaled. So now I can get 800 when I call bitmap's getWidth() function.

                Resources res = getResources();
                int id = R.drawable.map10;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inScaled = false;
//                options.inSampleSize = 3;
                Bitmap bb = BitmapFactory.decodeResource(res, id, options);

                float fwidth = bb.getWidth();

Upvotes: 3

Related Questions