【问题标题】:save image to the gallery after taken from the camera从相机拍摄后将图像保存到图库
【发布时间】:2019-11-20 18:57:44
【问题描述】:

我使用此代码通过插件 image_picker 拍照:

var image = await ImagePicker.pickImage(source: ImageSource.camera);

现在我想将图像保存在设备的图库目录中。

根据对这篇帖子How to Save Image File in Flutter ? File selected using Image_picker plugin 的接受回答,它应该是自动完成的,但我在图库中找不到拍摄的照片。

我已经将此权限添加到清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

【问题讨论】:

  • 如果您可以使用图像选择器选择图像文件,则该图像文件已经在您的设备上。为什么要复制它?
  • I use this code to take a picture with the plugin image_picker: 拍照?还是选择图像?相当混乱。那是打开相机应用吗?
  • 这是在 Android Q 上吗?
  • image.path的值是多少?
  • I would like to save the image in the gallery directory of the device 您认为设备的图库目录是什么?图库应用程序显示来自您设备上的所有图像。

标签: android flutter storage imagepicker


【解决方案1】:

您可以使用包 gallery_saver https://pub.dev/packages/gallery_saver
使用ImagePicker获取图像并使用GallerySaver.saveImage保存

代码sn-p

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!';
          });
        });
      }
    });
  }

演示

完整代码

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');
      });
    });
  }
}

【讨论】:

  • recordedImage.path 的值是多少?
  • recordedImage.path 是图像选择器临时保存这些文件的路径。
  • 是的。请举个例子,这个路径的值是这样的问题。它究竟存储在哪里?
  • 可以打印路径。因为在 ios, android, emulator 中,它们都不同的例子是 /data/0/emulated/your_app/cache
  • 谢谢。我问,因为我没有 Flutter,@Giacomo M 正在苦苦挣扎。我想知道他是否知道已经有一个文件。他没有给出 image.path 的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
  • 2017-09-20
  • 2015-10-19
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多