首页 > 解决方案 > 无法将文件复制到本地存储android颤动

问题描述

我正在尝试通过 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)




  

标签: flutterfilesystemsimagepicker

解决方案


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

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

像这样:

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

推荐阅读