BitmapFactory returns bigger image than source
Asked Answered
S

4

14

Hi i am creating a Bitmap from an png image named image.png. The image has the dimension 75 (width) x 92 (height). When I run this code:

Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(),  R.drawable.image
Log.d("image", "height: " + bitmap.getHeight() + " width: " + bitmap.getWidth());

the logger logs:

DEBUG/image(3550): height: 138 width: 113

and the image on the screen is bigger than other images which have the dimension 75 x 92. What can I do to make android load the image with the right dimension?

Stringhalt answered 9/9, 2011 at 15:0 Comment(1)
can you get the drawable's height and width() at runtime, in your code?Navigable
P
33

My solution:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;               
Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(),  R.drawable.image, options );
Pru answered 29/11, 2012 at 8:39 Comment(0)
N
15

It sounds like your screen density on your device is different than the density where image.png was created.

If you really want to prevent the scaling, you could try one of the following:

  1. Put the image in res/drawable-nodpi (http://developer.android.com/guide/practices/screens_support.html#qualifiers)

  2. Use ImageView.ScaleType.CENTER (http://developer.android.com/reference/android/widget/ImageView.ScaleType.html)

  3. Just found this related question on SO: Android: How to stop Android 1.6+ from scaling images

Neom answered 9/9, 2011 at 15:19 Comment(3)
The first one does affect nothing, the third is true for images from the web (I am already using it) and the second one is the solution!!! Thank you very muchStringhalt
"Put the image in res/drawable-nodpi" This works perfect for me using options.OutWidth & options.OutHeightFingerstall
Wow, life savor! I had a drawable that was just 100KB big but whenever I was loading it with Picasso, on some devices it resulted in an OOM with a failed allocation fo 28MB! Moving from drawable to drawable-nodpi solved the issue somehow...Phenacaine
N
4

Beause loader in BitmapFactory applies screen density scaling during loading. To override this, provide own desired inTargetDensity in BitmapFactory.Options in call to decodeResource.

Nagy answered 9/9, 2011 at 15:19 Comment(5)
I found those Options, but I want the image to be loaded correctly on all devices, not only for one density.Stringhalt
I assume you want to load in original pixel size on any device, right? If your image is in drawable resource folder, then set inTargetDensity=160, which is density of resources in drawable folder, and no scaling during load will occur. Otherwise correct answer may depend on what you mean by "right dimension"...Nagy
Default density values of various drawable res folders are documented here: developer.android.com/guide/practices/screens_support.htmlNagy
You want this to happen, it is happening, so what is the issue?Dennett
Is the TargetDensity 160 for tablets as well? If not the other answer is better, because I dont have too many view issues in my code.Stringhalt
S
0

Bitmap thumbImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.image), 640, 640, false);

Sacchariferous answered 12/8, 2018 at 8:13 Comment(1)
This may cause OOM in some situations because you are creating two bitmaps. The first one withBitmapFactory.decodeResource and the second one with Bitmap.createScaledBitmap. Even if you don't face the OOM issue, there is one more unnecessary overhead cost using the code above.Pomcroy

© 2022 - 2024 — McMap. All rights reserved.