1

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.

1
  • What have I done incorrectly? Nothing. You are probably stretching your ImageView to fit the width of its container. Jul 15, 2016 at 8:01

2 Answers 2

3

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();
1
  • options.inJustDecodeBounds = true; Will give you the width without reading the whole bitmap into memory.
    – Matt D.
    Mar 14, 2018 at 15:35
0

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

4
  • I followed your directions, wrap_content and no scale type, thanks but it is still same. Besides, I am using setImageMatrix, getImageMatrix and matrix.postScale to scale the image by fingers. Do I need to remove those codes? I am afraid that those are changing the ImageView's scale type. (like I used setScaleType())
    – Solek
    Jul 15, 2016 at 9:04
  • Yes , I think if you remove then, you will get the correct image size Jul 15, 2016 at 10:29
  • Then, how can I zoom in/out image inside ImageView??
    – Solek
    Jul 18, 2016 at 2:15
  • I found my image is in the folder /res/drawable, can it be a problem?
    – Solek
    Jul 18, 2016 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.