我拍了一张照片,它被存储在一个临时目录中,这样在发布之前就可以在预览屏幕上查看它。我正在尝试弄清楚如何将此imagePath转换为文件,以便将其存储在Firebase中。
这是拍摄照片的函数
_onCapturePressed(context) async {
// Take the Picture in a try / catch block. If anything goes wrong,
// catch the error.
try {
// Attempt to take a picture and log where it's been saved
final path = join(
// In this example, store the picture in the temp directory. Find
// the temp directory using the `path_provider` plugin.
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
//print(path);
await controller.takePicture(path);
imagePath = path;
// If the picture was taken, display it on a new screen
Navigator.push(
this.context,
MaterialPageRoute(
builder: (context) => displayStoryUploadScreen(),
),
);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
}这是预览图片的displayUploadScreen函数。
imageFile通过"body:“传递。
displayStoryUploadScreen() {
print(imagePath);
//Navigator.pop(context);
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
iconTheme: IconThemeData(color: Colors.blue),
title: Text(
"Preview",
style: TextStyle(
color: Colors.blue,
fontSize: 22.0,
),
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.done,
color: Colors.blue,
size: 30.0,
),
onPressed: () => controlUploadAndSave(),
),
],
),
// The image is stored as a file on the device. Use the `Image.file`
// constructor with the given path to display the image.
body: Image.file(File(imagePath),
//decoration: BoxDecoration(image: DecorationImage(image: FileImage(file), fit: BoxFit.cover)),
),
);
}这是我需要帮助的save函数。我需要得到的imagePath是一个文件,以便这一部分可以工作,我可以添加逻辑,以保存照片到火库。
controlUploadAndSave() async {
setState(() {
uploading = true;
});
await compressingPhoto();
String downloadUrl = await uploadPhoto(file);
savePostInfoToFireStore(url: downloadUrl);
setState(() {
file = null;
uploading = false;
storyPostId = Uuid().v4();
});
}发布于 2020-09-11 11:21:08
您实际上已经做到了这一点。使用File(path)返回给定路径中的文件。
编辑:基于额外信息的更多详细信息:
你真的应该在你的状态中使用更多的参数,而不是值,但最终这取决于你。基本上,您需要做的是将imagePath作为参数(或者File(imagePath),如果您不想在函数中转换它)传递给controlUploadAndSave。
此外,您还提到了imagePath为空的问题。当你将一个函数传递给页面路由时,我实际上不知道Flutter是否起作用。这是不正常的,你应该创建窗口小部件而不是返回窗口小部件的函数。但是,您也可以在此处将imagePath作为参数传递。
主要问题是您使用的是函数而不是小部件。对于新屏幕(ConfirmationScreen),您应该有一个单独的小部件(如果需要,可以有单独的状态)。然后,您可以使用类似以下内容:
MaterialPageRoute(
builder: (context) => ConfirmationScreen(imageFile: File(imagePath)),
),将您的图像发送到此小工具。一旦您完成了该屏幕并想要返回到上一个屏幕,您可以简单地弹出导航器并可选地包括一个值。该值将作为将来返回到您调用Navigator.push的位置。
https://stackoverflow.com/questions/63839507
复制相似问题