'react native fs don't read imported files

I'm using react native fs to export and import data in json format from a sqllite db. I'm writing and reading file MonthsData.json into DownloadDirectoryPath.

As long as I read and write the data through my app everything works fine.

If I download and upload the backup file from my pc with this commands

adb pull /storage/emulated/0/Download/MonthsData.json .
adb push .\MonthsData.json /storage/emulated/0/Download/

when I try to access the file

if (await getPermissions()) {
  let result = await RNFS.readFile(
    RNFS.DownloadDirectoryPath + '/' + filename,
    'utf8',
  );

async function getPermissions(): Promise<boolean> {
  let isPermitedExternalStorage = await PermissionsAndroid.check(
    PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
  );

  if (!isPermitedExternalStorage) {
    // Ask for permission
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
      {
        title: 'Storage permission needed',
        message: 'Can read ?',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      },
    );
    return granted === PermissionsAndroid.RESULTS.GRANTED;
  }

  return true;
}

I get the following error:

Error: ENOENT: /storage/emulated/0/Download/MonthsData.json: open failed: EACCES (Permission denied), open '/storage/emulated/0/Download/MonthsData.json'

If I try to list the file with the command

ls -la /storage/emulated/0/Download/

The owner and permissions are always the same both before and after the data upload

generic_x86_arm:/ $ ls -la /storage/emulated/0/Download/
total 12
-rw------- 1 u0_a149 u0_a149 6325 2022-02-20 14:34 MonthsData.json

I would appreciate every help or suggestion.



Solution 1:[1]

You may need to use the AndroidPermissions api to request the permission before attempting to read. The permissions in the android manifest are only automatically granted on older Android version

Solution 2:[2]

I found this workaround. I create the file through my application.

Push the file with a different name

adb push .\MonthsData.json /storage/emulated/0/Download/MonthsDataTemp.json

Then I connect to my android terminal

adb shell

generic_x86_arm:/ $ cd  /storage/emulated/0/Download/
generic_x86_arm:/storage/emulated/0/Download $ cat MonthsDataTemp.json > MonthsData.json

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PhantomSpooks
Solution 2 Mauro Rogledi