Take the 2-minute tour ×

I wrote a program that at any time displays 8 user selected images on the screen. Each image is taken from its original form and scaled down to a uniform size. In order to do this I am using the code bellow:

Bitmap bitmapOrg = BitmapFactory.decodeFile(File Location Here);

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 100;
        int newHeight = 100;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);
        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        //ImageView imageView = new ImageView(this);
        ImageView iv = (ImageView) findViewById(R.id.imageView1);

        // set the Drawable on the ImageView

        // center the Image

Even though my code works its not perfect. It seems like im using up a lot of memory especially calling this code possibly 8 times at once. Where in the code would i "recycle" the memory and how could i make this code possibly run better?


So i implemented the code in my project and it was working perfect and then i tried to add it to other sections and it just stopped working all together. my code looks like this: any idea what im doing wrong?

    BitmapFactory.Options options = new BitmapFactory.Options(); 
 options.outWidth = 50; 
 options.outHeight = 50; Bitmap bitmap = BitmapFactory.decodeFile(path, options);
 ImageView iv = (ImageView) findViewById(R.id.imageView7); 
share|improve this question
What is the error? Is the original section's code still working? –  Matthew Willis Mar 24 '11 at 0:21
add comment

2 Answers

up vote 3 down vote accepted

You can use BitmapFactory.Options to scale the image as you decode it rather than reading in a full image and then scaling it.

BitmapFactory.Options options = new BitmapFactory.Options();
// You can play with this setting depending on how large your images are
// For example, to scale ~400x400 images to ~100x100, you can use 4.
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);

Edit - George is correct. You should use inSampleSize to create a smaller image of the general size you need and then have it resized to the exact size you want using your ImageView. I've corrected my answer above to reflect this.

In any case, you should be much better off memory-wise if you are scaling the bitmaps during decode.

share|improve this answer
What does the options.inSampleSize = 4; line do? –  Peter Mar 23 '11 at 22:26
During decoding it makes the resulting image 1/4 the width and 1/4 the height of the original image. This should save memory by a factor of around 16 times. –  Matthew Willis Mar 25 '11 at 4:29
@MatthewWillis: You have no idea what a life saver your answer has been. 3 days away from a huge app's launch and I was suddenly stumped with all sorts of errors. You name it and I had it. Had this: bitmap too large to be loaded into a texture. Had this: OutOfMemoryError. Warnings, crashes, et all. Sorry for the long comment, but I absolutely had to. :-) :-) Thanks a tons hardly makes up for it. –  Siddharth Lele May 23 '12 at 9:50
add comment

@matthew-willis I do not think you can use outWidth and outHeight to scale a bitmap. I believe they are output parameters only: they report the size of the bitmap created after the fact--setting them prior to decoding has no effect. You should use inSampleSize if you want to scale as you decode. George

share|improve this answer
Wow, you're right! Thank you so much. I hunted down the code in BitmapFactory.cpp, and yes, both outWidth and outHeight are immediately getting cleared to -1 and later overwritten with the output image size. I guess I had never noticed because I was using an ImageView to resize the resulting image. I have been using inSampleSize, but I hadn't considered it necessary. Thanks again! –  Matthew Willis Mar 24 '11 at 6:55
@matthew Thanks so much for your cheerful comment. Believe you me, I thought long and hard--in addition to triple-checking my facts--before daring to doubt an up-voted, accepted answer posted by someone with rep >5K :-) –  George Freeman Mar 24 '11 at 22:54
Haha. I guess the moral of the story is that sometimes even incorrect code works--and to remain vigilant! –  Matthew Willis Mar 24 '11 at 23:00
add comment

Your Answer


