1

Possible Duplicate:
Is it possible to copy database file to SD card?

I have a database on my Android phone, and I need to get the information onto an SD card.

Is it possible to save the database file onto the SD card in a readable state? I haven't been able to find any information on how to do this. I know the name of the database, and fields etc...

I've found some examples that show how to save to SD cards, but not exactly what I need.

Some source code that copies the database file to an SD card would be perfect.

Hopefully this question is clear enough.

Community
  • 1
  • 1
Dave
  • 3,178
  • 5
  • 28
  • 44
  • In what way did [LAS_VEGAS](http://stackoverflow.com/questions/7744827/is-it-possible-to-copy-database-file-to-sd-card/7744850#7744850) not answer your question? Please do not re-ask questions -- he polite way to bring visibility to your questions is to [post a bounty](http://stackoverflow.com/faq#bounty). – sarnold Oct 14 '11 at 21:07
  • The first question was posted unintentionally. It is not a matter of politeness, but to recognize the contribution of the people who answered my question before I noticed that my browser posted before I was finished writing the question. Especially since, both questions contain good answers that deserve recognition. If I was a moderator and could merge answers I would, but I can't so I didn't. Deleting the other question would have been worse in my mind, than providing two different solutions. (One conceptual, and the second with a functional code example). – Dave Oct 17 '11 at 16:44
  • Dyarish, okay, that makes more sense, thanks. But do not overlook the [edit] button that allows you (and others) to improve a question or answer. :) – sarnold Oct 18 '11 at 21:11
  • Definitely. People are so great here, that there was a good answer in less than two minutes after the accidental post. Very valid points though, thanks sarnold. :) – Dave Oct 24 '11 at 19:01

2 Answers2

6

Yes. Here is the function that i use:

public void copyDBToSDCard() {
    try {
        InputStream myInput = new FileInputStream("/data/data/com.myproject/databases/"+DATABASE_NAME);

        File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                Log.i("FO","File creation failed for " + file);
            }
        }

        OutputStream myOutput = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+"/"+DATABASE_NAME);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
        Log.i("FO","copied");

    } catch (Exception e) {
        Log.i("FO","exception="+e);
    }


}

For a project that I worked on, I put a menu option in the home screen that I could call this function from at any time. Then, I'd move the database to my desktop and open it up with the SQLite Manager plugin for FireFox.

SBerg413
  • 14,515
  • 6
  • 62
  • 88
4

Sure. If this is a database that exists in your app, you can get a reference to the db file via Context.getDatabasePath(), passing it the database name. From there, it's just a routine file copy operation:

//Get a reference to the database
File dbFile = mContext.getDatabasePath("mydb");
//Get a reference to the directory location for the backup
File exportDir = new File(Environment.getExternalStorageDirectory(), "myAppBackups");
if (!exportDir.exists()) {
  exportDir.mkdirs();
}
File backup = new File(exportDir, dbFile.getName());
//Check the required operation String command = params[0];

//Attempt file copy
try {
  backup.createNewFile();
  fileCopy(dbFile, backup);
} catch (IOException e) {
  /*Handle File Error*/
}

Where the method fileCopy() is defined as:

private void fileCopy(File source, File dest) throws IOException {
  FileChannel inChannel = new FileInputStream(source).getChannel();
  FileChannel outChannel = new FileOutputStream(dest).getChannel();
  try {
    inChannel.transferTo(0, inChannel.size(), outChannel);
  } finally {
    if (inChannel != null) inChannel.close();
    if (outChannel != null) outChannel.close();
  }
}

HTH!

devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Thanks Devunwired! I wish I could mark two answers as correct because this is a good solution also. Definitely worth an upvote. Cheers! – Dave Oct 13 '11 at 00:11
  • I wish this could be marked as an answer; its much clean and precise. – Ali_Waris Jan 09 '18 at 08:47