我有一些Uint8lists,想将它们保存为jpg文件。有人可以帮忙吗?
我有一些Uint8lists,想将它们保存为jpg文件。有人可以帮忙吗?
你所说的“存储”是指写入文件吗?其实你不需要使用“flutter”来做这件事,只需使用dart提供的库即可。这里有一个示例,演示如何下载我的Gravatar并将其保存到文件中,你可以把它获取为Uin8List
。
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);
});
}
final dir = await getExternalStorageDirectory();
final myImagePath = dir!.path + "/myimg.png";
File imageFile = File(myImagePath);
if(! await imageFile.exists()){
imageFile.create(recursive: true);
}
imageFile.writeAsBytes(image);
- MaseedByteData byteData = await image.toByteData(format: ImageByteFormat.png); Uint8List pngBytes = byteData.buffer.asUint8List();
其中image
来自dart:ui
包。 - LZM以下是您问题的简单而短小的解决方案。像我一样在您的代码中使用这行:
"SettableMetadata(contentType: "image/jpeg"),"
代码:
if (kIsWeb) {
await ref.putData(
await imageFile.readAsBytes(),
SettableMetadata(contentType: "image/jpeg"),
);
url = await ref.getDownloadURL();
}
dart.io
中的File
类有保存列表的方法。 - pskink