How to persist backup file

nativescriptcore
file-system

#1

Hello,

I am trying to backup app data in json file so that user can restore it later on. I can save and read file fine. However, when app is uninstalled, the backup file is also removed. Here is the code:

const file = fileSystemModule.knownFolders.documents().getFile('backup.json');
file.writeText(JSON.stringify(data));

How can I make it so that backup file can always be restored later by user if he installs app again.

Thanks for the help


#2
  1. You may use ExternalStorageDirectory to save your files which will not be removed upon uninstallation.
  2. You can back up your files regularly to your Cloud server (if any) or Google Drive (Android) / iCloud (iOS).

#3

Thanks, can you please tell how to access this in nativescript or example usage of it within {N} app ? I see it is android core link instead of nativescript.

Thanks


#4

This documentation gives you an idea how you could use Java APIs in JS / TS (NativeScript).

https://docs.nativescript.org/core-concepts/android-runtime/marshalling/java-to-js

You just have to pass the path to getFile method in file system module. Make sure you acquire the external storage permission with nativescript-permissions plugin.


#7

sorry my bad, I had to give permission to app. Thanks for the pointers. :slight_smile:


#8

In case anybody else has same problem, this worked for me:

const rootPath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString();
const path = fileSystemModule.path.join(rootPath, fileName);
const file = fileSystemModule.File.fromPath(path);

file.writeText(JSON.stringify(data));