我如何用相机捕捉图像并将其保存到手机存储?



我可以用相机捕捉图像,但我无法将其保存到手机存储空间。

UI部分:

File? image;
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
height: 300,
width: 300,
child: image != null
? Image.file(image!)
: Container(
height: 300,
width: 300,
color: Colors.indigo,
),
),
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.grey[300],
child: Icon(Icons.camera),
onPressed: () {
getImage();
},
),
);
}

逻辑部分:点击浮动动作按钮后,getImage方法将被调用。

Future getImage() async {
final XFile? pickedFile =
await ImagePicker().pickImage(source: ImageSource.camera);
try {
if (pickedFile != null) {
Directory appDocDir = await getApplicationDocumentsDirectory();
String path = appDocDir.path;
File pickedImageFile = File(pickedFile.path);
final  _baseName = Path.basename(pickedFile!.path);
final String _fileExtension = Path.extension(pickedFile.path);
final File newImage = await pickedImageFile!.copy('$path/$_baseName$_fileExtension');
setState(() {
image = newImage;
print("file path...");
});
} else {
print('No image selected.');
}
} catch (e) {
print(e);
}
} 

首先,安装两个插件
这个插件gallery_saver保存图像和视频从相机或网络到本地存储(Android和iOS)。这个插件image_picker保存图像和视频从相机或网络到本地存储(Android和iOS)。

import 'package:flutter/material.dart';
import 'package:gallery_saver/gallery_saver.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String firstButtonText = 'Take photo';
String secondButtonText = 'Record video';
double textSize = 20;
final ImagePicker _picker = ImagePicker();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
color: Colors.white,
child: Column(
children: <Widget>[
Flexible(
child: SizedBox.expand(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.blue,
),
onPressed: _takePhoto,
child: Text(firstButtonText,
style:
TextStyle(fontSize: textSize, color: Colors.white)),
),
),
flex: 1,
),
Flexible(
child: SizedBox.expand(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white,
),
onPressed: _recordVideo,
child: Text(secondButtonText,
style: TextStyle(
fontSize: textSize, color: Colors.blueGrey)),
),
),
flex: 1,
)
],
),
),
));
}
void _takePhoto() async {
await _picker
.pickImage(source: ImageSource.camera)
.then((XFile? recordedImage) {
if (recordedImage != null && recordedImage.path != null) {
setState(() {
firstButtonText = 'saving in progress...';
});
GallerySaver.saveImage(recordedImage.path).then((path) {
setState(() {
firstButtonText = 'image saved!';
});
});
}
});
}
void _recordVideo() async {
await _picker
.pickVideo(source: ImageSource.camera)
.then((XFile? recordedVideo) {
if (recordedVideo != null && recordedVideo.path != null) {
setState(() {
secondButtonText = 'saving in progress...';
});
GallerySaver.saveVideo(recordedVideo.path).then((path) {
setState(() {
secondButtonText = 'video saved!';
});
});
}
});
}
}

N。B:你必须设置minSdkVersion21到(您已经在androidappbuild.gradle中找到了minSdkVersion)。

可以使用image_gallery_saver包。

_saveImageToGallery(String path) async {
final result = await ImageGallerySaver.saveFile(path);
print(result);
}

请记住将所需的权限添加到清单中。你可以在这个包的自述页面上找到它。

最新更新