michoprogrammer
michoprogrammer

Reputation: 1199

How can I handle java.lang.OutOfMemoryError bitmap size exceeds VM budget pageradapter?

I implemented in my app an activity that uses a ViewPager, with a PagerAdapter to show 4 images. You can see only one image at time and to show the next you have to swipe it. The size of the images is not very huge and I don't know why but it happens the error "java.lang.OutOfMemoryError bitmap size exceeds VM budget pageradapter".

This is my code:

    public class InfoActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.information);

        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }

    private class MyPagerAdapter extends PagerAdapter {

        public int getCount() {
            return 4;
        }

        public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;
            switch (position) {
            case 0:
                resId = R.layout.farleft;
                break;
            case 1:
                resId = R.layout.left;
                break;
            case 2:
                resId = R.layout.middle;
                break;
            case 3:
                resId = R.layout.right;
                break;
            }

            View view = inflater.inflate(resId, null);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
        }

        @Override
        public void finishUpdate(View arg0) {

        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);

        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }

    }

}

The R.layout.right is:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/info4" 
        android:contentDescription="@string/fourthInfoImage">
    </ImageView>

</LinearLayout>

And the others are equals except for the source image. How can I correct this error? There are several post about that error, but I didn't find the one that resolves my problem. I believe that the "destroyItem" function isn't called..

Thanks

P.S. Actually I follow the tutorial on http://mobile.tutsplus.com

Upvotes: 0

Views: 965

Answers (1)

Related Questions