简体   繁体   English

如何在颤振中将图像保存到图库?

[英]How to save image to gallery in flutter?

I am developing an app to crop the images and I am using image_cropper and image_picker plugins.我正在开发一个应用程序来裁剪图像,我正在使用 image_cropper 和 image_picker 插件。

I am getting the image from the image picker and then save it using the path provider to the app's documents directory.我从图像选择器获取图像,然后使用路径提供程序将其保存到应用程序的文档目录。

and everything is saved and the file exists at the documents directory.并且所有内容都被保存并且文件存在于文档目录中。

The problem happens when I try to save the image to the gallery using the image_gallery_saver plugin and it gave me the following exception:当我尝试使用 image_gallery_saver 插件将图像保存到图库时会出现问题,它给了我以下异常:

I/flutter ( 2726): /data/user/0/com.example.cropy/app_flutter/image1.png
I/flutter ( 2726): File: '/data/user/0/com.example.cropy/app_flutter/image1.png'
W/System.err( 2726): java.io.FileNotFoundException: /storage/emulated/0/Cropy/1579080598267.png: open failed: ENOENT (No such file or directory)
W/System.err( 2726):    at libcore.io.IoBridge.open(IoBridge.java:496)
W/System.err( 2726):    at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
W/System.err( 2726):    at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
W/System.err( 2726):    at kotlin.io.FilesKt__UtilsKt.copyTo(Utils.kt:205)
W/System.err( 2726):    at kotlin.io.FilesKt__UtilsKt.copyTo$default(Utils.kt:181)
W/System.err( 2726):    at com.example.imagegallerysaver.ImageGallerySaverPlugin.saveFileToGallery(ImageGallerySaverPlugin.kt:79)
W/System.err( 2726):    at com.example.imagegallerysaver.ImageGallerySaverPlugin.onMethodCall(ImageGallerySaverPlugin.kt:37)
W/System.err( 2726):    at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
W/System.err( 2726):    at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
W/System.err( 2726):    at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
W/System.err( 2726):    at android.os.MessageQueue.nativePollOnce(Native Method)
W/System.err( 2726):    at android.os.MessageQueue.next(MessageQueue.java:336)
W/System.err( 2726):    at android.os.Looper.loop(Looper.java:174)
W/System.err( 2726):    at android.app.ActivityThread.main(ActivityThread.java:7356)
W/System.err( 2726):    at java.lang.reflect.Method.invoke(Native Method)
W/System.err( 2726):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
W/System.err( 2726):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
W/System.err( 2726): Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err( 2726):    at libcore.io.Linux.open(Native Method)
W/System.err( 2726):    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
W/System.err( 2726):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
W/System.err( 2726):    at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
W/System.err( 2726):    at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7255)
W/System.err( 2726):    at libcore.io.IoBridge.open(IoBridge.java:482)
W/System.err( 2726):    ... 16 more

The method which saves the image is:保存图像的方法是:

Future saveImage(BuildContext context) async {
    await ImageGallerySaver.saveFile(fileImage.path);
    Scaffold.of(context).showSnackBar(
      SnackBar(
        backgroundColor: Colors.white,
        content: Text(
          'Image saved successfully!',
          style: TextStyle(
            color: Colors.cyan,
          ),
        ),
        action: SnackBarAction(
          label: 'OK',
          onPressed: () {
            Scaffold.of(context).hideCurrentSnackBar();
          },
        ),
      ),
    );
  }

UPDATED更新

  • fileImage.path = /data/user/0/com.example.cropy/app_flutter/image1.png fileImage.path = /data/user/0/com.example.cropy/app_flutter/image1.png

我的解决方案是使用图库保护程序包并将我的项目迁移到 AndroidX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM