I am trying to get name/path of all the files and directories present on an android device through my flutter app.
Below is the code used to get the desired data :
Directory(_storageInstance.rootDirectory).list(recursive: true).listen((event) {
_storageInstance.searchList.add(SearchEntity(path: event.path,type: event.runtimeType.toString() == "_Directory" ? EntityType.DIRECTORY : EntityType.FILE));
});
Now the issue I am facing is that devices having API 30 or above don't allow access to some specific directories like the 'data' and 'obb' Directories in the 'Android' Directory. Hence in the above code whenever my Directory().list().listen() stream receives those directories, it throws the following exception :
E/flutter (28808): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: FileSystemException: Directory listing failed, path = '/storage/emulated/0/Android/data' (OS Error: Permission denied, errno = 13)
E/flutter (28808):
E/flutter (28808): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: FileSystemException: Directory listing failed, path = '/storage/emulated/0/Android/obb' (OS Error: Permission denied, errno = 13)
I tried adding try catch like this :
Directory(_storageInstance.rootDirectory).list(recursive: true).listen((event) {
try{
_storageInstance.searchList.add(SearchEntity(path: event.path,type:
event.runtimeType.toString() == "_Directory" ? EntityType.DIRECTORY : EntityType.FILE));
}
catch(e){
print("Exception Caught");
}
});
But it still is not able to handle the exception and throws the same exception as above. Usually a simple try catch work ....... maybe I am using it properly in this scenario.
How can I properly handle the exception in this scenario ?
Thankyou in advance for answering :)
UPDATE :
I tried adding the permssions in Android Manifest.xml. But it still is throwing the same exceptions.
Below is the setting tab of the app :

