【问题标题】:Flutter Failed to decode image. The provided image must be a Bitmap., null)Flutter 无法解码图像。提供的图像必须是位图。,null)
【发布时间】:2020-05-23 11:43:05
【问题描述】:

在 Flutter 中在 Google Map 中创建自定义图标显示错误。

我的 pubspec.yaml 文件

assets:
  - assets/truck.png

我的代码是:

void getCustomIcon() async {
    customIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(
          devicePixelRatio: 2.5,
    ),
    'assets/truck.png');
}

错误是:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Failed to decode image. The provided image must be a Bitmap., null)
E/flutter (15757): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (15757): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:321:33)

【问题讨论】:

  • 您是否为资产配置了 pubspec。我认为您缺少配置
  • @Dev 我在 assets 文件夹中添加了该图像,并在 pubspec 文件中也提到了兄弟。
  • 创建标记的代码是什么
  • @Dev 我在我的问题中添加了用于创建标记的代码,并在我的类的 initstate 中调用了该方法

标签: google-maps flutter dart maps google-maps-markers


【解决方案1】:

在状态类中定义

BitmapDescriptor customIcon ;

致电initState

getBytesFromAsset('assets/truck.png', 64).then((onValue) {
      customIcon =BitmapDescriptor.fromBytes(onValue);

    });

函数在哪里

  static Future<Uint8List> getBytesFromAsset(String path, int width) async {
    ByteData data = await rootBundle.load(path);
    ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(), targetWidth: width);
    ui.FrameInfo fi = await codec.getNextFrame();
    return (await fi.image.toByteData(format: ui.ImageByteFormat.png)).buffer.asUint8List();
  }

然后在标记创建中

markers.add(
            Marker(
                markerId: ....,
                position: ....,
                icon: customIcon ,
                 onTap: () { 
                  ....

                }
            )

        );

【讨论】:

  • 可能最好把图片离线转成bmp。图像客户端一直在转换图像...
  • @giorgio79 我在在线将 png 转换为 bmp 时遇到的问题是他们强制用纯色填充透明背景区域。上述解决方案效果很好。
  • ui 来自哪里?请始终尝试提供完整的解决方案。此外,这段代码是从这里复制的javaer101.com/en/article/38474402.html
  • 添加import 'dart:ui' as ui;
  • 这行不通了
猜你喜欢
  • 2018-03-12
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多