flutter how save a picture from camera to device?
Solution 1
You can persist the Image
taken using the google's path provider plugin:
-
Path to local storage:
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 ;
-
Copy
imagen
file to the path you got in the previous step usingcopy
fuction:File savedImage = await imagen.copy('$path/saved_image.jpg');
These Image
s stored by this methodology can be accessed using your Files
application and indexed in your Gallery
or Photos
app depending on the platform.You can find more information on official cookbook read and write files and path provider API documentation.
Solution 2
This plugin https://pub.dev/packages/gallery_saver saves images and videos from camera or network to local storage(both Android and iOS)
This is how it's used:
GallerySaver.saveVideo(path)
GallerySaver.saveImage(path)
path is local path or network url.
It returns Future - true if it was saved successfully and false if it wasn't(for any reason).
Solution 3
I've just written album_saver plugin for this
This can work on both IOS and 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();
Solution 4
I tried a lot of flutter plugins to save image, and only this one works. Gallery_saver v1.0.7
But the example has a little error thats why i wasn't able to run it. Here's the correct example:
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!';
});
});
}
});
}
}
Solution 5
pickImage is deprecated. You should use ImagePicker.getImage().
Use below code to store the photo from camera or gallery.
//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;
});
}
lorena
Updated on June 04, 2022Comments
-
lorena 6 months
I'm trying to save a picture from camera in a real device, but can't find a way.
By now I get it saved in a File, but can't get it in the gallery..
My code at this moment is:
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; }); }
-
Mazin Ibrahim over 3 yearsThen use the
getApplicationDocumentsDirectory()
, but you have to test whether the image will appear in yourPhotos
app. -
Cold_Class about 3 yearsThis was by far the easiest solution for me, thx - works on my android phone
-
Thắng Mai about 3 years@MuhammadFaizan Yes, It will work with
ImageSource.camera
-
Mohammed H. Hannoush 12 monthsHow could you change image name?