12

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?

Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54

4 Answers4

31

My solution:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;               
Bitmap bitmap = BitmapFactory.decodeResource(this.context.getResources(),  R.drawable.image, options );
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
phusung
  • 336
  • 3
  • 2
12

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

Community
  • 1
  • 1
Ben Jakuben
  • 3,147
  • 11
  • 62
  • 104
  • 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 much – Franziskus Karsunke Sep 09 '11 at 15:35
  • 1
    "Put the image in res/drawable-nodpi" This works perfect for me using options.OutWidth & options.OutHeight – L.Grillo Sep 25 '14 at 07:49
  • 1
    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... – Simon Ninon Jul 11 '18 at 01:21
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.

Pointer Null
  • 39,597
  • 13
  • 90
  • 111
  • I found those Options, but I want the image to be loaded correctly on all devices, not only for one density. – Franziskus Karsunke Sep 09 '11 at 15:25
  • 1
    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"... – Pointer Null Sep 09 '11 at 15:33
  • Default density values of various drawable res folders are documented here: http://developer.android.com/guide/practices/screens_support.html – Pointer Null Sep 09 '11 at 15:35
  • You want this to happen, it *is* happening, so what is the issue? – Che Jami Sep 09 '11 at 15:35
  • 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. – Franziskus Karsunke Sep 09 '11 at 15:43
0

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

Tarun konda
  • 1,740
  • 1
  • 11
  • 19
  • This may cause OOM in some situations because you are creating two bitmaps. The first one with`BitmapFactory.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. – Jenix Oct 26 '18 at 10:52