Save a memory image (such as Uint8list) as image file in flutter

25.9k Views Asked by At

I have some Uint8lists and I want to save them as jpg files. Can anyone help?

2

There are 2 best solutions below

4
On BEST ANSWER

By 'storage', do you mean write to a file? You don't really need "flutter" to do this. Just use the libraries provided by dart. Here is an example of downloading my gravatar which you can get as Uin8List and then saving it to a file.

import 'dart:io';
import 'dart:typed_data';

import 'package:http/http.dart' as http;

void main() {
  http.get('https://www.gravatar.com/avatar/e944138e1114aefe4b08848a46465589').then((response) {
    Uint8List bodyBytes = response.bodyBytes;
    File('my_image.jpg').writeAsBytes(bodyBytes);
  });
}
1
On

Here is a simple and short solution of your problem. Use this line in your code as I did:

"SettableMetadata(contentType: "image/jpeg")," 

Code:

 if (kIsWeb) {
         await ref.putData(
          await imageFile.readAsBytes(),
           SettableMetadata(contentType: "image/jpeg"),
         );
         url = await ref.getDownloadURL();
         }