flutterdartflutter-layoutuint8arrayuint8list

Convert uint8list image to a supported Image file(jpg or etc.)


In android 10 and 11 (SDK number 29 and 30) music covers (artworks) formats aren't the same as previous versions, in fact, they aren't readable as a supported image file such as jpg or png, they are uint8lists and only Image.memory widget accepts and shows them.

Now, my problem is I don't wanna read them as a memory image, I need files such as jpg or etc.

Searched a lot! Read a lot and found only 1, not working solution! And that was writing unit8list as bytes in a file which it's path is ended with ".jpg". As I said it's not working.

Anyone can help?

Thanks in advance, <3.

final io.File image = File('directory/image.jpg').create();
image.writeAsBytesSync(uint8list);

uint8list sample:[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 226, 2, 40, 73, 67, 67, 95, 80, 82 ,...]

length: thousands


Solution

  • To write Uint8list as a file, you can use File(String).writeAsBytes(Uint8list);. You can set the path and file extension on the File object and pass the image bytes on writeAsBytes()

    Uint8List imageBytes;
    File('image.jpg').writeAsBytes(imageBytes);