内存不足错误 - 在Google Map中添加标记并制作位图时

时间:2014-08-29 21:36:16

标签: android out-of-memory google-maps-android-api-2 android-bitmap

当地图加载但在将地图位置移动到其他位置并且出现新标记后,不会发生此错误...

这是Stacktrace:

08-29 15:11:38.061: E/art(6669): Throwing OutOfMemoryError "Failed to allocate a 4194316 byte allocation with 3486016 free bytes"
08-29 15:11:38.201: E/AndroidRuntime(6669): FATAL EXCEPTION: GLThread 2298
08-29 15:11:38.201: E/AndroidRuntime(6669): Process: com.---.---, PID: 6669
08-29 15:11:38.201: E/AndroidRuntime(6669): java.lang.OutOfMemoryError: Failed to allocate a 4194316 byte allocation with 3486016 free bytes
08-29 15:11:38.201: E/AndroidRuntime(6669):     at android.graphics.Bitmap.nativeCreate(Native Method)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at android.graphics.Bitmap.createBitmap(Bitmap.java:810)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at android.graphics.Bitmap.createBitmap(Bitmap.java:754)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at opl.a(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at opp.a(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at opp.a(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at opp.b(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at oow.a(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at ope.a(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at okm.a(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at ojx.a(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at ojx.b(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at otq.k(Unknown Source)
08-29 15:11:38.201: E/AndroidRuntime(6669):     at otq.run(Unknown Source)

以下是我创建位图的方法:

   public Bitmap loadBitmapFromView(View v) {

        try {
            if (v.getMeasuredHeight() <= 0) {
                v.measure(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
                  Bitmap bActual = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
            Bitmap b = Bitmap.createScaledBitmap(bActual, 120, 120, true);
                Canvas c = new Canvas(b);
                v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
                v.draw(c);
                return b;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return null;
    }

在我的AsyncTask中的doInBackground中调用它,如:

  MarkerOptions marker = new MarkerOptions()
                        .position(LOC)
                        .snippet(user)
                        .title(name)
                        .icon(BitmapDescriptorFactory.fromBitmap(loadBitmapFromView(v1)));

onPostExecute中,我添加了数据库找到的尚未显示的新标记。

            for (int i = 0; i < markers.get(i).nl.size() - 1; i++) {
                Marker m = googleMap.addMarker(markers.get(i).mO.get(i));
                markerIdMap.put(m, markers.get(i).nl.get(i));
            }

以下是我设置地图的方式:

其他花絮:

    googleMap.setClustering(new ClusteringSettings().enabled(false)
            .addMarkersDynamically(true));

    googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cp) {

            LOC = cp.target;
            MainActivity.mLongitude = LOC.longitude;
            MainActivity.mLatitude = LOC.latitude;

            if (!running) {
                new NearLocationsTask().execute();
            }
        }
    });

我正在使用android-map-extensions

1 个答案:

答案 0 :(得分:1)

我不确定你是否真的要创建一个空的位图并将其缩放:

位图bActual = Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(),Bitmap.Config.ARGB_8888);

位图b = Bitmap.createScaledBitmap(bActual,120,120,true);

如果你有很多标记,也许你正在尝试分配太多的位图。