React-native-fs: RNFS.readDir(): Attempt to get length of null array

Created on 19 Mar 2018  Â·  4Comments  Â·  Source: itinance/react-native-fs

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()?

Most helpful comment

For anyone else facing the same issue, try this:

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

All 4 comments

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>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MuppaAkhil picture MuppaAkhil  Â·  3Comments

7korobi picture 7korobi  Â·  4Comments

AgNm picture AgNm  Â·  4Comments

wmonecke picture wmonecke  Â·  3Comments

CesarLanderos picture CesarLanderos  Â·  4Comments