0

I've an android application which is loading images from server. Server sends images in base64 format in a XML. When I am decoding this base64 into byte array I am getting out of memory error. This is happening on few devices which have lower heap memory. What is work around in this case?

else if (this.returnClass == byte[].class) {
            if (!xr.isEmptyElementTag()) {
                retVal = Base64.decode(xr.nextText(), Base64.NO_WRAP);
                System.gc();
                xr.nextTag();
            }

Error log says

{05-22 10:19:47.474: D/dalvikvm(2421): GC_FOR_MALLOC freed 0 objects / 0 bytes in 55ms
05-22 10:19:47.474: E/dalvikvm-heap(2421): Out of memory on a 5095368-byte allocation.
05-22 10:19:47.474: I/dalvikvm(2421): "ModernAsyncTask #5" prio=5 tid=14 RUNNABLE
05-22 10:19:47.474: I/dalvikvm(2421):   | group="main" sCount=0 dsCount=0 s=N obj=0x4481c6f0 self=0x300de8
05-22 10:19:47.474: I/dalvikvm(2421):   | sysTid=2546 nice=10 sched=0/0 cgrp=bg_non_interactive handle=3149608
05-22 10:19:47.474: I/dalvikvm(2421):   | schedstat=( 282605471378 64045071861 48652 )
05-22 10:19:47.474: I/dalvikvm(2421):   at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:~45)
05-22 10:19:47.474: I/dalvikvm(2421):   at java.nio.ReadWriteHeapByteBuffer.<init>(ReadWriteHeapByteBuffer.java:47)
05-22 10:19:47.474: I/dalvikvm(2421):   at java.nio.BufferFactory.newByteBuffer(BufferFactory.java:49)
05-22 10:19:47.474: I/dalvikvm(2421):   at java.nio.ByteBuffer.allocate(ByteBuffer.java:52)
05-22 10:19:47.474: I/dalvikvm(2421):   at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:317)
05-22 10:19:47.474: I/dalvikvm(2421):   at java.nio.charset.Charset.encode(Charset.java:692)
05-22 10:19:47.474: I/dalvikvm(2421):   at java.lang.String.getBytes(String.java:903)
05-22 10:19:47.474: I/dalvikvm(2421):   at android.util.Base64.decode(Base64.java:118)}
NinjaCoder
  • 2,381
  • 3
  • 24
  • 46

1 Answers1

0

It sounds like you just need to downsample the images as you downalod them. There are a number of posts about this on S/O with sample code like this one here. Just poke around and you'll find what you need.

Another tip - save the URLs to the images and only display/downalod the images you need. This saves a ton of memory and makes it really easy to get the images later as they are required.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
  • 1
    I cannot downsample image while decoding base64. Right? I know once I have byte array I can downsample it using BitmapFactory.Options – NinjaCoder May 22 '13 at 16:34
  • Right - you do it as you decode like in [this example](http://stackoverflow.com/questions/8587787/encoded-byte64-image-causes-outofmemoryerror-android) – Rarw May 22 '13 at 18:00
  • but my code doesn't even reaches till bitmap decoding part. It crashes while decoding base64 only. – NinjaCoder May 22 '13 at 18:12
  • Can you post that section of your code? Its very odd that one base64 string will cause an OOM exception by itself. – Rarw May 22 '13 at 18:22