Alex
Alex

Reputation: 1056

ViewPager: bitmap size exceeds VM budget

I have an app where i use ViewPager and set in every page"Fragment" an image.

when i scroll the view pager after image 25 it is giving me out of memory exception but i delete the image every time go to next page.

check code below:

public void destroyItem(View collection, int position, Object view) {
    Log.d("**", "remove image");
    ((ViewPager) collection).removeView((ImageView) view);
} //method: remove item from pager adapter

i tried code from here to delete ImageView but it did not solved the problem:

  private void unbindDrawables(View view) {
     if (view.getBackground() != null) {
          view.getBackground().setCallback(null);
     }
     if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
           unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
      }
}

edit:
below code will solve the problem, but i want to use the full image size and why it is not delete the image?

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //reduce image size

Upvotes: 0

Views: 1094

Answers (1)

dmon
dmon

Reputation: 30168

There are two possible issues:

  1. Your images are just too big
  2. The GC might not be running as quickly as it has to. If you're using bitmaps you should call recycle() after your remove them from the views. This makes sure the GC can reclaim the memory. Be careful if these Bitmaps are reused in any of the other views though.

Upvotes: 1

Related Questions