Roskvist
Roskvist

Reputation: 1040

Android bitmap out of memory error (mapviewBalloons)

I get the famous out of memory error. But i have tried many of the suggested solutions on this problem without any luck. I understand that to prevent a bitmap to exceed the memory, you make a static variable of the drawable (Android docs). But that isn't working in my app because i have so many markers as you can see..

Does anyone have a suggestion to a solution?

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);

ERROR:

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

EDIT:

I think i maybe have localized the problem.. I can trigger that outOfmemory exception if i click on several of the annotations. I use the mapViewBalloons from Here, and when i have open closed 2 several times, my app crashes with the exception. Anyone have similar problem?

Upvotes: 0

Views: 1167

Answers (2)

Fabian
Fabian

Reputation: 2713

There are a lot of ways to solve the out of memory exeption. Try to set inPurgeable at your BitmapFactory.Options http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPurgeable

or try a PurgeableBitmap. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/PurgeableBitmap.html

Scale Images to smaller size. If you have a view with an adapter make a list with FIFO and fixed size...

Upvotes: 1

Sebastian Breit
Sebastian Breit

Reputation: 6159

I had the same problem and the following code worked for me:

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;
    }

Upvotes: 1

Related Questions