Home > Net >  I am not able to access contents from External Storage in my React Native App
I am not able to access contents from External Storage in my React Native App

Time:02-16

I am using react-native-fs to build a media player like app but I am unable to access directories outside my project folder, I have added these permissions :

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

I am also checking the read and write permission using the following code :

    PermissionsAndroid.check(
      PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
    ).then(granted => {
      if (!granted) {
        PermissionsAndroid.requestMultiple([
          PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
          PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        ]).then(result => {
          console.log(result);
        });
      }
    });

RNFS.readDir() is throwing null error :

  const path = RNFS.ExternalStorageDirectoryPath;
  RNFS.readDir(path).then(result => {
    console.log('GOT RESULT', result);
  });

The error that is thrown is :

[Error: Attempt to get length of null array]

CodePudding user response:

Please check / try the following

  1. Allow storage permissions for the app from the device setting.
  2. Try update manifest like below by adding android:requestLegacyExternalStorage="true"

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

  • Related