flutterdartdirectoryandroid-11hidden-files

Cannot access hidden folder on android version 11 device in flutter


I am making an app that shows WhatsApp statuses. The WhatsApp status is stored on a hidden file called ".statuses" in the WhatsApp folder. But when I try to get a list of subdirectories and files from the .statuses folder I get an empty string. This problem occurs only on android version 11 devices. The code is working perfectly fine for folders that are not hidden and for devices that have an android version less than 11. My code is attached below:

final Directory _photoDir =
    Directory(
        '/storage/emulated/0/Android/media/com.whatsapp/WhatsApp/Media/.Statuses');

var imageList = _photoDir
          .listSync()
          .map((item) => item.path)
          .where((item) => item.endsWith(".jpg"))
          .toList(growable: false);

print(imageList);

Solution

  • This problem arises when our "target SDK version" is 30 (Android 11) or greater

    We have to include this permission in "android manifest" file:

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    

    If you are using "permission_handler" package like me you can display the permission prompt in this way:

     void requestStoragePermission() async {
        Map<Permission, PermissionStatus> result = await [
          Permission.storage,
          Permission.manageExternalStorage,
        ].request();
    
        //if permission is granted only then show home screen else permission error screen is already shown
        if (result[Permission.storage] == PermissionStatus.granted &&
            result[Permission.manageExternalStorage] == PermissionStatus.granted) {
          setState(() {});
        }
      }