0

I've recently been working in the 2.1 SDK (version 7) and created some images that are to be displayed via an ImageSwitcher and changed through a gallery as in one of the example apps. My images are all around 200Kb and I notice that when I switch them, Android crashes with an Out-Of-Memory error. I know the heap size is typically 16Mb, so I don't understand why the 200Kb images are crashing the app. I do have 21 images in the drawables directory, but only one is displayed at a time.

Here is my markup:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#FFFFFF">


         <ScrollView android:id="@+id/scroll"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginBottom="17dp">
             <ImageSwitcher android:id="@+id/switcher"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/adView"
            android:background="#FFFFFF"/>
        </ScrollView>

    <Gallery android:id="@+id/gallery"
        android:background="#66000000"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"        
        android:gravity="center_vertical"
        android:spacing="13dp"/>
</RelativeLayout>

and my code which came mostly from the Google Sample for the ImageSwitcher/Gallery:

public class HomeActivity extends Activity implements 
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{

    private Integer[] mThumbIds = {
            R.drawable.a3035, R.drawable.a40,
            R.drawable.a45, R.drawable.a50,
            R.drawable.a55, R.drawable.a60,
            R.drawable.a70, R.drawable.a80,
            R.drawable.a90,R.drawable.a100,
            R.drawable.a110,R.drawable.a120,
            R.drawable.a130,R.drawable.a140,
            R.drawable.a150,R.drawable.a160,
            R.drawable.a170,R.drawable.a180,
            R.drawable.a190,R.drawable.a200210,
            R.drawable.a220300};

    private Integer[] mImageIds = {
            R.drawable.aird3035, R.drawable.aird40, R.drawable.aird45,
            R.drawable.aird50, R.drawable.aird55, R.drawable.aird60,
            R.drawable.aird70, R.drawable.aird80,R.drawable.aird90,
            R.drawable.aird100, R.drawable.aird110, R.drawable.aird120,
            R.drawable.aird130, R.drawable.aird140,R.drawable.aird150,
            R.drawable.aird160, R.drawable.aird170,R.drawable.aird180,
            R.drawable.aird190, R.drawable.aird200210,R.drawable.aird220300};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

        scroll = (ScrollView)findViewById(R.id.scroll);

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(this);    
    }

    @Override
    public void onPause()
    {
        super.onPause();
        System.gc();
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        mSwitcher.setImageResource(mImageIds[position]);
        scroll.scrollTo(0,0);
            System.gc();
    }

    public void onNothingSelected(AdapterView parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFFFFFF);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        return i;
    }

    private ImageSwitcher mSwitcher;
    private ScrollView scroll;

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null) { // create new view
                convertView = new ImageView(mContext);

                convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            }
            ImageView iv = (ImageView) convertView;
            iv.setImageResource(mThumbIds[position]);
            return convertView;
        }

        private Context mContext;

    }     

}

If you notice on the OnItemSelected method, I'm even forcing a call to the garbage collector but I'm still crashing. If I downsize my images to around 93Kb each (which means I lose resolution for larger screen sizes) my app doesn't crash.

Can anyone provide some insight as to why this is happening and better yet, a valid means of fixing it?

inazaruk
  • 74,247
  • 24
  • 188
  • 156
Shawn
  • 2,406
  • 1
  • 26
  • 31

2 Answers2

4

Try this code:

  public View getView(int position, View convertView, ViewGroup parent)
  {
      if(convertView == null)
      { // create new view
          convertView = new ImageView(mContext);
          convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      }
      ImageView iv = (ImageView) convertView;
      BitmapFactory.Options options=new BitmapFactory.Options();
      options.inPurgeable=true; //bitmap can be purged to disk
      Bitmap bm=BitmapFactory.decodeResource(mContext.getResources(), mThumbIds[position], options);
      iv.setImageBitmap(bm);
      return iv;
  }

Key point here's BitmapFactory.Options.inPurgeable = true; (which is selfexplanatory) - also you can playaround with other options - sometimes it helps

Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • So far this looks like its helping barmaley. I'm curious, however, if I need to do anything different on the public void onItemSelected(AdapterView parent, View v, int position, long id) { mSwitcher.setImageResource(mImageIds[position]); scroll.scrollTo(0,0); } method since this is where the switch is actually occurring? – Shawn Jul 11 '11 at 19:48
  • @Shawn: if it helps - where's my +1? – Barmaley Jul 12 '11 at 03:52
  • I gave the +1 but its still crashing. Were you able to answer regarding the OnItemSelected question? – Shawn Jul 12 '11 at 19:51
  • @Shawn: Looks like that real switch is in onItemSelected(). Though I'm not really sure that garbage collection there is nice option. Also I don't understand purpose of scrollTo() method. Did you investigated ImageSwith example http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ImageSwitcher1.html? – Barmaley Jul 13 '11 at 04:16
  • Well, my images were larger than the available screen size so I wrapped the ImageSwitcher inside a ScrollView. That scrollTo makes sure the image starts at the top in case the previous image had been scrolled. – Shawn Jul 13 '11 at 17:15
1

Do not call System.gc(). Significantly slows down the performance.Do not do this in UI thread. NEVER!

Plamen Nikolov
  • 4,177
  • 3
  • 23
  • 24