Skip to content

[Google_maps]After change BitmapDescriptor.fromBytes to BitmapDescriptor.bytes app crash #149183

@PrzemyslawPluszowy

Description

@PrzemyslawPluszowy

Steps to reproduce

image
this func work good but if i change to BitmapDescriptor.bytes(imageToBytesUint8List);
i have this error:
image

What is wrong?

Expected results

Need find solution for this error

Actual results


E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): Failed to handle method call
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): java.lang.IllegalArgumentException: Cannot interpret [bytes, {bitmapScaling=auto, byteData=[B@420caa1, imagePixelRatio=1.0}] as BitmapDescriptor
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.plugins.googlemaps.Convert.toBitmapDescriptor(Convert.java:71)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.plugins.googlemaps.Convert.interpretMarkerOptions(Convert.java:452)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.plugins.googlemaps.MarkersController.addMarker(MarkersController.java:195)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.plugins.googlemaps.MarkersController.addMarkers(MarkersController.java:41)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.plugins.googlemaps.GoogleMapController.onMethodCall(GoogleMapController.java:385)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at android.os.Handler.handleCallback(Handler.java:900)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at android.os.Handler.dispatchMessage(Handler.java:103)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at android.os.Looper.loop(Looper.java:219)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at android.app.ActivityThread.main(ActivityThread.java:8668)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
E/MethodChannel#plugins.flutter.dev/google_maps_android_3(18423): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)

Code sample

Code sample
 static generateCustomMarkersIconFromPath(String? path) async {
    try {
      if (path == null || path.isEmpty) {
        return BitmapDescriptor.defaultMarker;
      }
      Uint8List bytes = File(path).readAsBytesSync();
      bytes = (_resizeImage(bytes, 140, 140))!;

      ui.Image image = await _convertImageFromUint8ListToCanvasImage(bytes);
      final generatedImage = await _drawCanvas(image);
      final ByteData? imageToBytes =
          await generatedImage.toByteData(format: ui.ImageByteFormat.png);
      if (imageToBytes == null) {
        return BitmapDescriptor.defaultMarker;
      }
      final Uint8List imageToBytesUint8List = imageToBytes.buffer.asUint8List();
      return BitmapDescriptor.bytes(imageToBytesUint8List);
    } catch (e) {
      return BitmapDescriptor.defaultMarker;
    }
  }

  static Future<ui.Image> _drawCanvas(ui.Image image) async {
    ui.PictureRecorder recorder = ui.PictureRecorder();
    Canvas canvasRecord = Canvas(recorder);
    PinCanavasDraw pinCanvasDraw = PinCanavasDraw(image: image);
    pinCanvasDraw.paint(canvasRecord, const Size(200, 200));
    var genratedImage = await recorder.endRecording().toImage(200, 200);
    return genratedImage;
  }

  static Uint8List? _resizeImage(Uint8List data, width, height) {
    Uint8List? resizedData = data;
    ui_image.Image? img = ui_image.decodeImage(data);
    ui_image.Image resized =
        ui_image.copyResize(img!, width: width, height: height);
    resizedData = Uint8List.fromList(ui_image.encodePng(resized));
    return resizedData;
  }

  static Future<ui.Image> _convertImageFromUint8ListToCanvasImage(
      Uint8List img) async {
    final Completer<ui.Image> completer = Completer();
    ui.decodeImageFromList(img, (ui.Image img) {
      return completer.complete(img);
    });
    return completer.future;
  }
}

class PinCanavasDraw extends CustomPainter {
  PinCanavasDraw({
    required this.image,
  });

  ui.Image image;

  @override
  void paint(Canvas canvas, Size size) async {
    Paint paint1 = Paint()
      ..color = const Color.fromARGB(163, 0, 0, 0).withOpacity(0.8)
      ..strokeWidth = 1
      ..style = PaintingStyle.fill;
    canvas.drawRect(const Rect.fromLTWH(23, 23, 154, 154), paint1);
    Path pathPin = Path()
      ..moveTo(100, 200)
      ..lineTo(80, 170)
      ..lineTo(120, 170)
      ..close();
    Path pathShadow = Path()..addRect(const Rect.fromLTWH(23, 23, 154, 154));
    canvas.drawPath(pathPin, paint1);
    canvas.drawShadow(pathShadow, const Color.fromARGB(255, 0, 0, 0), 3, false);

    canvas.drawImage(image, const Offset(30, 30), Paint());
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return false;
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[Paste your output here]

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority issues at the top of the work listc: regressionIt was better in the past than it is nowp: mapsGoogle Maps pluginpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions