自定义标记位图错误无法解码图像。提供的图像必须是位图
Custom marker bitmap error Failed to decode image. The provided image must be a Bitmap
我正在使用 firebase 实时数据库获取一些用户信息并为每个用户显示一个自定义标记。我使用下面的方法为标记选项设置图标,它适用于大多数情况。
但更常见的是,在为标记创建自定义视图的位图时,应用程序会崩溃......!有没有办法改进方法,防止位图问题!
代码
{ // other methods..
private void showMarker() {
BitmapDescriptor pin = BitmapDescriptorFactory
.fromBitmap(getMarkerBitmapFromView(data.getUserAvatar()));
userMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(data.getName() + "")
.snippet(data.getSectionName() + "")
.icon(pin));
}
// I take pic url from firebase and show it into the marker view
private Bitmap getMarkerBitmapFromView(String ImgUrl) {
if (getActivity() != null) {
View customMarkerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker, null);
CircleImageView markerImageView = customMarkerView.findViewById(R.id.pic_user);
FrameLayout pin = customMarkerView.findViewById(R.id.marker);
pin.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.map_marker_green));
Picasso.with(getActivity())
.load(ImgUrl)
.resize(80, 80)
.error(R.drawable.user_dummy)
.placeholder(R.drawable.user_dummy)
.into(markerImageView);
customMarkerView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
customMarkerView.buildDrawingCache(true);
Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, Mode.SRC_IN);
Drawable drawable = customMarkerView.getBackground();
if (drawable != null) {
drawable.draw(canvas);
}
customMarkerView.draw(canvas);
return returnedBitmap;
} else {
// the crash didn't happen because of this! I've checked using breakpoints, it returns bitmap 99.9% of times.
return null;
}
}
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.arapeak.katateeb, PID: 30706
com.google.maps.api.android.lib6.common.apiexception.b: Failed to decode image. The provided image must be a Bitmap.
at com.google.maps.api.android.lib6.impl.k.a(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):5)
at com.google.maps.api.android.lib6.impl.o.a(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):7)
at com.google.maps.api.android.lib6.impl.db.<init>(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):25)
at com.google.maps.api.android.lib6.impl.bc.a(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):496)
at com.google.android.gms.maps.internal.l.onTransact(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):94)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.internal.zzeu.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzg.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
你好,我用过这个,对我来说效果很好
//generate bit map from view
private Bitmap createBitMap(View v) {
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
if (b != null) {
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
}
return b;
}
// created user marker with his image
private void addMyLocationMarker() {
View view = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.layout_marker, null, false);
view.setLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
if (userMarker != null)
userMarker = googleMap
.addMarker(new MarkerOptions()
.anchor(0.5F, 0.5F)
.position(latlng)
.icon(BitmapDescriptorFactory.fromBitmap(createBitMap(bindingUserMarker.getRoot()))));
userMarker.setZIndex(2f);
updateUserMarkerImage(view);
}
}
//update user image
private void updateUserMarkerImage(View view ) {
ImageView imageView=view.findViewById(R.id.image);
//load your image using any image loader and recreate marker with your view and set it marker
}
我正在使用 firebase 实时数据库获取一些用户信息并为每个用户显示一个自定义标记。我使用下面的方法为标记选项设置图标,它适用于大多数情况。
但更常见的是,在为标记创建自定义视图的位图时,应用程序会崩溃......!有没有办法改进方法,防止位图问题!
代码
{ // other methods..
private void showMarker() {
BitmapDescriptor pin = BitmapDescriptorFactory
.fromBitmap(getMarkerBitmapFromView(data.getUserAvatar()));
userMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title(data.getName() + "")
.snippet(data.getSectionName() + "")
.icon(pin));
}
// I take pic url from firebase and show it into the marker view
private Bitmap getMarkerBitmapFromView(String ImgUrl) {
if (getActivity() != null) {
View customMarkerView = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker, null);
CircleImageView markerImageView = customMarkerView.findViewById(R.id.pic_user);
FrameLayout pin = customMarkerView.findViewById(R.id.marker);
pin.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.map_marker_green));
Picasso.with(getActivity())
.load(ImgUrl)
.resize(80, 80)
.error(R.drawable.user_dummy)
.placeholder(R.drawable.user_dummy)
.into(markerImageView);
customMarkerView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
customMarkerView.buildDrawingCache(true);
Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, Mode.SRC_IN);
Drawable drawable = customMarkerView.getBackground();
if (drawable != null) {
drawable.draw(canvas);
}
customMarkerView.draw(canvas);
return returnedBitmap;
} else {
// the crash didn't happen because of this! I've checked using breakpoints, it returns bitmap 99.9% of times.
return null;
}
}
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.arapeak.katateeb, PID: 30706
com.google.maps.api.android.lib6.common.apiexception.b: Failed to decode image. The provided image must be a Bitmap.
at com.google.maps.api.android.lib6.impl.k.a(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):5)
at com.google.maps.api.android.lib6.impl.o.a(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):7)
at com.google.maps.api.android.lib6.impl.db.<init>(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):25)
at com.google.maps.api.android.lib6.impl.bc.a(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):496)
at com.google.android.gms.maps.internal.l.onTransact(:com.google.android.gms.dynamite_dynamitemodulesb@12529020@12.5.29 (040308-192802242):94)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.internal.zzeu.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzg.addMarker(Unknown Source)
at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
你好,我用过这个,对我来说效果很好
//generate bit map from view
private Bitmap createBitMap(View v) {
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
if (b != null) {
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
}
return b;
}
// created user marker with his image
private void addMyLocationMarker() {
View view = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.layout_marker, null, false);
view.setLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
if (userMarker != null)
userMarker = googleMap
.addMarker(new MarkerOptions()
.anchor(0.5F, 0.5F)
.position(latlng)
.icon(BitmapDescriptorFactory.fromBitmap(createBitMap(bindingUserMarker.getRoot()))));
userMarker.setZIndex(2f);
updateUserMarkerImage(view);
}
}
//update user image
private void updateUserMarkerImage(View view ) {
ImageView imageView=view.findViewById(R.id.image);
//load your image using any image loader and recreate marker with your view and set it marker
}