2

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" /> 
Ashish
  • 35
  • 5
  • Wrong path. Use an existing public directory on external storage to create your folders and files in. For manage external storage obtained your app should be able to write anywhere. – blackapps Apr 20 '23 at 13:46
  • You cannot go for manage external storage at runtime if you did not mention that in manifest file. – blackapps Apr 20 '23 at 13:48
  • @blackapps hello sir can you Please provide me a minimal code to write files in Internal storage of device. also the above code is working on emulator with Android 9 but not on a real device with Android 11. – Ashish Apr 20 '23 at 13:57
  • Your code looks ok. You only should change used path as suggested. Strange you are not reacting on the manage external storage. – blackapps Apr 20 '23 at 14:19
  • @blackapps was right, in android 10 and above we also need to add ManageExternalStorage permission in the AndroidManifest.xml file. Adding only ReadExternalStorage and WriteExternalStorage is not enough for Android9< devices. – Ashish Apr 21 '23 at 06:51

1 Answers1

1

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);
A. Rokbi
  • 503
  • 2
  • 8
  • 1
    Thank you @a-rokbi . It's the right solution, also to everyone else: add the permissions in Android Manifest too. – Ashish Apr 21 '23 at 06:52