Out of memory + Bitmap + ImageView + ViewPager

905 Views Asked by At

I'm working on a simple photo gallery for my app. But I'm having some really annoying memory leaks, and I don't know how to handle them.

Can you guys help me? I've search in other question but, according to the answers, I seem to be doing things right. Of course, I'm not.

My code:

I'm using TouchImageView to show the images.

Also, I have extended ViewPager to support the zoom function, like this:

public class ExtendedViewPager extends ViewPager {

    public ExtendedViewPager(Context context) {
        super(context);
    }

    public ExtendedViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
        if (v instanceof TouchImageView) {
            /*
            canScrollHorizontally is not supported for Api < 14. To get around this issue,
            ViewPager is extended and canScrollHorizontallyFroyo, a wrapper around
            canScrollHorizontally supporting Api >= 8, is called.
            */
            return ((TouchImageView) v).canScrollHorizontallyFroyo(-dx);
        } else {
            return super.canScroll(v, checkV, dx, x, y);
        }
    }
}

Some of this photos may be big (like 2000x2000px, for example). So I resample them before adding them to the view, using this resample function:

public static Bitmap resample(int resourceID, boolean toFullScreen, Context c){
        //set the width and height we want to use as maximum display
        WindowManager wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        int targetWidth = display.getWidth();
        int targetHeight = display.getHeight();

        //create bitmap options to calculate and use sample size
        BitmapFactory.Options bmpOptions = new BitmapFactory.Options();

        //first decode image dimensions only - not the image bitmap itself
        bmpOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(c.getResources(), resourceID, bmpOptions);

        //image width and height before sampling
        int currHeight = bmpOptions.outHeight;
        int currWidth = bmpOptions.outWidth;

        //variable to store new sample size
        int sampleSize = 1;

        //calculate the sample size if the existing size is larger than target size
        if (toFullScreen || currHeight > targetHeight || currWidth > targetWidth){
            //use either width or height
            if (toFullScreen || currWidth > currHeight)
                sampleSize = Math.round((float)currHeight / (float)targetHeight);
            else
                sampleSize = Math.round((float)currWidth / (float)targetWidth);
        }

        //use the new sample size
        bmpOptions.inSampleSize = sampleSize;
        //now decode the bitmap using sample options
        bmpOptions.inJustDecodeBounds = false;

        //get the file as a bitmap and return it
        Bitmap pic = BitmapFactory.decodeResource(c.getResources(), resourceID, bmpOptions);
        return pic;
    }

And call in it on my PagerAdapter implementation, in which I also attempt to recycle() the bitmaps:

private class TouchImagePagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return galleryImages.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Context context = PhotoGalleryActivity.this;
        TouchImageView touchImageView = new TouchImageView(context);
        touchImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        touchImageView.setImageBitmap(Helper.resample(galleryImages[position].getImgResource(), false, getApplicationContext()));
        container.addView(touchImageView, 0);

        return touchImageView;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object object) {
        TouchImageView ti = (TouchImageView) object;
        BitmapDrawable bmpDrawable = (BitmapDrawable) ti.getDrawable();
        if (bmpDrawable != null) {
            Bitmap bmp = bmpDrawable.getBitmap();
            if(bmp != null && !bmp.isRecycled())
                bmp.recycle();
        }
        collection.removeView(ti);
        ti = null;
    }

    @Override
    public void destroyItem(View collection, int position, Object o) {
        View view = (View)o;
        ((ViewPager) collection).removeView(view);
        view = null;
    }
}

Any clues? Thanks in advance.

2

There are 2 best solutions below

1
On

I'd like to suggest to change the inSampleSize 2 or 4, if the original bmp size is larger than imageview. You said sometimes the image larger than 2000x2000. It may be cause a memory issue.

1
On

Add the following line in Manifest file of your project in tag

android:largeHeap="true"

For EX:

   <application
    android:name="com......"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:theme="@style/CustomActionBarTheme">

This helps you to have some more memory for your application to work