0
當地圖加載但地圖位置移動到其它位置和新的標誌出現後...內存不足錯誤 - 當添加在GoogleMap的標記,使位圖
這裏是堆棧跟蹤不會發生此錯誤:
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;
}
這就是所謂的我的doInBackground在我AsyncTask
,如:
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
我認爲你是對的。我的代碼中存在錯誤的邏輯,它會爲每個相機更改添加相同的標記。不只是新的一個。可能會非常快速地增加資源。 – KickingLettuce 2014-08-30 01:33:54
前段時間我有同樣的問題,如果你只是添加相同的標記而不刪除以前的標記,那麼由於內存不足很容易導致應用程序崩潰。 – 2014-08-30 17:27:34