How To Handle Loading Many Images From JSON

CodeMonkeyCodeMonkey GBMember ✭✭✭

Hi all

I am pulling down JSON data which is being displayed in a list, each item has a thumbnail image. Previously I had been decoding the images to Bitmaps in the List adapters GetView() function but found that this was affecting the scroll performance of my list.

I then tried to load all the images at the time of deserializationthe JSON (which happens on a different thread) but I am getting this error:

10-31 15:42:55.137 E/dalvikvm(16261): Out of memory: Heap Size=6279KB, Allocated=3837KB, Bitmap Size=26616KB, Limit=32768KB
10-31 15:42:55.137 E/dalvikvm(16261): Trim info: Footprint=6279KB, Allowed Footprint=6279KB, Trimmed=516KB
10-31 15:42:55.137 E/GraphicsJNI(16261): VM won't let us allocate 184800 bytes
Exception of type 'Java.Lang.OutOfMemoryError' was thrown.

I understand why this exception is occurring but don't know what pattern I can use to solve the issue. This is the code I'm using to decode my images:

private static Bitmap GetImageFromURL(string pURL)
        {
            URL url = new URL(pURL);
            Bitmap bmp = BitmapFactory.DecodeStream(url.OpenConnection().InputStream);
            return bmp;
        }

How do I handle this situation without affecting the scroll performance of my list? Am I going to need to store the images on the SD card?

Any help appreciated.

Posts

  • CodeMonkeyCodeMonkey GBMember ✭✭✭

    Think we've decided to pull down two version of the image, thumbnail and full size, hopefully get around the issue with less hassle.

  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    Storing the images on the SD card might speed up your app for the future.

    Related to this, it's not uncommon to get OutOfMemory errors when working with images - in a lot of cases they can require more heap than you'd think. You can dynamically scale down an image on disk so that it won't consume as much ram.

    We'e got a recipe for that : Loading Large Bitmaps Efficiently which might be of interest.

  • CodeMonkeyCodeMonkey GBMember ✭✭✭

    @topgenorth That's great, had missed that recipe! Gonna tinker with it today and see if I can't get it working without needing the second image.

  • TomOpgenorthTomOpgenorth CAXamarin Team Xamurai

    @CodeMonkey - you also might want to look at using something like the LruCache for caching the thumbnails after you create them.

Sign In or Register to comment.