0

我得到了著名的内存不足错误。但是我在这个问题上尝试了许多建议的解决方案,但没有任何运气。我知道为了防止位图超出内存,您可以创建一个可绘制的静态变量(Android 文档)。但这在我的应用程序中不起作用,因为您可以看到我有很多标记..

有人对解决方案有建议吗?

for(Poi p : poiarray){

                WeakReference<Bitmap> bitmap = new WeakReference<Bitmap>(p.get_poiIcon());
                if(bitmap!=null){

                    Drawable marker = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap.get(), 60, 60, true));
                annotationOverLays.add(new CustomAnnotation(marker,p,this,mapView));    
                //mapView.getOverlays().add(new CustomAnnotation(marker,p,this,mapView));   
                }
            }
            mapView.getOverlays().addAll(annotationOverLays);

错误:

05-23 13:08:31.436: E/dalvikvm-heap(22310): 20736-byte external allocation too large for this process.
05-23 13:08:31.436: E/dalvikvm(22310): Out of memory: Heap Size=23111KB, Allocated=22474KB, Bitmap Size=1505KB
05-23 13:08:31.436: E/GraphicsJNI(22310): VM won't let us allocate 20736 bytes

编辑:

我想我可能已经将问题本地化了。如果我单击几个注释,我可以触发内存不足异常。我使用Here的 mapViewBalloons ,当我打开关闭 2 次时,我的应用程序崩溃但异常。有人有类似的问题吗?

4

2 回答 2

1

我遇到了同样的问题,以下代码对我有用:

public static Bitmap decodeFile(File f, boolean goodQuality){
        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=100;

            //Find the correct scale value. It should be the power of 2.
            int scale=1;
            if(!goodQuality){
                while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
                    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;
    }
于 2012-05-23T11:19:53.287 回答
1

有很多方法可以解决内存不足异常。尝试在您的 BitmapFactory.Options http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPurgeable设置 inPurgeable

或尝试使用 PurgeableBitmap。 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/PurgeableBitmap.html

将图像缩放到更小的尺寸。如果您有一个带有适配器的视图,请使用 FIFO 和固定大小制作一个列表...

于 2012-05-23T11:27:33.923 回答