2

I want to create a simple profile picture for my app. I thought it would work like this, but it throws an exception when calling it and I can't figure out why.

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

The attribute user.profileImage has the datatype Uint8List.

When I call it, it throws this exception:

════════ 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)

I think it's caused by Image.memory.

Thanks for answers in advance!

7
  • It probably not one of the supported formats, like BMP, PNG or JPEG. Commented Feb 8, 2020 at 12:56
  • I retrieve my image from a server and I get content-type: [image/png] as the content-type, so it should be a png right? Commented Feb 8, 2020 at 13:03
  • 1
    what are the first 8 bytes of user.profileImage? in hex? is it 89 50 4E 47 0D 0A 1A 0A? if not, compare them with en.wikipedia.org/wiki/List_of_file_signatures Commented Feb 8, 2020 at 13:06
  • If the image is coming from the network, try Image.network Commented Feb 8, 2020 at 17:08
  • Ok, so I think it may be that the image i get from the server is corrupted, but I'm not quite sure yet. Commented Feb 9, 2020 at 13:13

4 Answers 4

2

I ran into the exact same problem. The image was a png file that was properly 'loaded' in the pubspec file. However I got the same error:

Exception: Could not instantiate image codec.

It turned out the image was corrupted. Replacing the image solved it for me.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes that was the problem with mine too!
2

It could be that you have to encode it to an appropriate type with https://pub.dev/packages/image like:

    import 'package:image/image.dart' as imageUtils;

    var encodedImage = Image.memory(
                    imageUtils.encodePng(user.profileImage));

Comments

1

I was having a similar exception when trying to load the contacts avatar using Image.memory. It basically takes in Uint8List as the input source.

And I found that you receive that exception when the list is empty(length=0). so you can prevent that exception by putting a check something like this

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)
             );
 }

Comments

0

My problem was try load and image in base64 type jpg or png with Image.memory() but the image was svg, so i just wrote:

 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,
    );}

I hope it helps, happy coding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.