简体   繁体   中英

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

I have some Uint8lists and I wanna save them as jpg files. I searched a lot for this on the web but found nothing? Anyone can help?

Thanks,

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);
  });
}

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();
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM