databaseandroid-sqlitemobile-developmentreact-native-fsreact-native-sqlite-storage

Unable to copy or move file from databases folder with RNFS in react native


I have my database created at /data/data/<app_packagename>/databases/dbfile by react-native-sqlite-storage i can see this file with Android Studio device explorer and the db works fine i need to access this path by RNFS to copie the file but i get Error: ENOENT: no such file or directory

I tried:

RNFS.copyFile(path, destPath).then(() => { console.log('successfu')})

And

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

Solution

  • I found a solution for this problem that i was facing lately too

    I solved the problem by guaranteeing the access and adding a control like this :

      var path_dbMaster="/data/user/0/com.<name_your_package>/databases/db_name"
      var path_dbCopie=RNFS.DownloadDirectoryPath+'/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'
               }])
    
              } 

    and by adding the permissions in androidManifest

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

    and add android:requestLegacyExternalStorage="true" in <application .. > of AndroidManifest