gpt4 book ai didi

flutter - 使用相机包拍照后图像未保存在移动设备上

转载 作者:行者123 更新时间:2023-12-05 05:35:55 25 4
gpt4 key购买 nike

当我使用相机包的 pub.dev 示例页面中的这段代码时,我试图将图像保存到我的本地移动设备。但是我没有找到保存在设备上的文件。谁能帮我解决如何将拍摄的图像保存到给定路径

void onTakePictureButtonPressed() {
takePicture().then((XFile? file) {
if (mounted) {
setState(() {
imageFile = file;
videoController?.dispose();
videoController = null;
});
if (file != null) {
showInSnackBar('Picture saved to ${file.path}');
}
}
});
}

最佳答案

使用 await ImagePicker.pickImage(...),您已经在正确的轨道上,因为该函数返回一个文件。

File类有一个copy方法,你可以用它来复制文件(它已经被相机或gallery保存在磁盘上)并把它放到你的应用程序文档目录中:

// using your method of getting an image
final File image = await ImagePicker.pickImage(source: imageSource);

// getting a directory path for saving
final String path = await getApplicationDocumentsDirectory().path;

// copy the file to a new path
final File newImage = await image.copy('$path/image1.png');

setState(() {
_image = newImage;
});

您还应该注意,您可以使用 image.pathImagePicker 获取图像文件的路径,其中还将包含您可能想要的文件结尾提取,您可以使用 newImage.path 保存图像路径。

关于flutter - 使用相机包拍照后图像未保存在移动设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73392882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com