将Android数据库保存到SD卡?

时间:2014-07-14 18:25:38

标签: java android sqlite file

我正在尝试将我正在创建的数据库导出到手机的SD卡中,这样我就可以在Eclipse的DDMS模式下查看其内容。

以下代码来自问题:Making a database backup to SDCard on Android

但是,我不确定如何在我的应用程序中使用此代码?即我需要实例化该类吗?如果是这样,在哪里?

package com.example.multapply;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {

    //Default constructor
    public ExportDatabaseFileTask() {

    }

    //delete if necessary
    private final ProgressDialog dialog = new ProgressDialog(null);


    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Exporting database...");
        this.dialog.show();
    }

    // automatically done on worker thread (separate from UI thread)
    protected Boolean doInBackground(final String... args) {

        //original database file location
        File dbFile = new File(Environment.getDataDirectory()
                + "/data/com.example.multapply/databases/MultapplyDatabase.db");

        //the destination file location
        File exportDir = new File(Environment.getExternalStorageDirectory(), "");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }


        File file = new File(exportDir, dbFile.getName());
        try {
            file.createNewFile();
            this.copyFile(dbFile, file);
            return true;
        } catch (IOException e) {
            Log.e("mypck", e.getMessage(), e);
            return false;
        }
    }

    // can use UI thread here
    protected void onPostExecute(final Boolean success) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        if (success) {
            Toast.makeText( null, "Export successful!", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(null, "Export failed", Toast.LENGTH_SHORT).show();
        }
    }

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

}

编辑(我当前用于添加到数据库的代码):

//Adding the score to the database from 

DatabaseHelper db = new DatabaseHelper(this);

db.addScore(new Score(UserName.getUserName(), score, System.currentTimeMillis() ));

1 个答案:

答案 0 :(得分:0)

它是一个AsyncTask,所以你只需要立即一个类并在你想要进行备份时调用它:

ExportDatabaseFileTask task = new ExportDatabaseFileTask();
task.execute();

onPostExecute()将尝试显示Toast,因此您需要从Activity调用备份(或删除那些Toast)。