tharuka
tharuka

Reputation: 31

Cannot access SDcard (ExternalStorageDirectoryPath points to /storage/emulated/0)

I am developing a react native app for android, which allow user to write files to an external SDcard on Android Pie. I have used React-native fs and react-native fetch-blob libraries. The problem is that i can not get the correct path to my external SDcard. I have tried using RNFS.ExternalDirectoryPath and RNFS.ExternalStorageDirectoryPath. They all give path as "/storage/emulated/0/Android/data/com.storage/files". this path does not save file in SDcard it save the file in internal memory. I'm using Android 9 and have given permission for read and write in AndroidManifest.xml. Any suggestion to how to write file to SD card and does android Pie support read and write to external SD Card?

Upvotes: 3

Views: 885

Answers (1)

riane riri
riane riri

Reputation: 11

A solution to access this kind of files and directories with react native and RNFS is by guaranteeing the access and adding a control in the code like this :

here example of how to copie database file from db folder to externalStorageDirectory

  var path_dbMaster="/data/user/0/com.<name_your_package>/databases/db_name"
  var path_dbCopie=RNFS.ExternalStorageDirectoryPath+'/dbCopie.db'

  const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, );
       if (granted === PermissionsAndroid.RESULTS.GRANTED)
        {   
          
          RNFS.copyFile(path_dbMaster,path_dbCopie)  
          .then(result => {  
            console.log('file copied:', result);
          })
          .catch(err => {
            console.log(err);
          });
        
         } 
        else {      
           console.log('not granted');     
           
           Alert.alert(''," Permission required ",[{
            text:'OK',
            style:'cancel'
           }])

          } 

Upvotes: 0

Related Questions