Android OutOfMemoryError位图大小超出VM预算
我有一个应用程序,我在其中读取一些gps数据,并将它们显示在地图上,我还在该位置添加了一个覆盖图,这是一个小的Android OutOfMemoryError位图大小超出VM预算,android,memory,drawable,Android,Memory,Drawable,我有一个应用程序,我在其中读取一些gps数据,并将它们显示在地图上,我还在该位置添加了一个覆盖图,这是一个小的.png图像 我在异步任务线程中读取数据,在onProgressUpdate方法中,我通过在新位置添加覆盖来更新GUI。这是我的密码: Drawable marker; public void onCreate(Bundle savedInstanceState) { marker=getResources().getDrawable
.png
图像
我在异步任务
线程中读取数据,在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]);
}
}
我试图回收
我的drawable,但还是出现了错误,过了一段时间,我发现:
//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;
}
//解码图像并对其进行缩放以减少内存消耗
私有位图解码文件(文件f){
试一试{
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码流(新的FileInputStream(f),null,o);
//我们要扩展到的新尺寸
所需的最终int_尺寸=70;
//找到正确的刻度值。它应该是2的幂。
内部宽度=o.向外宽度,高度=o.向外高度;
int标度=1;
while(true){
if(width_tmp/2Android多年来一直存在一个巨大的问题。Bitmap类、getDrawable函数等依赖于底层的Bitmap.h,它是NDK的一部分。每当getDrawable返回位图时,由于某种原因,Android无法在我们不再需要它时正确释放内存
我使用它在某处创建位图的单个静态实例,并在需要时弱引用它。这样,至少会将位图的一个实例加载到内存中
希望这有帮助。你能给我看一点你的代码,看看你是怎么做到的吗?因为我是编程新手,这对我来说并不容易。谢谢:)你可能想看看这个-:)
//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;
}