I have a problem with a ListActivity, it loads a search result, 5 items at a time, and when it reach about 45 items, it throws an out of memory exception.
I load images from web server asyncronically. If I comment this, i can scroll to the bottom of the list (aprox 58 items) without problem. I Know i should call bitmap.Recycle() to gc the bitmaps i no longer use, but I dont find when i should call that.
class ImageLoadingTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
String url;
public ImageLoadingTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
imageViewReference.get().setImageDrawable(null);
}
@Override
protected void onPostExecute(Bitmap result) {
if (isCancelled()) {
result = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(result);
//result.recycle();
}
}
}
@Override
protected void onPreExecute() {
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageResource(R.drawable.icon);
}
}
}
@Override
protected Bitmap doInBackground(String... params) {
url = params[0];
return Application.get(getContext()).loadBitmap(url);
}
}
As you see in the onPostExecute method. if I call recycle right after the image is asigned to the imageview, it thros an exeption of tying to use a recycled image.
Anybody could help?
Thanks in advance, and sorry for my english :P