0

All file images are returning jpeg. How to solve this and get the correct MIME type.

Flutter doctor - no issues

Flutter 3.7.7

pubspec.yaml
...
image_picker: ^0.8.7+5
mime: ^1.0.4
...

Future<void> chooseImage() async {
  var chosenImage = await _picker.pickImage(source: ImageSource.gallery);
  if (chosenImage != null) {
    setState(
      () {
        _tempFile = File(chosenImage.path);
      },
    );
  } else {
    // User canceled the picker
  }
}
...
List<int> imageBytes = _tempFile!.readAsBytesSync();
String baseimage = base64Encode(imageBytes);

print(lookupMimeType(baseimage, headerBytes: [0xFF, 0xD8]));
...

If anyone had the same issue and could provide some insight it would be really appreciated. thanks.

Rod Rodrigues
  • 53
  • 1
  • 9
  • 1
    `headerBytes` is supposed to consist of the beginning of the file you want to identify. You're passing a hard-coded byte sequence of `[0xFF, 0xD8]`, so it'll always identify the file as JPEG. Furthermore, the first argument to `lookupMimeType` is supposed to be the file *name*; passing a base64-encoded string makes no sense. See [Dart get extension from UInt8List](https://stackoverflow.com/q/66713893/) for an example of how to use it. – jamesdlin May 21 '23 at 06:02

0 Answers0