我有一個應用程序,其中我讀一些GPS數據和I顯示它們在地圖上,我也在該位置這是小.png
圖像添加的疊加。的Android的OutOfMemoryError位圖大小超過VM預算
我正在讀取Async Task
線程中的數據和onProgressUpdate
方法我通過在新位置添加疊加層來更新我的GUI。這裏是我的代碼:
Drawable marker;
public void onCreate(Bundle savedInstanceState) {
marker=getResources().getDrawable(R.drawable.curent_loc);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
}
在這裏我讀的GPS數據:
public class InitTask extends AsyncTask<Void, GeoPoint, Void> {
protected Void doInBackground(Void... voids) {
p = new GeoPoint(latitude, longitude);
publishProgress(p);
Thread.sleep(1500);
}
/*in here I update my GUI*/
protected void onProgressUpdate(GeoPoint... progress1) {
mapView.getOverlays().add(new SitesOverlay(marker,progress1[0]));
theRouteDraw(progress1[0]);
}
}
所以我讀了一個新的位置,我在那個位置上添加一個覆蓋。對於我用我的課SitesOverlay
擴展Overlay
。
一切順利,直到在某個時候,我收到以下異常:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:392)
at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:405)
at com.google.android.maps.StreetViewRenderer.getOverlay(StreetViewRenderer.java:149)
com.google.android.maps.StreetViewRenderer.renderTile(StreetViewRendere
at com.google.android.maps.AndroidTileOverlayRenderer.renderTile(AndroidTileOverlayRenderer.java:62)
at com.google.googlenav.map.Map.drawTile(Unknown Source)
at com.google.googlenav.map.Map.drawMapBackground(Unknown Source)
at com.google.googlenav.map.Map.drawMap(Unknown Source)
at com.google.android.maps.MapView.drawMap(MapView.java:1029)
at com.google.android.maps.MapView.onDraw(MapView.java:468)
at android.view.View.draw(View.java:6535)
at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
我試圖recycle
我繪製的,但我仍然得到這個錯誤,並在一段時間後,我發現這一點:
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
我發現,在這裏: Strange out of memory issue while loading an image to a Bitmap object
但我理解的幾個問題我T:
*首先,爲什麼是使用File f
作爲參數?這是我想使用的drawable嗎?每次我使用該drawable或僅在onCreate()
時,這個必須完成嗎?
因此,如果有人可以解釋如何使用,我將感激之餘,或者如果你有一個簡單可行的解決方案會更加美好。
你能告訴我一點你的代碼,看看你是怎麼做到的嗎?因爲我是編程新手,這對我來說並不是很容易。謝謝:) – adrian
你可能想檢查一下 - http://stackoverflow.com/questions/4197794/android-custom-view-bitmap-memory-leak :) –