Take the 2-minute tour ×

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());
    } catch (Exception e) {

and using it like this :

File database = new File("/sdcard/Stam/Data/");
        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?

share|improve this question
are u sure extension is .sqlite and not .db? –  Seshu Vinay Jan 31 '12 at 11:46
i'm pretty sure –  Android-Droid Jan 31 '12 at 11:47
It's usually better never to assume that the external storage is called "/sdcard" - always use Environment#getExternalStorageDirectory(). Also, do not manually construct the path to your database file, use Context#getDatabasePath("stam_sys_tpl.sqlite"). –  Jens Jan 31 '12 at 12:41
Ok, I've just tried to use it in your way...still creating a new empty file. Is it possible to copy database file to sd card without loosing any data. that's what I need actually. I can copy an empty one from my assets folder, but i need to keep users data. –  Android-Droid Jan 31 '12 at 12:53
Are you closing the SQLiteOpenHelper you used to create the database prior to copying it? –  Jens Jan 31 '12 at 13:22
show 3 more comments

2 Answers

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

share|improve this answer
That sounds odd. Worked just fine on Android 2.3 with a simple table & straight forward copying. –  Jens Jan 31 '12 at 12:49
add comment

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

share|improve this answer
add comment

Your Answer


