flutter - 无法将文件复制到本地存储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)
解决方案
首先确保您拥有所需的权限:例如对于 Android,您需要:
此外,额外的“/”文件路径似乎有点奇怪,如果不是故意的,您可能需要修复它:'storage/emulated/0//6544578463230218846.jpg'
像这样:
tempFile = await tempFile.copy('storage/emulated/0$image.name');
推荐阅读
- python - 如何使用浏览器开发者工具从 POST 方法中获取数据
- java - Java Eclipse 错误:无法创建 Java 虚拟机
- html - 加载另一个组件而不是 root-app-component
- javascript - Print json object to nested html table
- java - 使用 hatos 将 _links ,_refs 添加到 Spring 应用程序
- node.js - 如何使用 NodeJs 中的 FS 将文件从一个文件夹复制到另一个文件夹?
- wordpress - 有没有办法集中访问 wordpress 进行共同开发?
- javascript - 如何创建一个在语法上自动调用的函数?
- javascript - paypal 与 ruby on rails 集成的“zoid 破坏了所有错误”
- java - 如何使用 string.format 在下面给出字符串?