Reputation: 2688
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 :
Upvotes: 2
Views: 5129
Reputation: 4095
Add this line inside mainifest
tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You have to add this line of code in android/app/src/debug/AndroidMainifest.xml
which is for the debug version and in android/app/src/main/AndroidMainifest.xml
.
So,
<manifest ...>
// Here must be lots of other codes
// Paste those lines after all those codes, here just above </mainifest>
</manifest>
If that is still not clear. Here is a example of AndroidMainifest.xml
from my project in debug
directory.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.do_app">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
*Note:- You have to run from the start after you make those changes.
Upvotes: 1