Android creating BitmapDescriptor exception

跟風遠走 提交于 2019-11-30 20:58:16

Well here is what i got. Looks like BitmapFactory can't create Bitmap if it don't have enought memory. So if GC didn't do job and u don't have enough memory u'll get this exception. In my case that was pretty often because i need to generate about 10-20 markers every time user moves google map camera.

First of all don't be stupid like me and don't use android-maps-utils just for IconGenerator :) I wrote my own class that generate's BitmapDescriptor from Bitmap and caches it in LruCache. Here's good tutorial for caching Bitmaps. You can do almost the same for BitmapDescriptor. Pay attention to LruCache size. You can't get BitmapDescriptor size in bytes, so you need to think about amount of these objects in LruCache. Just look at your bitmap size and do some calculations.

If you need text in your image do something like this:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.mark_active_grey).copy(Bitmap.Config.ARGB_8888, true);

    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(offersCount,
            canvas.getWidth()/2,
            canvas.getHeight()/2 - ((clustersPaint.getFontMetrics().ascent + clustersPaint.getFontMetrics().descent) / 2) ,
            clustersPaint);

Sorry for bad english and i hope this information will be useful to some one.

I had same problem. With Kuva's answer I make a new class like this:

public class MapBmpContainter
{
    private int mBmpSize;
    public BitmapDescriptor mBmpDescriptor;

    public MapBmpContainter(Bitmap bmp)
    {
        mBmpSize=bmp.getByteCount()/1014;
        mBmpDescriptor= BitmapDescriptorFactory.fromBitmap(bmp);
    }

    public int getSize()
    {
        return mBmpSize;
    }
}

I cache new class object in LruCache instead of Bitmap. Same with Kuva I think Bitmap and BitmapDescriptor almost same size. And It worked

For me the problem was related to the size of the icon. I just change dynamically the size of the Bitmap and works fine.

Bitmap image2 = Bitmap.createScaledBitmap(iconBitmap, 120, 120, false);

Use Picasso , Glide or Fresco Literary to cache bitmaps efficiently.

 Picasso.with(getContext())
   .load(R.drawable.marker)
   .resize(width, width)
    .into(new Target() {
   @Override
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
   markerOptionsHome = new MarkerOptions();
   markerOptionsHome.title("Home location");
   markerOptionsHome.snippet("");
   markerOptionsHome.position(latlng);
   markerOptionsHome.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
   homeLocationMarker = map.addMarker(markerOptionsHome);

            }

    @Override
   public void onBitmapFailed(Drawable errorDrawable) { }
     @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {  }
          });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!