0

我有一个从服务器加载图像的 android 应用程序。服务器以 XML 格式发送 base64 格式的图像。当我将此 base64 解码为字节数组时,出现内存不足错误。这发生在少数堆内存较低的设备上。在这种情况下有什么解决方法?

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

错误日志说

{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)}
4

1 回答 1

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.

于 2013-05-22T16:28:52.837 回答