Is it possible to copy database file to SD card?

你。 提交于 2019-11-28 11:08:01

问题


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.

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


回答1:


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




回答2:


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();
            }
        }
    }
}


来源:https://stackoverflow.com/questions/7744827/is-it-possible-to-copy-database-file-to-sd-card

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!