How to pick an external directory and read its content in react native

837 Views Asked by At

I wanted to implement a functionality where user selects an external storage directory and i read and list files in side the seleted directory. I am using react-native-document-picker library to pick the directory.

const per = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE)
const res = await DocumentPicker.pickDirectory()
const conn = await RNFS.readDir(res.uri)

i see this as the response of DocumentPicker.pickDirectory()function

{"uri": "content://com.android.externalstorage.documents/tree/primary%3ADownload%2FIRCTC%20ERS"}

but when i try to react the directory using RNFS.readDir(res.uri), I am getting below error

[Error: Folder does not exist]

i added these permissions in my AndroidManifest.xml file.


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

<application
 ...
 android:requestLegacyExternalStorage="true"
</application>

1

There are 1 best solutions below

0
Nezih Talha Karakoç On

Try res[0].uri; I think it will work.