Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter how save a picture from camera to device?

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;
  });
}
like image 292
lorena Avatar asked Mar 08 '19 07:03

lorena


4 Answers

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).

like image 68
jelenap Avatar answered Nov 08 '22 10:11

jelenap


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 using copy fuction:

    File savedImage = await imagen.copy('$path/saved_image.jpg');
    

These Images 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.

like image 21
Mazin Ibrahim Avatar answered Nov 08 '22 10:11

Mazin Ibrahim


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();
like image 5
Thắng Mai Avatar answered Nov 08 '22 11:11

Thắng Mai


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!';
          });
        });
      }
    });
  }
}
like image 2
sazzlebells Avatar answered Nov 08 '22 12:11

sazzlebells