VectorDrawable with GoogleMap BitmapDescriptor

I have a problem with google maps BitmapDescriptor when creating an icon for MarkerOptions using VectorDrawable , API 5.0 +

Method used to create:

 @NonNull private BitmapDescriptor getBitmapDescriptor(int id) { return BitmapDescriptorFactory.fromResource(id); } 

Everything works fine when the id argument contains png drawable, however if I try it with the VectorDrawable defined in xml, the application always crashes when googleMap.addMarker(marker) ( BitmapDescriptor not null).

 11-05 10:15:05.213 14536-14536/xxx.xxxxx.app E/AndroidRuntime: FATAL EXCEPTION: main Process: xxx.xxxxx.app, PID: 14536 java.lang.NullPointerException at com.google.aaae.a(Unknown Source) at com.google.maps.api.android.lib6.d.dn.<init>(Unknown Source) at com.google.maps.api.android.lib6.d.dm.a(Unknown Source) at com.google.maps.api.android.lib6.d.ag.<init>(Unknown Source) at com.google.maps.api.android.lib6.d.eu.a(Unknown Source) at com.google.android.gms.maps.internal.j.onTransact(SourceFile:167) at android.os.Binder.transact(Binder.java:380) at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source) at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source) at xxx.xxxxx.app.ui.details.DetailActivity.lambda$initGoogleMaps$23(DetailActivity.java:387) at xxx.xxxxx.app.ui.details.DetailActivity.access$lambda$10(DetailActivity.java) at xxx.xxxxx.app.ui.details.DetailActivity$$Lambda$13.onMapReady(Unknown Source) at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) at com.google.android.gms.maps.internal.zzl$zza.onTransact(Unknown Source) at android.os.Binder.transact(Binder.java:380) at com.google.android.gms.maps.internal.av.a(SourceFile:82) at com.google.maps.api.android.lib6.d.fa.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

It doesn't matter how I extract the drawable, tried to create a bitmap using BitmapFactory.fromResources and later BitmapDescritpionFactory.fromBitmap , but the results are the same. It just won't work with a vector. I tried different vectors, but it seems that the problem with the vector is not a problem.

Does anyone know how to fix this error?

@edit

It seems that the problem was not in BitmapDescriptior itself, but in the loading of VectorDrawable , which returned the wrong bitmap. However, the solution proposed in response is still excellent.

+26
android vector-graphics google-maps
Nov 05 '15 at 15:24
source share
5 answers

Possible workaround:

 private BitmapDescriptor getBitmapDescriptor(int id) { Drawable vectorDrawable = context.getDrawable(id); int h = ((int) Utils.convertDpToPixel(42, context)); int w = ((int) Utils.convertDpToPixel(25, context)); vectorDrawable.setBounds(0, 0, w, h); Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bm); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bm); } 
+39
Nov 05 '15 at 16:55
source share

According to the bug report ( published by vaughandroid - thanks!), Using VectorDrawable so far will not be supported. See the comment in the error report for more information.

Here's a workaround from the Google Maps team:

 /** * Demonstrates converting a {@link Drawable} to a {@link BitmapDescriptor}, * for use as a marker icon. */ private BitmapDescriptor vectorToBitmap(@DrawableRes int id, @ColorInt int color) { Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null); Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); DrawableCompat.setTint(vectorDrawable, color); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bitmap); } 

used as follows:

 // Vector drawable resource as a marker icon. mMap.addMarker(new MarkerOptions() .position(ALICE_SPRINGS) .icon(vectorToBitmap(R.drawable.ic_android, Color.parseColor("#A4C639"))) .title("Alice Springs")); 

Vector tinting is a bonus

+8
May 23 '17 at 16:34
source share

Here is another link: http://qiita.com/konifar/items/aaff934edbf44e39b04a

 public class ResourceUtil { @TargetApi(Build.VERSION_CODES.LOLLIPOP) private static Bitmap getBitmap(VectorDrawable vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; } private static Bitmap getBitmap(VectorDrawableCompat vectorDrawable) { Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return bitmap; } public static Bitmap getBitmap(Context context, @DrawableRes int drawableResId) { Drawable drawable = ContextCompat.getDrawable(context, drawableResId); if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof VectorDrawableCompat) { return getBitmap((VectorDrawableCompat) drawable); } else if (drawable instanceof VectorDrawable) { return getBitmap((VectorDrawable) drawable); } else { throw new IllegalArgumentException("Unsupported drawable type"); } } } 
+6
Jul 07 '16 at 13:02
source share

VectorDrawable to BitmapDescriptor without hue

 private BitmapDescriptor getBitmapDescriptor(@DrawableRes int id) { Drawable vectorDrawable = ResourcesCompat.getDrawable(getResources(), id, null); Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); return BitmapDescriptorFactory.fromBitmap(bitmap); } 

Thanks @lbarbosa

+3
Jun 12 '17 at 18:07 on
source share

same on Kotlin

 private fun getBitmapDescriptorFromVector(id: Int, context: Context): BitmapDescriptor { var vectorDrawable: Drawable = context.getDrawable(id) var h = (24 * getResources().getDisplayMetrics().density).toInt(); var w = (24 * getResources().getDisplayMetrics().density).toInt(); vectorDrawable.setBounds(0, 0, w, h) var bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888) var canvas = Canvas(bm) vectorDrawable.draw(canvas) return BitmapDescriptorFactory.fromBitmap(bm) } 
0
Oct 11 '18 at 16:51
source share



All Articles