简体   繁体   English

从相机拍摄后将图像保存到图库

[英]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.

相关问题 Android通过使用文件系统保存从相机或图库拍摄的图像 - Android save taken image from camera or gallery by using file system 拍摄相机意图照片后删除图库图像 - Deleting a gallery image after camera intent photo taken 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? 如何检查图库中的图像是从相机还是屏幕截图中拍摄的? - How to check if image in gallery was taken from camera or screenshot? 如何旋转从相机或画廊拍摄的图像? - How rotate image taken from camera or gallery.? ImageView不显示从手机摄像头或照片库拍摄的图像 - ImageView does not display image taken from phone camera or photo gallery 在上传之前,请调整从图库或照相机拍摄的图像的大小 - Resize image taken from gallery or camera, before being uploaded 从相机获取图像或从图库上传并在 ImageView (Android Studio) 中显示后,保存图像为 SharedPreferences - Save image is SharedPreferences after Image is get from camera or upload from gallery and display at ImageView (Android Studio) 如何将从相机拍摄的图像保存到内部存储器中 - How can I save image taken from camera into internal storage 如何保存从相机拍摄的图像并将其显示到列表视图-发生“ IllegalStateException”崩溃 - how to save image taken from camera and show it to listview - crashes with “IllegalStateException”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM