Create directory denied on Samsung phone even after Storage Permissions are granted

714 views
Skip to first unread message

Faraz Khan

unread,
Aug 13, 2020, 7:59:06 PM8/13/20
to Flutter Development (flutter-dev)
At the launch of my app, I create a folder in storage. To achieve that I ask for permissions within ``initState()``. Only when it completes should the directory check go forward.

    @override
      void initState(){
        super.initState();
        easyDir = Directory(widget.folderPath);
        getPermissionStatus().whenComplete(() {
          setState(() {
            //never call async operations in setState, do it in a function call.
            directoryCheck();
          });
        });
      }

the ``folderPath`` is produced by the ``permission_handler`` package. Which for my phone is ``/storage/emulated/0/Documents/easyFolder``

      //check if directory exists, create if not.
      Future<void> directoryCheck() async {
        // using awaits fixed my problem of setstate being called before
        // directory was created.. and calling setState at end
        exists = await easyDir.exists();
        if (!exists){
          await new Directory(widget.folderPath).create(recursive: true);
          exists = true;
        }
        setState(() {
    
        });
    
      }

This works as expected on the emulator. The app asks for permission at startup, waits for it to complete, and when allowed creates the directory. 

But when installed on my Samsung phone and even a friends phone (which is Samsung) the folder cannot be created due to permission denied, even after granting permission.

This is the error I receive on my Samsung phone:

    E/flutter (23275): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FileSystemException: Creation failed, path = '/storage/emulated/0/Documents' (OS Error: Permission denied, errno = 13)

I clearly gave permission at startup of app. Whats more is when I go into the appinfo of my app it shows that ``Storage`` permissions are granted.

I tried installing this on Huawei and Xiaomi phones and both worked as expected.

What am I doing wrong here?


Here is the error stack if that helps:

    E/flutter (23275): #0      _Directory.create.<anonymous closure> (dart:io/directory_impl.dart:124:11)
    E/flutter (23275): #1      _rootRunUnary (dart:async/zone.dart:1192:38)
    E/flutter (23275): #2      _CustomZone.runUnary (dart:async/zone.dart:1085:19)
    E/flutter (23275): #3      _FutureListener.handleValue (dart:async/future_impl.dart:141:18)
    E/flutter (23275): #4      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:682:45)
    E/flutter (23275): #5      Future._propagateToListeners (dart:async/future_impl.dart:711:32)
    E/flutter (23275): #6      Future._completeWithValue (dart:async/future_impl.dart:526:5)
    E/flutter (23275): #7      Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:556:7)
    E/flutter (23275): #8      _rootRun (dart:async/zone.dart:1184:13)
    E/flutter (23275): #9      _CustomZone.run (dart:async/zone.dart:1077:19)
    E/flutter (23275): #10     _CustomZone.runGuarded (dart:async/zone.dart:979:7)
    E/flutter (23275): #11     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1019:23)
    E/flutter (23275): #12     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
    E/flutter (23275): #13     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
    E/flutter (23275): 

Faraz Khan

unread,
Aug 14, 2020, 5:52:40 AM8/14/20
to Flutter Development (flutter-dev)
Missed adding the getPermissionStatus function earlier, so here it is


Future<void> getPermissionStatus() async {
_status = await Permission.storage.status;
// if permission is not granted yet, request it and update _status
if (_status != PermissionStatus.granted){
await Permission.storage.request();
_status = await Permission.storage.status;
}
}

Souvik Dutta

unread,
Aug 15, 2020, 9:06:17 AM8/15/20
to Faraz Khan, Flutter Development (flutter-dev)
Could be a bug. Better to report it here https://github.com/flutter/flutter/issues

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/201f57ec-f52c-4c1f-a23b-81261e660f21n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages