Android 如何在文件系统中写入映像的内容
我正在尝试写图片的内容,我从图库或相机中选择,但是,在一个文件系统中,它没有写任何东西,我不知道为什么,请有人帮助我Android 如何在文件系统中写入映像的内容,android,flutter,dart,Android,Flutter,Dart,我正在尝试写图片的内容,我从图库或相机中选择,但是,在一个文件系统中,它没有写任何东西,我不知道为什么,请有人帮助我 List<int> bytes; Future<String> get _localPath async { final directory = await getApplicationDocumentsDirectory(); print(directory.path); return directory.path; }
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;
});
}
列出字节;
未来获取\u本地路径异步{
最终目录=等待getApplicationDocumentsDirectory();
打印(directory.path);
返回directory.path;
}
未来获取\u本地文件异步{
最终路径=等待_localPath;
打印(路径);
返回文件(“$path/image.png”);
}
未来写计数器(变量字节)异步{
final file=wait\u localFile;
打印(文件);
//写这个文件。
返回文件.writeAsBytes(字节);
}
Future readCounter()异步{
试一试{
final file=wait\u localFile;
//读取文件。
List contents=wait file.readAsBytes();
返回int.parse(contents.toString());
}捕获(e){
//如果遇到错误,则返回0。
返回0;
}
}
Future pickImage()异步{
tempStore=wait ImagePicker.pickImage(源:ImageSource.gallery);
bytes=等待tempStore.readAsBytes();
//encoded1=base64.encode(字节);
写计数器(字节);
var val=等待读取计数器();
打印(val);
//打印(编码D1);
设置状态(){
pickedImage=tempStore;
isImageLoaded=true;
});
}
1。打开图像文件
对于打开图像,实际上可以将可变内容设置为文件
try {
final file = await _localFile;
// Read the file.
List<int> contents = await file.readAsBytes();
return int.parse(contents.toString());
}
所以稍后,我们可以使用这个小部件轻松地渲染它
class DisplayImage extends StatelessWidget {
DisplayImage({
@required this.imageData,
});
final File imageData;
@override
Widget build(BuildContext context) {
return Image.file(imageData);
}
}
2.保存图像文件
另一方面,为了保存图像,您实际上可以更改此设置
codetempStore = await ImagePicker.pickImage(source: ImageSource.gallery);
bytes = await tempStore.readAsBytes();
// encoded1 = base64.encode(bytes);
writeCounter(bytes);
及
这一个
final file = await _localFile;
File result = await file.writeAsBytes(imageData.readAsBytesSync());
return result;
3.完整示例
幸运的是,我在这里构建了完整的工作示例,我发现如果我们将更多的业务逻辑分离到其他文件中,它将变得更易于管理
演示
文件: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;
}
}
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;
});
}
Future loadImage() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
try {
File result = await LocalStorage().loadImageLocal(
imageFilename: imageFilename,
);
setState(() {
loadedImage = result;
});
}
}
}
文件:lib/screen/openImage.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;
}
}
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;
});
}
Future loadImage() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
try {
File result = await LocalStorage().loadImageLocal(
imageFilename: imageFilename,
);
setState(() {
loadedImage = result;
});
}
}
}