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

2

10

I already googling and the solution that I found are using path_provider, add permission in AndroidManifest.xml and yes I already try that. But still error in mine.

This is my function

  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);
  }

This is my 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>

And this is full of the log.

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)
Kreiker answered 7/1, 2021 at 13:20 Comment(0)
A
8

You need to modify the directory path.

Just change the code

from

String directoryPath = '$root/bozzetto_camera';

to

String directoryPath = root.path + '/bozzetto_camera';
Adolphadolphe answered 7/1, 2021 at 13:33 Comment(0)
A
0

The exception occurred because you assigned a directory, not a path, to the variable named directoryPath with the label "root." In this context, "root" represents a directory, not a path. Therefore, you should obtain the path instead of the directory. Please refer to the example below for clarification.

Convert:

String directoryPath = '$root/bozzetto_camera';

To:

String directoryPath='${root.path}/bossetto_camera';
Anglican answered 27/12, 2023 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.