1
    for (int imageId : mImageIds) {
            Bitmap originalImage;
            originalImage= BitmapFactory.decodeResource(
                    getResources(), imageId);
            ImageView imageView=null;
            imageView = new ImageView(mContext);
            imageView.setImageBitmap(originalImage);
            imageView.setLayoutParams(new myView.LayoutParams(
                    (int) ((6 * width) / 10), MenuHeight));
            Log.i("MenuHeight", "" + MenuHeight);
            Log.i("MenuWidth", "" + (6 * width) / 10);

            imageView.setScaleType(ScaleType.MATRIX);
            mImages[index++] = imageView;
            originalImage.recycle();

        }

I am using this snippet and its giving exception trying to use recycle bitmap, if I didn't recycle the bitmap it will consume memory, so can you please tell me when and where it's best to use bitmap.recycle in any code.

zaiff
  • 999
  • 2
  • 13
  • 29

2 Answers2

0

this will fix it:

     for (int imageId : mImageIds) {
         if(originalImage!=null && !originalImage.isRecycled()){             
                Bitmap originalImage;
                originalImage= BitmapFactory.decodeResource(
                        getResources(), imageId);
                ImageView imageView=null;
                imageView = new ImageView(mContext);
                imageView.setImageBitmap(originalImage);
                imageView.setLayoutParams(new myView.LayoutParams(
                        (int) ((6 * width) / 10), MenuHeight));
                Log.i("MenuHeight", "" + MenuHeight);
                Log.i("MenuWidth", "" + (6 * width) / 10);

                imageView.setScaleType(ScaleType.MATRIX);
                mImages[index++] = imageView;

                originalImage.recycle();
                originalImage = null;
           }
}
noxius
  • 144
  • 10
0

It throw an exception cause your bitmap is used in your ImageView. To call recycle, your bitmap doesn't have to be linked to a view.

As all your pictures seem to be displayed (or linked to a physic view object), you can't recycle.

A way to handle your problem is use less memory for each pictures.

See this post: how to compress image for imageview in android

Community
  • 1
  • 1
Astrorvald
  • 223
  • 1
  • 9