未经手的异常:filesystemexception:无法打开文件,路径...(OS错误:没有这样的文件或目录,errno = 2)
我正在尝试使用dio.download下载 *.xlsx文件,并且正在抛出错误: 未经指导的异常:fileSystemException:无法打开文件,path ='/storage/emulat/0/android/data/data/com.example.foodagator_app/files/files/files/files/file.xlsx'(OS错误:没有此类文件或目录,目录,errno = 2)
另一个try/catch块中的一个错误: filesystemexception:创建失败,路径='file:''(OS错误:读取文件系统,errno = 30)
我在AndroidManifest中写了“ AndroidManifest”的权限,用于外部存储,并且还尝试了临时目录,但它不起作用。谁能帮我吗?这是我的代码
void download() async {
var tempDir = await getExternalStorageDirectory();
File file = File(tempDir!.path + '/file.xlsx');
try {
Response response = await dio.download(
url,
file,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
),
);
var raf = file.openSync(mode: FileMode.write);
// response.data is List<int> type
raf.writeFromSync(response.data);
await raf.close();
} catch (e) {
print('Error is: $e');
}
}
void readFile() async {
var tempDir = await getExternalStorageDirectory();
var filePath = tempDir!.path + "/file.xlsx";
var bytes = File(filePath).readAsBytesSync();
var decoder = SpreadsheetDecoder.decodeBytes(bytes, update: true);
for (var table in decoder.tables.keys) {
print(table);
print(decoder.tables[table]!.maxCols);
print(decoder.tables[table]!.maxRows);
for (var row in decoder.tables[table]!.rows) {
print('$row');
}
}
}
I'm trying to download *.xlsx file using dio.download, and it's throwing the errors:
Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Android/data/com.example.foodagator_app/files/file.xlsx' (OS Error: No such file or directory, errno = 2)
Another one error from try/catch block:
FileSystemException: Creation failed, path = 'File: '' (OS Error: Read-only file system, errno = 30)
I wrote the permission in androidmanifest for external storage, and also tried temporary directory, but it's not working. Can anyone help me with this? Here is my code
void download() async {
var tempDir = await getExternalStorageDirectory();
File file = File(tempDir!.path + '/file.xlsx');
try {
Response response = await dio.download(
url,
file,
options: Options(
responseType: ResponseType.bytes,
followRedirects: false,
),
);
var raf = file.openSync(mode: FileMode.write);
// response.data is List<int> type
raf.writeFromSync(response.data);
await raf.close();
} catch (e) {
print('Error is: $e');
}
}
void readFile() async {
var tempDir = await getExternalStorageDirectory();
var filePath = tempDir!.path + "/file.xlsx";
var bytes = File(filePath).readAsBytesSync();
var decoder = SpreadsheetDecoder.decodeBytes(bytes, update: true);
for (var table in decoder.tables.keys) {
print(table);
print(decoder.tables[table]!.maxCols);
print(decoder.tables[table]!.maxRows);
for (var row in decoder.tables[table]!.rows) {
print('$row');
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于没有名为 file.xlsx 的文件,此错误正在遇到,您可以检查文件是否存在,
如果不存在文件,您可以使用一个,
This error is getting because there is no file named file.xlsx you can check if file exists or not
if file does not exist, you can create one using,
就我而言,这是因为我已经将DART文件移到了另一个文件夹中,但是我的其他文件仍在使用旧路径引用该文件,您可以使用新路径再次导入该文件,并使用“软件包:”关键字到解决此错误。
道德:不要使用相对路径在项目中的任何位置导入文件,请始终使用“软件包:”方案。
In my case, it was caused because I had moved a dart file to another folder but my other files were still referencing that file using the old path, you can import the file again using the new path and using the "package:" keyword to solve this error.
Moral: Don't use relative paths to import a file anywhere in your project, always use the "package:" scheme.
在Android 11及以下较高使用权限中,没有
工具:ignore =“ scopedStorage”
in android 11 and higher use below permission, without
tools:ignore="ScopedStorage"
为了扑朔迷离
,您需要
For Flutter use
Of course, you need to
您也可以制作自定义名称。
You could make a custom name as well.