0

From backend server, I received buffer array like belows.

{
  "type": "Buffer",
  "data": [
    239,
    191,
    189,
    80,
    78,
    71,
    13,
    10,
    26,
    ...
  ]
}

To receive above data, I created a response DTO like this.

@freezed
class FileDownloadResponse with _$FileDownloadResponse {
  factory FileDownloadResponse({
    /// file type
    @JsonKey(name: 'type') required String type,

    /// buffer
    @JsonKey(name: 'data') required List<int> imageBuffer,
  }) = _FileDownloadResponse;

  factory FileDownloadResponse.fromJson(Map<String, dynamic> json) => _$FileDownloadResponseFromJson(json);
}

And, with that imageBuffer, I tried to make a Image.memory widget like this. (For easy description, I simplified my code, and checked that profileImageUrl value is not null)

class SelectProfileWidget extends StatelessWidget {
  final List<int>? imageBuffer;
  final double? height;
  final double? width;
  final double? iconHeight;
  final double? iconWidth;

  SelectProfileWidget({
    Key? key,
    this.imageBuffer,
    this.height,
    this.width,
    this.iconHeight,
    this.iconWidth,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (imageBuffer != null) {
      return Image.memory(
        Uint8List.fromList(imageBuffer!),
        height: height ?? 88,
        width: width ?? 88,
        fit: BoxFit.cover,
      );
    }
    return const SizedBox.shrink();
  }
}

When I tried to run this code, this error was occurred.

E/FlutterJNI( 3742): Failed to decode image
E/FlutterJNI( 3742): android.graphics.ImageDecoder$DecodeException: Failed to create image decoder with message 'unimplemented'Input contained an error.
E/FlutterJNI( 3742):    at android.graphics.ImageDecoder.nCreate(Native Method)
E/FlutterJNI( 3742):    at android.graphics.ImageDecoder.access$200(ImageDecoder.java:173)
E/FlutterJNI( 3742):    at android.graphics.ImageDecoder$ByteBufferSource.createImageDecoder(ImageDecoder.java:250)
E/FlutterJNI( 3742):    at android.graphics.ImageDecoder.decodeBitmapImpl(ImageDecoder.java:1847)
E/FlutterJNI( 3742):    at android.graphics.ImageDecoder.decodeBitmap(ImageDecoder.java:1840)
E/FlutterJNI( 3742):    at io.flutter.embedding.engine.FlutterJNI.decodeImage(FlutterJNI.java:524)

======== Exception caught by image resource service ================================================
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data

When the exception was thrown, this was the stack: 
#0      _futurize (dart:ui/painting.dart:5718:5)
#1      ImageDescriptor.encoded (dart:ui/painting.dart:5574:12)
#2      instantiateImageCodec (dart:ui/painting.dart:2056:60)
<asynchronous suspension>
====================================================================================================

What is the problem in my code...? Or, is there another way to convert buffer array to image widget??

Please help me

Jun
  • 451
  • 4
  • 16
  • 1
    Convert your array to hex and check first 8 bytes (for example) against https://en.wikipedia.org/wiki/List_of_file_signatures - what file does it match? but honestly I see 13 and 10 so it means it is some kind of text file, not any flutter supported image – pskink Nov 15 '22 at 03:43
  • @pskink OMG... i will check that... Thanks! – Jun Nov 15 '22 at 04:08
  • `profileImageUrl` is a very misleading variable name if it stores byte data and isn't a `String` or an `Url`. You say that `profileImageUrl` isn't `null`, but is it non-empty? You haven't demonstrated that it has data at all and haven't shown any code to populate it. – jamesdlin Nov 15 '22 at 06:43
  • @jamesdlin for simplification, I edited the SelectProfileWidget class code, but it might be able to be confused... I fix it! – Jun Nov 15 '22 at 07:07

0 Answers0