[android-developers] OOM exception with custom imageadapter

2010-04-26 Thread Lars
Hi

I have a gallery in wich I show a row of images, the images can both
be from the Camera gallery or from the internet as a web url. My
getView() method makes an exception when I put to many images from the
gallery inside it. Should i down sample the images before attaching
them to the view? and how is that done ? heres my getView method:

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


Controller co = Controller.GetInstance();

ImageView imageView = new ImageView(context);

 
if(co.server.currRecord.image.get(position).containsKey(data))
{
// we have a content URI and assign it to the gallery
 
imageView.setImageURI((Uri)co.server.currRecord.image.get(position).get(data));
}
else
{
//we have a web url and need our ImageOperation method to
fetch and decode the drawable.
 
imageView.setImageDrawable(FileConvert.ImageOperations(context,
Controller.GetInstance().server.currRecord.image.get(position).get(url).toString()));
}

imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);

return imageView;
}

 - Lars

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] OOM exception with custom imageadapter

2010-04-26 Thread Romain Guy
Hi,

As written, your getView() method *never* reuses the convertView
parameter. This means you are creating new Views and new Bitmaps every
time the method is called, which happen very often while
scrolling/flinging a grid.

You first need to start using the convertView when possible (if
convertView != null, cast it to an ImageView and set your drawable on
it.) Note that setting the scale type, layout params and background
resource should be done only when convertView == null. This will save
you a lot of time.

Finally, you should create a cache for your bitmaps to avoid reloading
them all the time.

I encourage you to read the source code of the following application
to see how to implement an efficient list of images:
http://code.google.com/p/shelves/

Also refer to my Google I/O 2009 talk about UI optimization:
http://code.google.com/events/io/2009/sessions/TurboChargeUiAndroidFast.html
In this talk I describe how to implement getView() efficiently and
talk about image caches briefly at the end.

On Mon, Apr 26, 2010 at 12:50 AM, Lars axberg.l...@gmail.com wrote:
 Hi

 I have a gallery in wich I show a row of images, the images can both
 be from the Camera gallery or from the internet as a web url. My
 getView() method makes an exception when I put to many images from the
 gallery inside it. Should i down sample the images before attaching
 them to the view? and how is that done ? heres my getView method:

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


        Controller co = Controller.GetInstance();

        ImageView imageView = new ImageView(context);


 if(co.server.currRecord.image.get(position).containsKey(data))
        {
                // we have a content URI and assign it to the gallery

 imageView.setImageURI((Uri)co.server.currRecord.image.get(position).get(data));
        }
        else
        {
                //we have a web url and need our ImageOperation method to
 fetch and decode the drawable.

 imageView.setImageDrawable(FileConvert.ImageOperations(context,
 Controller.GetInstance().server.currRecord.image.get(position).get(url).toString()));
        }

        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
        imageView.setBackgroundResource(itemBackground);

        return imageView;
    }

  - Lars

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en




-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support.  All such questions should be posted on
public forums, where I and others can see and answer them

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en