Flutter: Unhandled Exception: FileSystemException: Creation failed, path = 'Directory: '' (OS Error: Read-only file system, errno = 30)

Flutter: Unhandled Exception: FileSystemException: Creation failed, path = 'Directory: '' (OS Error: Read-only file system, errno = 30)

我已经用谷歌搜索了,我找到的解决方案是使用 path_provider,在 AndroidManifest.xml 中添加权限,是的,我已经尝试过了。但是我的还是出错了。

这是我的功能

  Future<File> _takePicture() async {
    Directory root = await getTemporaryDirectory(); // this is using path_provider
    String directoryPath = '$root/bozzetto_camera';
    await Directory(directoryPath).create(recursive: true); // the error because of this line
    String filePath = '$directoryPath/${DateTime.now()}.jpg';
    try {
      await _cameraController.takePicture(filePath);
    } catch (e) {
      return null;
    }
    return File(filePath);
  }

这是我的AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        ...
        android:requestLegacyExternalStorage="true">
        
        ...

    </application>

这是满满的日志。

E/flutter (14879): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FileSystemException: Creation failed, path = 'Directory: '' (OS Error: Read-only file system, errno = 30)
E/flutter (14879): #0      _Directory.create.<anonymous closure> (dart:io/directory_impl.dart:117:11)
E/flutter (14879): #1      _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter (14879): #2      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (14879): #3      _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
E/flutter (14879): #4      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
E/flutter (14879): #5      Future._propagateToListeners (dart:async/future_impl.dart:725:32)
E/flutter (14879): #6      Future._completeWithValue (dart:async/future_impl.dart:529:5)
E/flutter (14879): #7      Future._asyncCompleteWithValue.<anonymous closure> (dart:async/future_impl.dart:567:7)
E/flutter (14879): #8      _rootRun (dart:async/zone.dart:1190:13)
E/flutter (14879): #9      _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter (14879): #10     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter (14879): #11     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter (14879): #12     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter (14879): #13     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

您需要修改目录路径

只需更改代码

来自

String directoryPath = '$root/bozzetto_camera';

String directoryPath = root.path + '/bozzetto_camera';