1

我正在使用此代码创建文本位图以在谷歌地图中显示多边形的网格编号。

我多次调用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;
}

日志猫

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 错误而强制关闭,我看到堆空间在不断增加

4

1 回答 1

2

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

于 2014-02-18T12:00:36.517 回答