Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ImageView.setImageURI scales image

Tags:

android

I have a view that I am drawing to a bitmap, saving to disk and then putting in an ImageView via setImageURI. However, when the image is displayed in the ImageView it is not being shown at the correct size. It is about 1/3 smaller than it should be. I'm guessing that this is a density issue, but I can't figure out what's going wrong (my emulator is WVGA). Ideas anyone?

like image 957
herbrandson Avatar asked Jul 03 '26 11:07

herbrandson


2 Answers

I ran into this issue today too. I ended up loading a bitmap instead.

            Bitmap bit = BitmapFactory.decodeFile(image_path);
            mImageView.setImageBitmap(bit);

Which displays correctly.

like image 190
Dazzy_G Avatar answered Jul 05 '26 00:07

Dazzy_G


You're right This is a density issue. To ensure that your image displays in the correct dimensions regardless of the density of the device you should consider using dip (density independent pixel) units.

Also, Android 1.5 does not support image density -- ie. it doesn't know how to distinguish between mdpi, hdpi, ldpi bitmaps. Android 1.6 and above does. You can use Bitmap.setDensity() or BitmapDrawable.setTargetDensity()

Finally -- you mention it is 1/3 small that it should be which is a good indication that the problem is related to density as mdpi density is 160dpi and hdpi is 240dpi -- 160/240 = 2/3 which is 1/3 less than your original image.

Hope this helps!

like image 44
jagsaund Avatar answered Jul 05 '26 01:07

jagsaund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!