itinance / react-native-fs

Native filesystem access for react-native
MIT License
4.92k stars 966 forks source link

[question] save file outside app bundle - persist file after app remove #1051

Open nriccar opened 2 years ago

nriccar commented 2 years ago

Hello,

This is a simple question, not an issue.

I read the documentation and I did not find anything about this, and I tried with different file locations and the file always gets removed when the app is removed from the phone.

What I need to do is an offline backup of the app information. The intended functionality is to save this file - which Im already doing - and being able to recover it if the app is removed.

It would be great being able to share this file between different phones too.

Thanks for reading

creambyemute commented 2 years ago

@nriccar iOS will not allow you to save files outside of the AppFolder. To do so, you have to make the user pick a location where the file will be saved to/share, for example:

RNFetchBlob.ios.openDocument(myFilePathInsideApp)

I don't think RN-FS supports this so you have to resort to another module for that.

Android, beginning with Android 10 (without requestLegacyExternalStorage) and more notably Android 11 (it forces ScopedStorage) will also not allow you to save a file outside of the AppFolder. To do so, either use react-native-scoped-storage module or actionViewIntent

RNFetchBlob.android.actionViewIntent(myFilePathInsideApp, mimeType)

actionViewIntent will copy the file to the downloads folder on Android 10/11, but you have to make sure your FileProvider exposes this file to external applications (I copy it to TempDirectory (cache), actionViewIntent it and on the next run I cleanup the cache folder again)

provider_paths.xml example

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path name="cache" path="." />
</paths>

Android 9 and below will let you save for example to the Downloads folder and some more, for example RNFS.DownloadDirectoryPath

If you go for rn-fetch-blob, make sure to go with the maintained one: https://github.com/RonRadtke/react-native-blob-util

I hope this cleared up you question a bit