0

Hi guys i am facing the outofmemory error while navigating between two page which has 2 Gallery controls(Image Gallery) each with some more additional details to fill out the profile informations. In first page we can view the profile details and we move to another page to edit the details and from there we can click on the cancel button to return back to the first page. So when clicking the edit and cancel buttons continuously the profile edit page opens and closed. While repeating the same process for some time, then the applications starts running through outofmemory and at one stage application fails to load the page due to insufficient internal memory. I am checking out for the possible solution to overcome this issue. Any advice or comments would be appreciated. Hoping for better response. Thanks in Advance.

Below is my Code:

edit_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //startActivity(new Intent(ProfileView.this, ProfileEdit.class));
            startActivity(new Intent(ProfileView.this, ProfileEditLatest.class));
        }
    });

Below is getView method of gallery adapter:

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

    /*if (position>data.length || position<0) {
        return null;
    }*/
    View vi=convertView;
    ImageView image;
    if(convertView==null){
        vi = inflater.inflate(R.layout.gridview_single, null);

    //TextView text=(TextView)vi.findViewById(R.id.text);;
    image=(ImageView)vi.findViewById(R.id.image);

    final float scale = activity.getResources().getDisplayMetrics().density;
    //int pixels = (int) (100 * scale + 0.5f);
    int pixels = (int) (100 * scale + 0.5f);

    //i.setLayoutParams(new Gallery.LayoutParams(100, 100));
    //image.setLayoutParams(new Gallery.LayoutParams(pixels, pixels));
    image.setLayoutParams(new RelativeLayout.LayoutParams(pixels,pixels));

    //image.setLayoutParams(new LayoutParams(100,100));

    //text.setText("item "+position);
    }
    else{
        image=(ImageView)convertView;
    }
    try{
    vi.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, image);
    }
    catch (ArrayIndexOutOfBoundsException e) {
        // TODO: handle exception
    }
    return vi;
}

In above one i have used the lazy loading implemented which is available at URL

And in my Profile edit page i have a bitmap object declared as static which is used while uploading some new images into the gallery.

Hope the above details would be helpful for finding the solution to the problem.

Dinash
  • 3,027
  • 4
  • 32
  • 45
  • Wild guess: you're loading the images again everytime to enter the gallery without freeing (removing references) the previous ones. – m0skit0 Dec 18 '12 at 10:28
  • 1
    U can find solution here http://stackoverflow.com/questions/3238388/android-out-of-memory-exception-in-gallery – Veaceslav Gaidarji Dec 18 '12 at 10:29
  • @m0skit0 could you please let me know how to remove the reference to the images once the page is closed. – Dinash Dec 18 '12 at 10:40
  • @AamirShah i have added the code to my question – Dinash Dec 18 '12 at 11:01
  • check the heap size while debugging the code and you will find where the heap size is increasing abruptly more over also take a view at the following link http://developer.android.com/training/displaying-bitmaps/index.html – Usman Kurd Dec 18 '12 at 11:37

1 Answers1

1

There is a problem with your getView() method. This is not how you are supposed to use it. Instead you must use staic View Holder class...

 @Override
public View getView(int position, View v, ViewGroup parent) {
    // Keeps reference to avoid future findViewById()
    ViewHolder viewHolder;

    if (v == null) {
        LayoutInflater li = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.gridview_single, null);

        viewHolder = new ViewHolder();
        viewHolder.mIV = (ImageView) v.findViewById(R.id.image);


        v.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) v.getTag();
    }

  // call ur image loader here
    imageLoader.DisplayImage(data[position], activity, image);
    return v;
}

static class ViewHolder {
    ImageView mIV;

}
Aamir Shah
  • 4,473
  • 8
  • 21
  • 28