I'm trying to read files on my device and it's external SD card but keep running into Attempt to get length of null array. I'm 100% sure the directories I'm trying to read exist and here's my code:
getPaths() = async () => {
let pathsList = []
let path = '/somewhere/on/device'
let files = getFilePaths(path)
pathsList.push(files)
console.log(pathsList[0])
return pathsList
}
// version 1
getFilePaths = (path) => {
if(RNFS.exists(path)) {
console.log('reading from:\t' + path)
try {
RNFS.readDir(path)
.then(files => {
console.log("found:\t" + files)
return files
})
.catch(err => {
console.error(err)
})
}
catch(e) {
// console.error(e)
}
}
else {return null}
}
// version 2
getFilePaths = (path) => {
console.log('reading from:\t' + path)
RNFS.readDir(path)
.then((files) => {
console.log("found:\t" + files.path)
return files
})
.catch((err) => console.error(err))
}
What am I doing incorrectly or could there be a bug with readDir()?
Solved: Allow storage permissions for the app from the device settings.
@ToJen I allow storage permissions for the app but not revolve。
@ToJen thanks,it worked for me
For anyone else facing the same issue, try this:
<manifest ... >
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>
Most helpful comment
For anyone else facing the same issue, try this: