我的应用程序以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
答案 0 :(得分:-1)
您必须对图像进行缩减采样。请记住,图像将使用大约H x W x 4 bytes
的内存来显示,因此“文件大小”并不真正反映内存中的实际大小。您必须对图像进行缩减采样,以使其仅适合屏幕中所需的大小。