2014-07-14 161 views
-1

我正试图将我创建的数据库导出到手机的SD卡上,以便我可以在eclipse的DDMS模式下查看其内容。将Android数据库保存到SD卡?

下面的代码是从这样一个问题: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())); 
+0

这个'新的ProgressDialog(null);'是不会工作的。 – njzk2

+0

(并且这两者都不是'Toast.makeText(null') – njzk2

+0

进度对话框和吐司是否需要进行备份? – RYJava2014

回答

0

它是一个的AsyncTask,所以你只需要instantieate一类并调用它,只要你想要做备份:

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

的onPostExecute()将尝试显示敬酒,所以你会需要调用从活动备份(或删除那些干杯)。

+0

谢谢,你能看到我最新的编辑在底部,我只是打电话给你的代码在那个代码之后指定? – RYJava2014

+0

是的,那可能是正确的。他们唯一可能会失败的东西,就像我告诉过你的那些调用Toast ... show(),如果你从一个Activity之外调用它;但是你可以删除 –

+0

顺便说一句,你应该接受答案,因为它已经回答你的问题。 –