Thanks for the reply.

I'm currently using this as my image cache, maybe you can point out
where I'm going wrong:

private final class MyGalleryAdapter extends SimpleCursorAdapter {
        private final HashMap<String, SoftReference<Bitmap>> mImageCache =
                new HashMap<String, SoftReference<Bitmap>>();

        private final BitmapFactory.Options mOpts;

        private int mImagePathColumn;

        public MyGalleryAdapter(Context context) {
                super(
                        context,
                        R.layout.my_gallery_item,
                        null,
                        new String[] {MediaStore.Images.Thumbnails.DATA},
                        new int[] {R.id.my_gallery_item_image}
                );

                mOpts = new BitmapFactory.Options();
                mOpts.inDither = false;
                mOpts.inPreferredConfig = Bitmap.Config.RGB_565;

        }

        @Override public void changeCursor(Cursor cursor) {
                mImageCache.clear();
                super.changeCursor(cursor);
                mImagePathColumn = cursor.getColumnIndex
(MediaStore.Images.Thumbnails.DATA);

        }

        @Override public void bindView(View view, Context context, Cursor
cursor) {
                final ImageView imgView = (ImageView)view;
                final String path = cursor.getString();
                Bitmap image = null;

                if(mImageCache.containsKey(path)) {
                        image = mImageCache.get(path).get();

                }

                if(image==null) {
                        image = BitmapFactory.decodeFile(path, mOpts);
                        mImageCache.put(path, new SoftReference<Bitmap>(image));

                }

                imgView.setImageBitmap(image);
        }
}


I also tried replacing the call to mImageCache.clear() in changeCursor
() with this:

Bitmap tmp = null;
for(String key: mImageCache.keySet()) {
        tmp = mImageCache.get(key).get();
        if(tmp!=null) {
                tmp.recycle();
                tmp = null;
        }
}
mImageCache.clear();

In both cases I can get up to around 40 or so 280x320 jpg's loaded
before I crash (on a Droid).

Thanks again!

On Jan 19, 9:02 pm, Romain Guy <[email protected]> wrote:
> The easiest thing to do is to create a LRU cache for your images,
> using SoftReferences. You can find an example of such a cache in my
> app Shelves: code.google.com/p/shelves. I use this technique to load
> hundreds of book covers.
>
>
>
>
>
> On Tue, Jan 19, 2010 at 8:53 PM, Nerdrow <[email protected]> wrote:
> > I've read in a few posts that there was an bug in the how the Gallery
> > widget recycles views, that it was targeted for Eclair, but it looks
> > like it's still an issue.  I'm trying to use an extended Gallery (all
> > I do is override getChildStaticTransformation).  The Gallery items are
> > all simple custom views that simply draw a FastBitmapDrawable, the
> > images are pre-scaled, very lean and very basic.  All items are
> > 280x320 jpg's, so not huge individually.
>
> > As I scroll through the gallery, I get to the same point each time and
> > when I call BitmapFactory.decodeFlie(path) I get the "bitmap size
> > exceeds VM budget" error.  I've offset the start point in the list and
> > it's not a specific image causing the issue, it's when I've decoded a
> > certain number of images from disk.  The other posts indicate that
> > it's cumulative; multiple bitmaps are being held in memory instead of
> > being recycled, and I assumed this was because the Gallery views
> > aren't being recycled properly and hanging on to their bitmaps.  I've
> > tried paging the image set to 10-20 images at a time, this works but
> > after a few pages I get the same exception.
>
> > Is there any workaround for this?  Even if it's addressed in an
> > update, it's not really viable to wait since all phones aren't going
> > to just instantly upgrade once an upgrade is released (judging by how
> > slowly 2.0/2.1 is rolling out).  Without a fix or workaround, the
> > Gallery seems pretty much useless for images (unless there's only a
> > few).
>
> > Thanks in advance.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Romain Guy
> Android framework engineer
> [email protected]
>
> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to