Android-Droid
Android-Droid

Reputation: 14585

android copying database from internal to external storage return empty database

I'm trying to copy my databases from my internal storage to external, but it's not copying the database. It's just creating a new empty sqlite file. Any ideas what I'm missing actually?

Here is what I'm using :

    public static void copyFile(String currentDBPath, String backupDBPath){
    try {
        File sd = Environment.getExternalStorageDirectory();
        //File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            File currentDB = new File(currentDBPath);
            File backupDB = new File(backupDBPath);

            if (currentDB.exists()) {
                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

and using it like this :

File database = new File("/sdcard/Stam/Data/");
        database.mkdirs();
        RPCCommunicator.copyFile("/data/data/" + this.getPackageName()+ "/databases/stam_sys_tpl.sqlite","/sdcard/Stam/Data/system.sqlite");

So using this, it's creating the system.sqlite, but it's empty without any tables. The thing that I want to do is to move the database from it's current directory to a new one without loosing any data.

Any ideas why it's not working properly?

Upvotes: 2

Views: 1316

Answers (2)

user1438821
user1438821

Reputation: 21

Use Context.getDataBasePath() to get your db file!

Upvotes: 2

SathishBabu S
SathishBabu S

Reputation: 372

you cannot copy database file to external storage.

you can create a new database in the external storage.

query the internal database and insert in the new database you have created

Upvotes: 1

Related Questions