我正在尝试将相机中的图片保存在真实设备中,但找不到方法。
现在,我将其保存在文件中,但无法在图库中保存。.
此刻我的代码是:
File _imagenTemporal;
String _opcion = "";
var imagen;
Future getImagen(String opcion) async {
if (opcion == "camara") {
imagen = await ImagePicker.pickImage(source: ImageSource.camera);
} else if (opcion == "galeria") {
imagen = await ImagePicker.pickImage(source: ImageSource.gallery);
}
setState(() {
_imagenTemporal = imagen;
});
}
答案 0 :(得分:2)
此插件 https://pub.dev/packages/gallery_saver 将相机或网络中的图像和视频保存到本地存储(Android和iOS)
这是它的用法:
GallerySaver.saveVideo(path)
GallerySaver.saveImage(path)
path是本地路径或网络URL。
它返回Future-如果成功保存则返回true,否则返回false(出于某种原因)。
答案 1 :(得分:2)
我尝试了很多flutter插件来保存图像,只有这个插件有效。 Gallery_saver v1.0.7
但是该示例有一个小错误,这就是为什么我无法运行它。这是正确的示例:
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;
@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).then((path) {
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).then((path) {
setState(() {
secondButtonText = 'video saved!';
});
});
}
});
}
}
答案 2 :(得分:1)
您可以坚持使用google's path provider plugin拍摄的Image
:
本地存储路径:
directory = getApplicationDocumentsDirectory() // AppData folder path
directory = getExternalStorageDirectory() // main storage folders path,but only works on android as IOS is not currently supported.
path = directory.path ;
使用imagen
功能将copy
文件复制到上一步中获得的路径:
File savedImage = await imagen.copy('$path/saved_image.jpg');
可以使用您的Image
应用程序访问此方法存储的这些Files
,并根据平台在Gallery
或Photos
应用程序中建立索引。您可以找到更多有关official cookbook read and write files和path provider API documentation的信息。
答案 3 :(得分:0)
我刚刚为此编写了album_saver插件
这可以同时在IOS和Android上使用。
import 'package:album_saver/album_saver.dart';
import 'package:image_picker/image_picker.dart';
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
// Save to ablum
AlbumSaver.saveToAlbum(filePath: image.path, albumName: "YourAlbumName");
// Create album
// In Android, it will create a folder in DCIM folder
AlbumSaver.createAlbum(albumName: "YourAlbumName");
// Get DCIM folder path (just work on Android)
AlbumSaver.getDcimPath();
答案 4 :(得分:0)
*我针对此问题的解决方案是SharedPreferences-将FileName.path存储为字符串并恢复到File(path);也许这对某人有帮助*
//global variables:
File imageFile;
String finalPath;
//store string value
Future<void> storeSharedPrefs(String key, String value) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(key, value);
}
//restore string value
Future<String> restoreSharedPrefs(String key, String inputValue) async {
final prefs = await SharedPreferences.getInstance();
final value = prefs.getString(key);
if (value == null) {
return null;
}
setState(() {
inputValue = value;
});
return inputValue;
}
//load saves
void loadPrefs() async {
try {
//restore saved shared prefs:
finalPath = await restoreSharedPrefs('image', finalPath);
//load imageFile with restored shared prefs path:
this.setState(() {
if (finalPath != null) imageFile = File(finalPath);
});
debugPrint('restored path is: $finalPath');
} catch (e) {
print('loading error: $e');
}
}
@override
void initState() {
super.initState();
loadPrefs();
}
答案 5 :(得分:0)
pickImage已弃用。您应该使用ImagePicker.getImage()。
使用以下代码存储相机或图库中的照片。
//getImage(ImageSource.camera) or getImage(ImageSource.gallery)
void getImage(ImageSource imageSource) async {
PickedFile imageFile = await picker.getImage(source: imageSource);
if (imageFile == null) return;
File tmpFile = File(imageFile.path);
final appDir = await getApplicationDocumentsDirectory();
final fileName = basename(imageFile.path);
localFile = await tmpFile.copy('${appDir.path}/$fileName');
setState(() {
_image = localFile;
});
}