18

Using this code:

    Drawable blankDrawable = context.getResources().getDrawable(image);
    Bitmap blankBitmap=((BitmapDrawable)blankDrawable).getBitmap();

I get a bitmap that is scaled to the density of the context, preserving the physical size of the bitmap (based on its dpi value). So for example, I have a 405x500 bitmap (dpi=96) as a resource. But when I load it on a device, I get a 608x750 image with density=240. I want to load the bitmap without scaling. How do I do that?

This question is very similar to:

How to create a Drawable from a stream without resizing it?

However, that solution cannot be used in my case, because I don't have an input stream. All I have is a resource id, and the getDrawable() method does not have parameters for density. Once the bitmap is loaded, it is too late - it was already resized.

Thank you.

weston
  • 54,145
  • 21
  • 145
  • 203
nagylzs
  • 3,954
  • 6
  • 39
  • 70

5 Answers5

30

use this

InputStream is = this.getResources().openRawResource(imageId);
Bitmap originalBitmap = BitmapFactory.decodeStream(is);  
imageview.setImageBitmap(originalBitmap);
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
16

When you're decoding the bitmap with

BitmapFactory.decodeResource (Resources res, int id, BitmapFactory.Options opts) 

Set the flag inScaled in BitmapFactory.Options to false first.

Example:

/* Set the options */
Options opts = new Options();
opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
opts.inScaled = false; /* Flag for no scalling */ 


/* Load the bitmap with the options */
bitmapImage = BitmapFactory.decodeResource(context.getResources(),
                                           R.drawable.imageName, opts);
Chirry
  • 640
  • 6
  • 12
  • Could you explain inDither? I have read the doc, but do not quite understand it. "@deprecated. if dither is true, the decoder will attempt to dither the decoded image." – CoderYel May 29 '17 at 15:00
7

Another good option may be to put the bitmap in the drawable-nodpi resource folder

nmr
  • 16,625
  • 10
  • 53
  • 67
3

Create a drawable (without hdpi/mdpi etc) folder in res. Keep the drawable in that folder. Now try it. This may help you.

Walid Hossain
  • 2,724
  • 2
  • 28
  • 39
  • 8
    Here is another way to do it. Create new folder res/drawable-nodpi and put your image there! It won't be scaled so this will load it with the original size (in pixels): Drawable dh = Context.getResources().getDrawable(R.drawable.your_image_id); Bitmap bh =((BitmapDrawable)dh).getBitmap(); – nagylzs Oct 03 '11 at 10:32
2
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView image = (ImageView) findViewById(R.id.test_image);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
    image.setImageBitmap(bMap);
}

First create an ImageView instance containing the ImageView from the layout. Then create a bitmap from the application icon (R.drawable.icon) with BitmapFactory.decodeResource(). Finally set the new bitmap to be the image displayed in the ImageView component of the layout.

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
  • 1
    It might work. However, my application directly uses hundreds of bitmaps, and draws on them in-memory. In fact, the method gets the context as a parameter. It is only used to get resources. Otherwise the bitmap is not directly displayed... So I like the InputStream version much better, because it does not use any GUI component. – nagylzs Oct 01 '11 at 16:43