flutterflutter-file

Flutter: Access File from specified location


As question explains itself the problem,

Below code is used to access the file, but it's returning false, but the file exists on a particular location

          Directory appDocDir = await getApplicationDocumentsDirectory();
          String appDocPath = appDocDir.path;
          File file = File('${appDocPath}/folder_name/out.mp4');
          bool value = await file.exists();
          print(value); // return false 

How could I get the right access of file here?


Solution

  • Before reading any file first we need to give access to storage permssion.

    Add Storage Permission

    permission_handler: ^5.0.1 // in pubspec.yaml file
    

    Get Access at Runtime of storage permission

       var status = await Permission.storage.status;
          if (status.isUndetermined) {
            // You can request multiple permissions at once.
            Map<Permission, PermissionStatus> statuses = await [
              Permission.storage,
            ].request();
            print(statuses[Permission.storage]); // this must show permission granted. 
          }
    

    Finally, Call

          File file = File('/storage/emulated/0/folder_name/out.mp4');
          bool value = await file.exists();
          print(value); // return false