开发者

trying to export db to SDCARD

I'm pretty new to android and I'm trying to export my DB to the SDCARD so I can import it into my other appl开发者_StackOverflowication.

Any suggestions?

Thanx in advance.


Export example:

public void exportDB(){
    try {
        File sd = Environment.getExternalStorageDirectory();
        if (sd.canWrite()) {
            String currentDBPath = "data/data/com.mypack.myapp/databases/mydb.db";
            String backupDBPath = sd + "/filename.db";
            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();
    }
}

Import is very similar to export:

public void importDB(){
    try {
        File sd = Environment.getExternalStorageDirectory();
        if (sd.canWrite()) {
            String currentDBPath = sd + "/filename.db";
            String backupDBPath = "data/data/com.mypack.myapp/databases/mydb_2.db";
            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();
    }
}


Although this might not be the best solution for what you are trying to do, your database is already stored in /data/data/your.package.name. You just need to load it as a file and save to SD card. Check here for the the code that does this:

http://www.screaming-penguin.com/node/7749

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜