我正在尝试从图库或照相机中选择要写入的图像内容,但是它没有写任何我不知道为什么的文件系统,请有人帮我
List<int> bytes;
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
print(directory.path);
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
print(path);
return File('$path/image.png');
}
Future<File> writeCounter(var bytes) async {
final file = await _localFile;
print(file);
// Write the file.
return file.writeAsBytes(bytes);
}
Future<int> readCounter() async {
try {
final file = await _localFile;
// Read the file.
List<int> contents = await file.readAsBytes();
return int.parse(contents.toString());
} catch (e) {
// If encountering an error, return 0.
return 0;
}
}
Future pickImage() async {
tempStore = await ImagePicker.pickImage(source: ImageSource.gallery);
bytes = await tempStore.readAsBytes();
// encoded1 = base64.encode(bytes);
writeCounter(bytes);
var val = await readCounter();
print(val);
//print(encoded1);
setState(() {
pickedImage = tempStore;
isImageLoaded = true;
});
}
答案 0 :(得分:1)
要打开图像,实际上您可以将变量内容设置为“文件”。
try {
final file = await _localFile;
// Read the file.
List<int> contents = await file.readAsBytes();
return int.parse(contents.toString());
}
将其变成
try {
File file = await _localFile;
return file;
}
所以稍后,我们可以使用此小部件轻松渲染它
class DisplayImage extends StatelessWidget {
DisplayImage({
@required this.imageData,
});
final File imageData;
@override
Widget build(BuildContext context) {
return Image.file(imageData);
}
}
另一方面,为了保存图像,您实际上可以更改
codetempStore = await ImagePicker.pickImage(source: ImageSource.gallery);
bytes = await tempStore.readAsBytes();
// encoded1 = base64.encode(bytes);
writeCounter(bytes);
和
final file = await _localFile;
print(file);
// Write the file.
return file.writeAsBytes(bytes);
进入这个
final file = await _localFile;
File result = await file.writeAsBytes(imageData.readAsBytesSync());
return result;
幸运的是,我在此Github Repo中建立了完整的工作示例,发现如果我们将更多的业务逻辑与其他文件分开,它将变得更易于管理。
文件: lib / services / localStorage.dart
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class LocalStorage {
String getFileExt(File imageData) {
String fullFilename = imageData.uri.pathSegments.last;
String extension = fullFilename.split(".").last;
return extension;
}
Future<File> _getLocalFile({String filename}) async {
var dir = await getApplicationDocumentsDirectory();
return File('${dir.path}/$filename');
}
// Loading Image File
Future<File> loadImageLocal({String imageFilename}) async {
final file = await _getLocalFile(filename: imageFilename);
if (!file.existsSync()){
throw FormatException("does not exist");
}
return file;
}
// Saving Image File
Future<File> saveImageLocal({File imageData, String newFilename}) async {
final file = await _getLocalFile(filename: newFilename);
File result = await file.writeAsBytes(imageData.readAsBytesSync());
return result;
}
}
文件: lib / screen / saveImage.dart
Future saveImage() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
await LocalStorage().saveImageLocal(
imageData: imageData,
newFilename: newFilename,
);
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text('$newFilename saved successfully'),
duration: Duration(seconds: 3),
));
}
}
Future getImage() async {
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
imageData = image;
});
}
文件: lib / screen / openImage.dart
Future loadImage() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
try {
File result = await LocalStorage().loadImageLocal(
imageFilename: imageFilename,
);
setState(() {
loadedImage = result;
});
}
}
}