将 ImagePicker 中的图像保存为本地内存(缓存)
Save Image From ImagePicker Locally as a Memory(cache)
我想将 ImagePicker 中的图像保存为内存,但出现错误。你能帮我解决这个功能吗?如果需要其他功能来加载图像,请在下面提到它。
Uint8List? memoryImage;
Future getImage() async {
final picker = ImagePicker();
final image = await picker.getImage(source: ImageSource.camera);
if (image == null) return;
final Directory directory = await getApplicationDocumentsDirectory();
final path=directory.path;
final Filename=basename(image.path);
File file = File('$directory/$Filename.jpg');
final bytes = await file.readAsBytes();
final byte1= file.writeAsBytes(bytes);
setState(() {
memoryImage = byte1 as Uint8List?;
});
}
通过这一行,您可以将图像字节写入文件。
File imageFile = await File(fileSavePath).writeAsBytes(imageBytes);
从您需要使用的文件访问 Uint8List
Uint8List memoryImage = File(imagePath).readAsBytesSync();
或者
Uint8List memoryImage = await File(imagePath).readAsBytes();
您的代码中的问题是您正在将文件分配给 Uint8List。这是我猜的错误
我想将 ImagePicker 中的图像保存为内存,但出现错误。你能帮我解决这个功能吗?如果需要其他功能来加载图像,请在下面提到它。
Uint8List? memoryImage;
Future getImage() async {
final picker = ImagePicker();
final image = await picker.getImage(source: ImageSource.camera);
if (image == null) return;
final Directory directory = await getApplicationDocumentsDirectory();
final path=directory.path;
final Filename=basename(image.path);
File file = File('$directory/$Filename.jpg');
final bytes = await file.readAsBytes();
final byte1= file.writeAsBytes(bytes);
setState(() {
memoryImage = byte1 as Uint8List?;
});
}
通过这一行,您可以将图像字节写入文件。
File imageFile = await File(fileSavePath).writeAsBytes(imageBytes);
从您需要使用的文件访问 Uint8List
Uint8List memoryImage = File(imagePath).readAsBytesSync();
或者
Uint8List memoryImage = await File(imagePath).readAsBytes();
您的代码中的问题是您正在将文件分配给 Uint8List。这是我猜的错误