我想将图像复制到应用程序目录,但是我总是会收到此错误:
[错误:flutter / shell / common / shell.cc(181)] Dart错误:未处理 例外:E / flutter(4159):FileSystemException:无法将文件复制到 '/data/user/0/com.vendetta.recipe/app_flutter',路径= '/storage/emulated/0/Android/data/com.vendetta.recipe/files/Pictures/a6fd32a9-60b2-4cff-8f10-2ffc2933bf751208556873045090039.jpg' (操作系统错误:是目录,错误号= 21)E / flutter(4159):#0
_File.copy。 (dart:io / file_impl.dart:340:9)E / flutter(4159):#1 _RootZone.runUnary (dart:async / zone.dart:1379:54)E / flutter(4159):#2
_FutureListener.handleValue(dart:async / future_impl.dart:129:18)E / flutter(4159):#3
Future._propagateToListeners.handleValueCallback (dart:async / future_impl.dart:642:45)E / flutter(4159):#4
Future._propagateToListeners(dart:async / future_impl.dart:671:32) E / flutter(4159):#5 Future._completeWithValue (dart:async / future_impl.dart:486:5)E / flutter(4159):#6
Future._asyncComplete。 (dart:async / future_impl.dart:516:7)E / flutter(4159):#7
_microtaskLoop(dart:async / schedule_microtask.dart:41:21)E / flutter(4159):#8 _startMicrotaskLoop (dart:async / schedule_microtask.dart:50:5)
我想复制此图像,以确保在保存时用户没有删除重要文件。我想将其存储在数据库中。
这是我复制图片的代码,该代码是使用image_picker拍摄的:
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path;
File newImage = await _image.copy('$path');
我希望有人能够解决我的问题。
答案 0 :(得分:2)
path
是目录/data/user/0/com.vendetta.recipe/app_flutter
。尝试在其中添加一个/filename.jpg
。
File newImage = await _image.copy('$path/filename.jpg');
答案 1 :(得分:0)
请尝试此操作,然后您的问题即可解决
我提供给您的还可以动态复制任何扩展名的文件
遵循这些步骤-
1。首先添加此导入
import 'package:path/path.dart' as path;
2。获取带有扩展名的文件名
var basNameWithExtension = path.basename(sourceFile.path);
3。然后将扩展名传递给
var file = await moveFile(sourceFile,newPath+"/"+basNameWithExtension);
4。然后编写此方法
Future<File> moveFile(File sourceFile, String newPath) async {
try {
/// prefer using rename as it is probably faster
/// if same directory path
return await sourceFile.rename(newPath);
} catch (e) {
/// if rename fails, copy the source file
final newFile = await sourceFile.copy(newPath);
return newFile;
}
}