Skip to content

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

Closed
flutter/packages
#6832

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]

Activity

  1. GrzywN commented on May 28, 2024

    @GrzywN

    Having the same problem after migration, pls help :D

  2. changed the title [-]After change BitmapDescriptor.fromBytes to BitmapDescriptor.bytes app crash[/-] [+][Google_maps]After change BitmapDescriptor.fromBytes to BitmapDescriptor.bytes app crash[/+] on May 28, 2024
  3. darshankawar commented on May 29, 2024

    @darshankawar
    Member

    @PrzemyslawPluszowy Can you provide us a runnable reproducible code sample in properly formatted text so that we can directly use it and verify at our end ? Providing code screenshot isn't much helpful.

  4. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 29, 2024
  5. PrzemyslawPluszowy commented on May 29, 2024

    @PrzemyslawPluszowy
    Author
  6. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 29, 2024
  7. PrzemyslawPluszowy commented on May 29, 2024

    @PrzemyslawPluszowy
    Author

    another error after migration to flutter maps 2.6.1 in another project
    when i'm back to .fromAssetImage all work fine

    static Future<BitmapDescriptor> _createIcon(MapPinType type) async {
        return await BitmapDescriptor.asset(
          const ImageConfiguration(
            devicePixelRatio: 2.5,
            size: Size(28, 28),
          ),
          type.getPathIcon(),
        );
      }
    }
    
    enum MapPinType {
      accident,
      towing,
      delivery;
    
      String getPathIcon() {
        switch (this) {
          case MapPinType.accident:
            return Assets.images.iconMapCrashPng.path;
          case MapPinType.towing:
            return Assets.images.garageMapIcon.path;
          case MapPinType.delivery:
            return Assets.images.iconMapHome.path;
        }
      }
    }

    image

  8. darshankawar commented on May 29, 2024

    @darshankawar
    Member
    static generateCustomMarkersIconFromPath(String? path) async {

    @PrzemyslawPluszowy How are you calling this method ? Can you provide us complete code (ex: main.dart) so that we can simply copy paste and verify this further ?

    another error after migration to flutter maps 2.6.1 in another project

    What version you were on earlier before upgrading to 2.6.1 ?
    Also, this issue I was able to replicate with the help of another issue.

  9. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 29, 2024
  10. PrzemyslawPluszowy commented on May 29, 2024

    @PrzemyslawPluszowy
    Author

    this is the same issue #149226 (comment)
    Version 2.6.0, but I think the problem is with it when I upgraded Flutter to the newest version and ide show me deprecated

  11. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 29, 2024
  12. stuartmorgan-g commented on May 29, 2024

    @stuartmorgan-g
    Contributor

    This is an unintended effect of the way the federated change to add the new formats was rolled out. The new formats won't be supported until flutter/packages#6826 lands.

    @jokerttu Could you do a new PR ASAP to remove the deprecation notice? We can re-add it after the implementation PRs have landed.

  13. added
    c: regressionIt was better in the past than it is now
    and removed
    in triagePresently being triaged by the triage team
    on May 29, 2024
  14. 10 remaining items

  15. MMoarales commented on Jun 3, 2024

    @MMoarales

    Same problem here, someone has any solution for this ?

  16. stuartmorgan-g commented on Jun 3, 2024

    @stuartmorgan-g
    Contributor

    The solution is not to use the new constructors until flutter/packages#6826 lands.

  17. MMoarales commented on Jun 3, 2024

    @MMoarales

    Ok, got it, thank you!!!

  18. SkimoBen commented on Jun 19, 2024

    @SkimoBen

    One thing I've noticed when using BitmapDescriptor.fromAssetImage() is that the marker sizing ends up being totally different on android Vs. iOS / chrome. So like everyone else has mentioned, using the other deprecated method return BitmapDescriptor.fromBytes(resizedData); Seems like the best solution currently.

    It will be really awesome if BitmapDescriptor.asset() just works soon.

  19. jokerttu commented on Jun 20, 2024

    @jokerttu

    flutter/packages#6826 is now landed and new methods, BitmapDescriptor.asset() and BitmapDescriptor.bytes() are implemented for each platform, and can be used after using after plugin versions are updated to latest.
    Latest version of google_maps_flutter 2.7.0 now also exposes new AssetMapBitmap and BytesMapBitmap classes.
    See example to see how to use these new methods.

  20. github-actions commented on Jul 4, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  21. locked as resolved and limited conversation to collaborators on Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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