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

How to copy files from Android bundle?

Open NunoCardoso opened this issue 8 years ago • 6 comments

Hi.

I am bundling a sqlite3 file with my react-native app.

For iOS, I can access easily by adding the file in XCode, and then access it with RNFS.MainBundlePath (I want to copy it to the Documents area so I can write on it)

How to do this on Android?

NunoCardoso avatar Oct 06 '17 14:10 NunoCardoso

i am also bundling sqlite-files with my rn-apps using this practice from react-native-sqlite-storage: https://github.com/andpor/react-native-sqlite-storage/#setting-up-your-project-to-import-a-pre-populated-sqlite-database-from-application-for-ios

have you tried this? works out of the box if everything was done right.

itinance avatar Oct 06 '17 15:10 itinance

Aren't those instructions for iOS only? I want to do this for Android, my iOS version is working fine.

NunoCardoso avatar Oct 06 '17 15:10 NunoCardoso

Got your point now. There is a small sentence stating:

"For Android, the www directory is always relative to the assets directory for the app: src/main/assets"

Putting the database into the assets directory will let you create a database copied over from the prebundled one.

itinance avatar Oct 06 '17 15:10 itinance

template.sqlite:

screen shot 2017-10-06 at 17 16 11

    let params = { name: DATABASE_NAME + mandantId, createFromLocation: "~template.sqlite" }
    if (Platform.OS === 'ios') {
        params.location = 'Library';
    }

    return new Promise((resolve, reject) => {
        SQLite.openDatabase(params, (db) => {
            __db = db;
            __isDatabaseOpen = true;

            resolve(db)
        }, (err) => reject(err));
    })

itinance avatar Oct 06 '17 15:10 itinance

Ok, will give a try.

Still, regarding my original question, which is to copy files from the app bundle to the dedicated app's space... that is possible with iOS, but for Android, I then assume it is not be cause react-native-fs MainBundlePath doesn't return anything.

On Fri, 6 Oct 2017 at 17:16, Hagen Hübel [email protected] wrote:

[image: screen shot 2017-10-06 at 17 16 11] https://user-images.githubusercontent.com/1758597/31284851-1c423b9a-aaba-11e7-8698-c945f6432151.png

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/itinance/react-native-fs/issues/359#issuecomment-334785001, or mute the thread https://github.com/notifications/unsubscribe-auth/AAgE2AvN3UvvCPjum1Ob5YZpCQne-DxXks5spkRbgaJpZM4Pwmhg .

NunoCardoso avatar Oct 09 '17 10:10 NunoCardoso

Ok, will give a try. Still, regarding my original question, which is to copy files from the app bundle to the dedicated app's space... that is possible with iOS, but for Android, I then assume it is not be cause react-native-fs MainBundlePath doesn't return anything.

Did you solve it? Did you manage to open the database? I have a similar problem on android

livinggston avatar Jul 30 '23 18:07 livinggston