【问题标题】:Cannot copy file to local storage android flutter无法将文件复制到本地存储android颤动
【发布时间】:2021-11-02 05:55:57
【问题描述】:

我正在尝试通过 image_picker 从图库或相机中选择图像,并将图像保存在特定位置。当我从相机中选择图像时,我的代码可以完美运行,但是当我在保存时从图库中选择时,它会出现异常。这是我的代码

onPressed: () async {
  final XFile? image = await picker.pickImage(source: ImageSource.gallery);
  File tempFile = File(image.path);
  tempFile = await tempFile.copy('storage/emulated/0/$image.name');
}

上面的代码通过异常

[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot copy file to 'storage/emulated/0//6544578463230218846.jpg', path = '/data/user/0/com.example.nysu/cache/image_picker6544578463230218846.jpg' (OS Error: No such file or directory, errno = 2)




  

【问题讨论】:

    标签: flutter filesystems imagepicker


    【解决方案1】:

    首先确保您拥有所需的权限: 例如,对于 Android,您需要:

    此外,额外的“/”文件路径似乎有点奇怪,如果不是故意的,您可能需要修复它: '存储/模拟/0//6544578463230218846.jpg'

    像这样:

    tempFile = await tempFile.copy('storage/emulated/0$image.name');
    

    【讨论】:

      【解决方案2】:

      我的应用程序的基本流程是,首先我从图库或相机中选择图像,然后我输入用户的图像名称,然后我将用户提供的名称的图像保存到本地存储。

      所以在选择图像的间隔和输入名称之后,图像选择器的缓存会丢失一段时间,因为这个(操作系统错误:没有这样的文件或目录,errno = 2)发生。

      为了解决这个问题,我在选择图像之前输入名称,并在选择它们后立即保存图像。效果很好。

      【讨论】:

        猜你喜欢
        • 2016-12-30
        • 2019-08-13
        • 1970-01-01
        • 2019-10-02
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多