react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

Cannot write to SD card outside of app dir

Open ascorbic opened this issue 8 years ago • 6 comments

Hi. Thanks for the plugin. I am having an issue, which I'm assuming is permissions-related. I need to save some data on a physical SD card that persists after the app is removed, so I need to save it outside of the Android/data/com.example/ directory. My app will only ever be installed on one type of device, so I can hard-code the path. I have no trouble writing to the apps' private directory on the card, but if I try to create a directory elsewhere then it fails.

My manifest includes:

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

..and checking dynamically shows the permission as available:

  const directory = await getSdCardDirectory();
// /mnt/m_external_sd/Android/media/hrp
  if (!await RNFS.exists(directory)) {
    console.log("doesn't exist", directory);
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, {
          title: "Grant SD card access",
          message: "We need access",
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("Permission OK");
      } else {
        console.log("Permission failed");
      }
      if (await RNFS.mkdir(directory)) {
        console.log("Directory created", directory);
      } else {
        console.warn("Could not create", directory);
      }
    } catch (err) {
      console.warn("Create directory error", err);
    }

  }

This shows "Permission OK", but throws "Error: Directory could not be created"

Can anyone help? Thanks.

ascorbic avatar Jan 17 '18 12:01 ascorbic

Same problem, the first time created the folder successfully, but after I erased the folder this error appear to me everytime.

albertosantiago avatar May 29 '18 19:05 albertosantiago

Same problem, my client wants to save data on device so that app can work in offline mode. For this i need to download large files on device. I was looking if there is a way to save file directly to SD Card. but i did not find any solution for this in this plugin.

vikasds avatar Dec 19 '18 11:12 vikasds

no solution yet :D ?

Ashkan-Oliaie avatar Jul 14 '19 14:07 Ashkan-Oliaie

+1

luandro avatar Oct 16 '19 16:10 luandro

+1

maria-antignolo avatar Feb 03 '20 18:02 maria-antignolo

+1

YogiHa avatar Mar 18 '21 09:03 YogiHa