Android内存不足错误:位图超出了vm预算

时间:2013-05-23 05:55:15

标签: google-maps polygon populate

我正在使用此代码创建文本位图,以便在Google地图中显示多边形的网格数。

我多次调用 txtToBitmap 方法,比如在谷歌地图中,​​项目中可能有350个网格,当我点击项目时,我调用此方法350次,点击项目后10次​​或更多次,堆空间增加,它让我误以下...我正在搜索这个6天,任何人都可以帮忙。

public void txtToBitmap(String text,double middlelatitude, double middlelongitude){
        //String text = GridsXY.get(forMarker-1);//"Hello world!";
        Bitmap b= Bitmap.createBitmap(60, 60, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        c.drawBitmap(b, 0, 0, null);
        TextPaint textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(16.0F);
        textPaint.setColor(Color.RED);
        StaticLayout sl= new StaticLayout(text, textPaint, b.getWidth()-8, Alignment.ALIGN_CENTER, 0.0f, 0.0f, false);
        c.translate(6, 40);
        sl.draw(c);
        GridMarker=mMap.addMarker(new MarkerOptions().position(new LatLng(middlelatitude, middlelongitude))
                .icon(BitmapDescriptorFactory.fromBitmap(b)));
        b.recycle();
        b=null;
}

logcat的

05-22 19:44:10.667: E/dalvikvm-heap(16826): 14400-byte external allocation too large for this process.
05-22 19:44:10.667: E/GraphicsJNI(16826): VM won't let us allocate 14400 bytes
05-22 19:44:10.667: D/AndroidRuntime(16826): Shutting down VM
05-22 19:44:10.667: W/dalvikvm(16826): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
05-22 19:44:10.679: E/AndroidRuntime(16826): FATAL EXCEPTION: main
05-22 19:44:10.679: E/AndroidRuntime(16826): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-22 19:44:10.679: E/AndroidRuntime(16826):    at     android.graphics.Bitmap.nativeCreate(Native Method)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at com.commdex.catp.TestArea.txtToBitmap(TestArea.java:847)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at com.commdex.catp.TestArea.showpolygon(TestArea.java:817)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at com.commdex.catp.TestArea.showGridsFromDB(TestArea.java:696)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at com.commdex.catp.TestArea.onResume(TestArea.java:580)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.Activity.performResume(Activity.java:3823)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.os.Looper.loop(Looper.java:123)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at java.lang.reflect.Method.invoke(Method.java:521)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
05-22 19:44:10.679: E/AndroidRuntime(16826):    at dalvik.system.NativeStart.main(Native Method)


05-23 11:05:23.416: E/AndroidRuntime(6904): FATAL EXCEPTION: GLThread 19898
05-23 11:05:23.416: E/AndroidRuntime(6904): java.lang.OutOfMemoryError
05-23 11:05:23.416: E/AndroidRuntime(6904):     at org.apache.harmony.luni.platform.OSMemory.malloc(Native Method)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at org.apache.harmony.luni.platform.PlatformAddressFactory.alloc(PlatformAddressFactory.java:150)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:66)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at java.nio.ReadWriteDirectByteBuffer.<init>(ReadWriteDirectByteBuffer.java:51)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at java.nio.BufferFactory.newDirectByteBuffer(BufferFactory.java:93)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:68)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.aa.n.a(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.aa.n.c(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.aa.q.e(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.aa.q.d(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.ab.k.b(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.as.aw.a(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.w.g.a(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.w.g.b(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.q.p.l(Unknown Source)
05-23 11:05:23.416: E/AndroidRuntime(6904):     at maps.q.p.run(Unknown Source)

为什么用oom错误强制关闭,我看到堆空间不断增加

1 个答案:

答案 0 :(得分:2)

我无法解决这个问题,但尝试了解决方法。我创建了60 * 60缩小其质量的位图,并通过杀死应用程序在低内存()上处理,它现在可以工作,但是等待更好的解决方法。