我的应用程序从服务器接收图像数据作为 XML。在解析 XML 期间,我使用 StringBuilder 附加数据,解码图像数据,然后显示图像。如果图像大小大于 1MB,我会得到 Dalvik-vm “Out Of Memory” 异常,如下所示:
07-16 19:23:35.376: E/dalvikvm-heap(374): Out of memory on a 10501076-byte allocation.
07-16 19:23:35.376: I/dalvikvm(374): "AsyncTask #2" prio=5 tid=10 RUNNABLE
07-16 19:23:35.376: I/dalvikvm(374): | group="main" sCount=0 dsCount=0 obj=0x405e3238 self=0x38ff88
07-16 19:23:35.376: I/dalvikvm(374): | sysTid=434 nice=10 sched=0/0 cgrp=bg_non_interactive handle=3735744
07-16 19:23:35.376: I/dalvikvm(374): | schedstat=( 3989536323 3728953745 437 )
07-16 19:23:35.376: I/dalvikvm(374): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:~95)
07-16 19:23:35.376: I/dalvikvm(374): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:153)
07-16 19:23:35.376: I/dalvikvm(374): at java.lang.StringBuilder.append(StringBuilder.java:217)
07-16 19:23:35.376: I/dalvikvm(374): at os.android.lib.asgsuitesdk.XmlReader.characters(XmlReader.java:121)
07-16 19:23:35.376: I/dalvikvm(374): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:165)
07-16 19:23:35.376: I/dalvikvm(374): at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
07-16 19:23:35.376: I/dalvikvm(374): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:518)
07-16 19:23:35.376: I/dalvikvm(374): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
07-16 19:23:35.376: I/dalvikvm(374): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
07-16 19:23:35.376: I/dalvikvm(374): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
07-16 19:23:35.376: I/dalvikvm(374): at javax.xml.parsers.SAXParser.parse(SAXParser.java:390)
无论如何,我可以摆脱这个异常吗?我知道,取决于设备,为 Android 应用程序分配的堆内存至少为 16MB。在我的情况下,图像大小在 1MB ~ 2MB 之间,它会崩溃。非常感谢任何帮助。
谢谢, DK