0

i'm using react-native-fs package to save files to android storage.

when i perform these two lines of code, it works

await RNFS.readDir(RNFS.DocumentDirectoryPath);
=====>/data/user/0/com.youtubedljs/files
await RNFS.readDir(RNFS.CachesDirectoryPath);
=====>/data/user/0/com.youtubedljs/cache

but when i try to access external storage, like Download or documents files

await RNFS.readDir(RNFS.DownloadDirectoryPath);

i get this error

Error: Attempt to get length of null array

i already granted storage permissions to the application, it didn't work.

Update: Works fine on android 8.1, i think that the package doesn't support android 10 yet

2 Answers2

3

Need to give read-write permissions.

that's okay.. but you also need to get incode permissions, before accessing file

import {PermissionsAndroid} from 'react-native';

const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      //alert('You can use the location');
      console.log('write granted');
      return true;
    } else {
      return false;
    }
  } catch (err) {
    console.warn(err);
    return false;
  }
mainak
  • 1,886
  • 2
  • 9
  • 19
  • i added these two lines, same issue ` ` – Abdelkader Oumrani Jul 31 '20 at 13:11
  • that's okay.. but you also need to get incode permissions, before accessing file see my edited reply above – mainak Jul 31 '20 at 13:16
  • `const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { await RNFS.readDir(RNFS.DownloadDirectoryPath); } else { console.log('not granted'); }` the access is granted but the error remains **Error: Attempt to get length of null array** – Abdelkader Oumrani Jul 31 '20 at 13:27
  • @AbdelkaderOumrani were you able to fix the issue? I'm also getting this error after upgrading to android x. Would be helpful if you could share the steps to fix this. – vindev Aug 30 '20 at 14:16
  • You can’t save the file “statement19May2021.pdf” because the volume “APPLE SSD - Data” is read only Getting this error After try to copy a file in iOS Documents folder – Rover May 19 '21 at 05:34
0
First you have to ask for permission 
then you can save the into download folder of mobile
import {request, PERMISSIONS} from 'react-native-permissions';

 request(
      Platform.OS === 'ios'
        ? PERMISSIONS.IOS.MEDIA_LIBRARY
        : PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE,
    ).then(result => {
      console.log(result);
     
      );
    });

 var path = RNFS.DownloadDirectoryPath + '/filename.txt';