flutter - 在flutter上使用最新版本的相机包时如何修复错误
问题描述
我是一名新手开发人员,正在使用 plutter 进行开发。我正在使用相机包开发过滤器应用程序。您使用的软件包版本是相机:^0.8.1+5。
我从flutter.dev中拿官方的例子来用,但是问题是运行app时第一次拍照,图片库中不再保存。仅拍摄并保存第一张照片。
我使用一个包来保存画廊照片,image_gallery_saver:^1.6.9
以下是代码部分。有什么问题?我在这上面花了一个星期,但我不知道。我需要专家的帮助。谢谢
class _TakePictureScreenState extends State<TakePictureScreen> {
late CameraController _controller;
late Future<void> _initializeControllerFuture;
XFile? imageFile;
@override
void initState() {
super.initState();
_controller = CameraController(
widget.camera,
ResolutionPreset.high,
);
_initializeControllerFuture = _controller.initialize();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Container(
child: CameraPreview(_controller),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
try {
await _initializeControllerFuture;
final path = join(
(await getApplicationDocumentsDirectory()).path,
'${DateTime.now()}.png',
);
imageFile = await _controller.takePicture();
imageFile!.saveTo(path);
Fluttertoast.showToast(msg: 'take Picture');
ImageGallerySaver.saveFile(path);
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => DisplayPictureScreen(imagePath: path),
// ),
// );
} catch (e) {
Fluttertoast.showToast(msg: e.toString());
}
},
),
);
}
}
解决方案
I fixed it. The picture is starting to be taken properly. I hope people who use the latest version of the camera package refer to it.
_controller.takePicture().then((XFile? file) {
if (mounted) {
setState(() {
imageFile = file;
});
if (file != null)
Fluttertoast.showToast(msg: 'Picture saved to ${file.path}');
ImageGallerySaver.saveFile(file!.path);
}
});
推荐阅读
- rest - 在 REST 服务中返回异步 ElasticSearch 数据
- html - 如何将多个 div(及其子元素)显示为内联?
- javascript - 将数据从 Vue 函数移动到外部 JSON 文件
- reporting-services - SSRS 在 TextBox 中使用 vbcrlf 来连接字段需要排除空白返回行
- c# - 工作代码需要重构以提高速度
- xcode - 有没有办法从构建阶段运行脚本访问 Xcodes 当前构建输出?
- javafx - 如何创建已有形状的新实例(副本)?JavaFX
- sql-server - 如何合并具有相同名称 SQL Pivot 的行
- python - 如何修复“List Index Our of Range”错误
- c++ - C++ std::unique 没有显示我对它的期望