Search code examples
androidimageviewout-of-memoryandroid-pageradapter

ViewPager Out of Memory + ImageView


i try to make ViewPager for Images. But i get a OutOfMemory-Error.

I read that i sholud use Bitmaps or do somthing like "reorganize();"... vut i don't understand it...

My Code:

Adapter:

public class app_info_Adapter extends PagerAdapter {
 private Activity act; 
 int[] pictures = { 
               R.drawable.1,
               R.drawable.2,
               R.drawable.3,
               R.drawable.4
             };

public app_info_Adapter(Activity act) {
    this.act = act;
}

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

@Override
public void finishUpdate(View arg0) {
    // TODO Auto-generated method stub

}
@Override
public int getCount() {
    return pictures.length;
}

@Override
public Object instantiateItem(View context, int position) {
    ImageView imageView = new ImageView(act);
    imageView.setImageResource(pictures[position]);
    LayoutParams imageParams = new LayoutParams();
    imageParams.height = LayoutParams.WRAP_CONTENT;
    imageParams.width = LayoutParams.WRAP_CONTENT;
    imageView.setLayoutParams(imageParams);

    ((ViewPager) context).addView(imageView);

    return imageView;
}

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

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
    // TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}
@Override
public void startUpdate(View arg0) {
    // TODO Auto-generated method stub
}
}

Activity

@Override
protected void onStart() {
    super.onStart();
    setContentView(R.layout.app_info);


    ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
    app_info_Adapter myPagerAdapter = new app_info_Adapter(this);
    pager.setAdapter(myPagerAdapter);
}

How can get a better performance and no OurOfMemory-Error

Thank you soo much!


Solution

  • you must read:

    http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    then use my below function which I changed the sample to read from drawable:

      public static Bitmap decodeSampledBitmapFromDrawable(Resources res,int resId,int reqWidth, int reqHeight) {
    
                // First decode with inJustDecodeBounds=true to check dimensions
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeResource(res, resId, options);
    
                // Calculate inSampleSize
                options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
                // Decode bitmap with inSampleSize set
                options.inJustDecodeBounds = false;
                return BitmapFactory.decodeResource(res, resId, options);
            }
    
    
    public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
    
                // Calculate the largest inSampleSize value that is a power of 2 and keeps both
                // height and width larger than the requested height and width.
                while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }
    
            return inSampleSize;
        }