Java 将Android数据库保存到SD卡?

Java 将Android数据库保存到SD卡?,java,android,sqlite,file,Java,Android,Sqlite,File,我正在尝试将我正在创建的数据库导出到我手机的SD卡上,这样我就可以在eclipse的DDMS模式下查看它的内容 以下代码来自问题: 但是,我不确定如何在我的应用程序中使用此代码?例如,我是否需要实例化该类?如果是,在哪里 package com.example.multapply; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOExce

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

以下代码来自问题:

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

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

}

这是一个异步任务,因此只要实例化一个类,并在需要备份时调用它即可:

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

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

新建ProgressDialog(null)
是如此的不起作用。(这也是Toast.makeText(null)为了进行备份,progressDialog和Toast甚至是必要的吗?谢谢,你能看到我在底部的最新编辑吗?我能在代码之后调用你指定的代码吗?是的,那可能是对的。正如我告诉你的,唯一可能失败的是那些调用Toast…show()如果您从活动外部调用此功能,但您可以删除祝酒词。顺便说一句,您应该接受答案,因为它已经回答了您的问题。此外,文件将保存在哪里?例如,在DDMS中我将在哪里找到它?您的问题已经得到了回答。现在您只需要有人为您解释所有代码。这不是poi新界。
ExportDatabaseFileTask task = new ExportDatabaseFileTask();
task.execute();