Android creating BitmapDescriptor exception

前端 未结 4 1642
孤街浪徒
孤街浪徒 2021-01-06 03:01

I\'m writing an application that works alot with google map and markers on it. My task is to create and display some amount of markers on google map. Markers have custom ima

相关标签:
4条回答
  • 2021-01-06 03:26

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

    0 讨论(0)
  • 2021-01-06 03:27

    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.

    0 讨论(0)
  • 2021-01-06 03:28

    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) {  }
              });
    
    0 讨论(0)
  • 2021-01-06 03:40

    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

    0 讨论(0)
提交回复
热议问题