0

I am getting this error while creating an android gallery in eclipse. When I add only two images, it runs fine but when i add more images to gallery, it gives this error. Any help would be appreciated!

package com.hospital;
import com.hospital.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;


import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;


public class myGallery extends Activity {

    private final Integer[] pic = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3


    };
    ImageView imageview;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        Gallery gallery = (Gallery)findViewById(R.id.pics);
        gallery.setAdapter(new ImageAdapter(this));

        imageview = (ImageView)findViewById(R.id.ImageView01);
        gallery.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView <?> arg0, View arg1, int arg2, long arg3) {
                Toast.makeText(myGallery.this, "" + arg0, Toast.LENGTH_SHORT).show();
                imageview.setImageResource(pic[arg2]);
            }
        });
        registerForContextMenu(gallery);
    }

public class ImageAdapter extends BaseAdapter {
    private final int mGalleryItemBackground;
    private final Context mContext;
    private final Integer[] pic = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3


    };

       public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = c.obtainStyledAttributes(R.styleable.photoGallery);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.photoGallery_android_galleryItemBackground, 1);
        attr.recycle();
    }

    public int getCount() {
        return pic.length;
    }
    public Object getItem(int arg0) {
        return arg0;
    }

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

    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageResource(pic[arg0]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}
}
rdevil
  • 11
  • 2
  • Check this question and the answer. http://stackoverflow.com/questions/10161079/out-of-memory-exception-in-android-when-adding-some-image-buttons/10161107#10161107 – Carl-Emil Kjellstrand Apr 17 '12 at 10:35

2 Answers2

0

OutOfMemory is a usual error coming when using large number of bitmaps is used.The only way to reduce the error is to Call System.gc();.System.gc() in your code the Java VM may or may not decide at runtime to do a garbage collection at that point. Try calling "Bitmap".recycle() on your bitmaps after you finish your business with them. This will help you to reduce this error to the most.

Sreedev
  • 6,563
  • 5
  • 43
  • 66
  • This solution will not work. Visit the following mentioned thread to get proper picture why this exception is coming. http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object/823966#823966 – Ashwin N Bhanushali Apr 17 '12 at 11:42
0

Google has provided the lessions to deal with the issue you are facing you can visit it in the following link.

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

Hope this helps.

Ashwin N Bhanushali
  • 3,872
  • 5
  • 39
  • 59