【发布时间】:2020-09-11 01:11:11
【问题描述】:
我拍了一张照片,它被存储在一个临时目录中,因此可以在发布之前在预览屏幕上查看。我正在尝试弄清楚如何将此 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)),
),
);
}
这是我需要帮助的保存功能。我需要让 imagePath 成为一个文件,这样这部分才能工作,我可以添加将照片保存到 Firebase 的逻辑。
controlUploadAndSave() async {
setState(() {
uploading = true;
});
await compressingPhoto();
String downloadUrl = await uploadPhoto(file);
savePostInfoToFireStore(url: downloadUrl);
setState(() {
file = null;
uploading = false;
storyPostId = Uuid().v4();
});
}
【问题讨论】:
标签: file flutter path filepath temporary-files