Search code examples
androidfluttermobileflutter-file

How to save create a folder and save an image in Flutter?


I am trying to capture some images and save them in offline storage. I am not storing the images in app directory but instead in ...

String myNewBarcodeFolder = '/storage/emulated/0/MyApp/images';

and doing...

await Directory(myNewBarcodeFolder).create(recursive: true);

this is the error I am receiving

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Creation failed, path = '/storage/emulated/0/Prepacking' (OS Error: Permission denied, errno = 13)

And yes I've asked for the permissions in my manifest as well as in my permissions_manager.dart file

void requestAllPermission() async {
  var cameraStatus = await Permission.camera.status;
  if (!cameraStatus.isGranted) {
    await Permission.camera.request();
  }
  var writeStorageStatus = await Permission.manageExternalStorage.status;
  if (!writeStorageStatus.isGranted) {
    await Permission.manageExternalStorage.request();
  }
  var readStorageStatus = await Permission.storage.status;
  if (!readStorageStatus.isGranted) {
    await Permission.storage.request();
  }
}
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Solution

  • I tried the following code on my mobile device (Samsung A70) and work fine, which created the folders MyApp/images.

    String path = '/storage/emulated/0/MyApp/images';
    
    //Request permission
    await Permission.manageExternalStorage.request();
    
    var stickDirectory = Directory(path);
    await stickDirectory.create(recursive: true);