[英]save image to the gallery after taken from the camera
I use this code to take a picture with the plugin image_picker:我使用此代码使用插件 image_picker 拍照:
var image = await ImagePicker.pickImage(source: ImageSource.camera);
Now I would like to save the image in the gallery directory of the device.现在我想将图像保存在设备的画廊目录中。
According to the accepted answer to this post How to Save Image File in Flutter?根据这篇文章如何在 Flutter 中保存图像文件的公认答案? File selected using Image_picker plugin , it should have been done automatically, but I do not find in the gallery the picture taken. 使用 Image_picker 插件选择的文件,它应该是自动完成的,但我在图库中找不到拍摄的照片。
I already added this permission to the manifest:我已经将此权限添加到清单中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You can use package gallery_saver https://pub.dev/packages/gallery_saver您可以使用 package gallery_saver https://pub.dev/packages/gallery_saver
Get image with ImagePicker
and save with GallerySaver.saveImage
使用GallerySaver.saveImage
获取图像并使用ImagePicker
保存
code snippet代码片段
void _takePhoto() async {
ImagePicker.pickImage(source: ImageSource.camera)
.then((File recordedImage) {
if (recordedImage != null && recordedImage.path != null) {
setState(() {
firstButtonText = 'saving in progress...';
});
GallerySaver.saveImage(recordedImage.path, albumName: albumName)
.then((bool success) {
setState(() {
firstButtonText = 'image saved!';
});
});
}
});
}
demo演示
full code完整代码
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:gallery_saver/gallery_saver.dart';
import 'package:image_picker/image_picker.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String firstButtonText = 'Take photo';
String secondButtonText = 'Record video';
double textSize = 20;
String albumName ='Media';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Container(
color: Colors.white,
child: Column(
children: <Widget>[
Flexible(
flex: 1,
child: Container(
child: SizedBox.expand(
child: RaisedButton(
color: Colors.blue,
onPressed: _takePhoto,
child: Text(firstButtonText,
style:
TextStyle(fontSize: textSize, color: Colors.white)),
),
),
),
),
Flexible(
child: Container(
child: SizedBox.expand(
child: RaisedButton(
color: Colors.white,
onPressed: _recordVideo,
child: Text(secondButtonText,
style: TextStyle(
fontSize: textSize, color: Colors.blueGrey)),
),
)),
flex: 1,
)
],
),
),
));
}
void _takePhoto() async {
ImagePicker.pickImage(source: ImageSource.camera)
.then((File recordedImage) {
if (recordedImage != null && recordedImage.path != null) {
setState(() {
firstButtonText = 'saving in progress...';
});
GallerySaver.saveImage(recordedImage.path, albumName: albumName)
.then((bool success) {
setState(() {
firstButtonText = 'image saved!';
});
});
}
});
}
void _recordVideo() async {
ImagePicker.pickVideo(source: ImageSource.camera)
.then((File recordedVideo) {
if (recordedVideo != null && recordedVideo.path != null) {
setState(() {
secondButtonText = 'saving in progress...';
});
GallerySaver.saveVideo(recordedVideo.path, albumName: albumName)
.then((bool success) {
setState(() {
secondButtonText = 'video saved!';
});
});
}
});
}
// ignore: unused_element
void _saveNetworkVideo() async {
String path =
'https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4';
GallerySaver.saveVideo(path, albumName: albumName).then((bool success) {
setState(() {
print('Video is saved');
});
});
}
// ignore: unused_element
void _saveNetworkImage() async {
String path =
'https://image.shutterstock.com/image-photo/montreal-canada-july-11-2019-600w-1450023539.jpg';
GallerySaver.saveImage(path, albumName: albumName).then((bool success) {
setState(() {
print('Image is saved');
});
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.