gpt4 book ai didi

Android OutOfMemoryError 位图大小超出 VM 预算

转载 作者:太空狗 更新时间:2023-10-29 13:40:30 25 4
gpt4 key购买 nike

我有一个应用程序,我在其中读取了一些 gps 数据并将它们显示在 map 上,我还在那个位置添加了一个叠加层,这是一个小的 .png 图像。

我在 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]);
}
}

所以我读取了一个新位置,并在该位置添加了一个叠加层。为此,我使用了扩展 Overlay 的类 SitesOverlay

一切顺利,直到我收到以下异常:

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)

我尝试回收我的可绘制对象,但我仍然收到该错误,过了一会儿我发现了这个:

//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

但是我在理解上遇到了一些问题:

*首先,为什么使用 File f 作为参数?这是我想使用的可绘制对象吗?每次我使用该可绘制对象时都必须这样做还是仅在 onCreate() 中完成?

因此,如果有人可以解释如何使用它,我将不胜感激,或者如果您有更简单的工作解决方案,那就更好了。

最佳答案

长期以来,这一直是 Android 的一个大问题。 Bitmap 类、getDrawable 函数等依赖于作为 NDK 的一部分提供的底层 bitmap.h。每当 getDrawable 返回位图时,由于某些原因,当我们不再需要时,Android 无法正确释放内存。

一种变通方法我用它在某处创建位图的单个静态实例,并在需要时对其进行弱引用。这样至少将位图的一个实例加载到内存中。

希望这对您有所帮助。

关于Android OutOfMemoryError 位图大小超出 VM 预算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6402858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com