【问题标题】:Flutter: Could not instantiate image codec when calling Image.memoryFlutter:调用 Image.memory 时无法实例化图像编解码器
【发布时间】:2020-02-08 12:14:43
【问题描述】:

我想为我的应用创建一张简单的个人资料图片。 我认为它会像这样工作,但是在调用它时会引发异常,我 不知道为什么。

Widget _buildProfilePicture(double size) {
      if (user.profileImage != null) {
        return ClipRRect(
          borderRadius: BorderRadius.circular(8.0),
          child: Image.memory(
            user.profileImage,
            width: size,
            height: size,
          ),
        );
      }
}

属性user.profileImage 的数据类型为Uint8List

当我调用它时,它会抛出这个异常:

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following _Exception was thrown while resolving an image:
Exception: Could not instantiate image codec.

When the exception was thrown, this was the stack: 
#0      _futurize (dart:ui/painting.dart:4304:5)
#1      instantiateImageCodec (dart:ui/painting.dart:1682:10)
#2      PaintingBinding.instantiateImageCodec (package:flutter/src/painting/binding.dart:88:12)
#3      MemoryImage._loadAsync (package:flutter/src/painting/image_provider.dart:714:18)
#4      MemoryImage.load (package:flutter/src/painting/image_provider.dart:706:14)
...
Image provider: MemoryImage(Uint8List#2592a, scale: 1.0)
Image configuration: ImageConfiguration(bundle: PlatformAssetBundle#633d8(), devicePixelRatio: 2.6, locale: en_US, textDirection: TextDirection.ltr, size: Size(50.0, 50.0), platform: android)
Image key: MemoryImage(Uint8List#2592a, scale: 1.0)

我认为是Image.memory引起的。

提前感谢您的回答!

【问题讨论】:

  • 它可能不是受支持的格式之一,例如 BMP、PNG 或 JPEG。
  • 我从服务器检索我的图像,我得到 content-type: [image/png] 作为内容类型,所以它应该是 png 对吗?
  • user.profileImage 的前 8 个字节是什么?十六进制?是89 50 4E 47 0D 0A 1A 0A 吗?如果不是,请将它们与en.wikipedia.org/wiki/List_of_file_signatures 进行比较
  • 如果图片来自网络,请尝试Image.network
  • 好的,所以我认为可能是我从服务器获取的图像已损坏,但我还不太确定。

标签: image exception flutter dart


【解决方案1】:

我遇到了完全相同的问题。该图像是在 pubspec 文件中正确“加载”的 png 文件。但是我得到了同样的错误:

Exception: Could not instantiate image codec.

原来图像已损坏。 替换图像为我解决了它。

【讨论】:

  • 是的,这也是我的问题!
【解决方案2】:

我在尝试使用 Image.memory 加载联系人头像时遇到了类似的异常。它基本上以Uint8List为输入源。

我发现当列表为空(长度=0)时您会收到该异常。因此您可以通过检查类似这样的内容来防止该异常

Widget myAvatarWidget(){
    final imageBytes = contact.avatar;
    return imageBytes == null || imageBytes.length == 0
           ? showDefaultWidget(path: profileUrl, radius: 30.0)
           : ClipRRect(
               borderRadius: BorderRadius.circular(30.0),
               child: Image.memory(imageBytes)
             );
 }

【讨论】:

    【解决方案3】:

    您可能必须使用 https://pub.dev/packages/image 将其编码为适当的类型,例如:

        import 'package:image/image.dart' as imageUtils;
    
        var encodedImage = Image.memory(
                        imageUtils.encodePng(user.profileImage));
    
    

    【讨论】:

      【解决方案4】:

      我的问题是尝试使用Image.memory() 以 base64 类型的 jpg 或 png 加载和图像,但图像是 svg,所以我只写了:

       String _imageBase64;
       Uint8List bytes;
      
        @override
        Widget build(BuildContext context) {
      
          final size = getMediaSize(context);
          _imageBase64 = widget.reward.img;
          const Base64Codec base64 = Base64Codec();
      
          bytes = base64.decode(_imageBase64);
          return SvgPicture.memory(
            bytes,
            fit: BoxFit.fill,
            height: size.width * 0.1,
            width: size.width * 0.1,
          );}
      

      希望对您有所帮助,祝您编码愉快。

      【讨论】:

        猜你喜欢
        • 2020-06-27
        • 2015-11-21
        • 2016-09-25
        • 2020-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-12
        • 2022-10-20
        相关资源
        最近更新 更多