Jeff Bootsholz
Jeff Bootsholz

Reputation: 3068

Ways to load imageView / Image Button with the least meory

Which style consumes less memory usage ? ( to avoid OOM Exception) ..

    ImageView img = (ImageView)findViewById(R.id.test) 
 // <--android:src="@drawable/test.png" declared in layout.xml

or

       res = getBaseContext().getResources();

    imgV = (ImageView)findViewById(R.id.imageView1);
    bm1 = BitmapManager.ShrinkBitmap(res , R.drawable.test, MainActivity.this);
    imgV.setImageBitmap(bm1);

    @Override
public void onDestroy() {

    if(bm1!=null)
        if(!bm1.isRecycled()){
            bm1.recycle();
            //bm1 = null;
        }
    .....
}

...


public static Bitmap ShrinkBitmap(Resources res , int id , Activity parent){
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        bmpFactoryOptions.inPurgeable = true;
        bmpFactoryOptions.inInputShareable = true;
        bmpFactoryOptions.inPreferredConfig= Config.RGB_565;
        Bitmap bitmap = BitmapFactory.decodeResource(res, id, bmpFactoryOptions) ;          
        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeResource(res, id, bmpFactoryOptions);
        return bitmap;
    }

Upvotes: 0

Views: 128

Answers (1)

yushulx
yushulx

Reputation: 12140

I think the first one cost less memory than second one. For the first way, the decoded bitmap size(width, height) is same as ImageView size. For the second way, you decode bitmap first, but you didn't specify the width and height in Options. probably the size is bigger than ImageView. so when you set bitmap into ImageView, the bitmap will bed re-scaled to fit.

Upvotes: 1

Related Questions