开发者

Is it possible to copy database file to SD card?

I have a database on my Android phone, and I开发者_如何学Go 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.

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


The database file is just like any other file, if you make a binary file copy it will work.

Java has no built in file copy method, so you can use this:

Standard concise way to copy a file in Java?

Just don't forget to add your manifest permission to write to the SD card:
Permission to write to the SD card


Here's a script I've bastardized from several other users on SO. It looks like you can tell android where to store the file, but when you go into the phone with adb shell you might have a hard time finding it!

This code (which I mapped to a temporary button in my action bar for debugging) would print something like: "database saved to: /storage/emulated/0/DB-DEBUG/todotable.db", but going into the shell on my phone I actually found my database at: "/storage/emulated/legacy/DB-DEBUG/"... not sure what's up with that, but now I can check out my database with an sqlite browser!

//db will reside in: /storage/emulated/legacy/DB_DEBUG
private void copyDatabase(Context c, String DATABASE_NAME) {
    String databasePath = c.getDatabasePath(DATABASE_NAME).getPath();
    File f = new File(databasePath);
    OutputStream myOutput = null;
    InputStream myInput = null;
    Log.d("testing", " testing db path " + databasePath);
    Log.d("testing", " testing db exist " + f.exists());

    if (f.exists()) {
        try {
            File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/DB-DEBUG");
            if (!directory.exists()){
                directory.mkdir();
            }

            String copyPath = directory.getAbsolutePath() + "/" + DATABASE_NAME;
            myOutput = new FileOutputStream(copyPath);
            myInput = new FileInputStream(databasePath);

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

            myOutput.flush();
            Toast.makeText(getBaseContext(), "Your database copied to: " + copyPath, Toast.LENGTH_LONG).show();
            Log.d("testing", " database saved to: " + copyPath);

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();

        } finally {
            try {
                if (myOutput != null) {
                    myOutput.close();
                    myOutput = null;
                }
                if (myInput != null) {
                    myInput.close();
                    myInput = null;
                }
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜