未经手的异常:filesystemexception:无法打开文件,路径...(OS错误:没有这样的文件或目录,errno = 2)

发布于 2025-02-01 07:54:04 字数 1480 浏览 4 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假扮的天使 2025-02-08 07:54:04

由于没有名为 file.xlsx 的文件,此错误正在遇到,您可以检查文件是否存在,

if(file.existsSync())

如果不存在文件,您可以使用一个,

new File('$path/file.xlsx').create(recursive: true);

This error is getting because there is no file named file.xlsx you can check if file exists or not

if(file.existsSync())

if file does not exist, you can create one using,

new File('$path/file.xlsx').create(recursive: true);
凡间太子 2025-02-08 07:54:04

就我而言,这是因为我已经将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.

故事灯 2025-02-08 07:54:04

在Android 11及以下较高使用权限中,没有工具:ignore =“ scopedStorage”

   <uses-permission 
  android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

in android 11 and higher use below permission, without tools:ignore="ScopedStorage"

   <uses-permission 
  android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
双马尾 2025-02-08 07:54:04

为了扑朔迷离

final directory = await getApplicationDocumentsDirectory();

await Hive.initFlutter(directory.path);

,您需要

import 'package:path_provider/path_provider.dart';

WidgetsFlutterBinding.ensureInitialized();    
final directory = await getApplicationDocumentsDirectory();
Hive.init(directory.path);

For Flutter use

final directory = await getApplicationDocumentsDirectory();

await Hive.initFlutter(directory.path);

Of course, you need to

import 'package:path_provider/path_provider.dart';

WidgetsFlutterBinding.ensureInitialized();    
final directory = await getApplicationDocumentsDirectory();
Hive.init(directory.path);
蓝海似她心 2025-02-08 07:54:04

您也可以制作自定义名称。

String createDownloadDocName(){
    return'${fileName}-${DateTime.now().microsecond}';
  }

You could make a custom name as well.

String createDownloadDocName(){
    return'${fileName}-${DateTime.now().microsecond}';
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文