You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a jpg file, but it's named "xx.png", when I try decode this file , I using decodeImageFile("xx.png"); . But get a null .
I check the code of decodeImageFile, it will get decoder by file name at findDecoderForNamedImage, in my case, it will use pngDecoder, of course it a mistake.
I find in pngDecoder, it will check file header:
final pngHeader = _input.readBytes(8);
const expectedHeader = [137, 80, 78, 71, 13, 10, 26, 10];
for (var i = 0; i < 8; ++i) {
if (pngHeader[i] != expectedHeader[i]) {
return null;
}
}
If not png header, will return null.
So, why don't try another decoder if file header not match file name's extension?
Like change:
Future<Image?> decodeImageFile(String path, {int? frame}) async {
final bytes = await readFile(path);
if (bytes == null) {
return null;
}
final decoder = findDecoderForNamedImage(path);
if (decoder != null) {
- return decoder.decode(bytes, frame: frame);
}
return decodeImage(bytes, frame: frame);
}
I have a jpg file, but it's named "xx.png", when I try decode this file , I using
decodeImageFile("xx.png");
. But get a null .I check the code of
decodeImageFile
, it will get decoder by file name atfindDecoderForNamedImage
, in my case, it will use pngDecoder, of course it a mistake.I find in pngDecoder, it will check file header:
If not png header, will return null.
So, why don't try another decoder if file header not match file name's extension?
Like change:
Future<Image?> decodeImageFile(String path, {int? frame}) async { final bytes = await readFile(path); if (bytes == null) { return null; } final decoder = findDecoderForNamedImage(path); if (decoder != null) { - return decoder.decode(bytes, frame: frame); } return decodeImage(bytes, frame: frame); }
To:
Future<Image?> decodeImageFile(String path, {int? frame}) async { final bytes = await readFile(path); if (bytes == null) { return null; } final decoder = findDecoderForNamedImage(path); if (decoder != null) { + return decoder.decode(bytes, frame: frame) ?? decodeImage(bytes, frame: frame); } return decodeImage(bytes, frame: frame); }
How about it?
The text was updated successfully, but these errors were encountered: