2014-07-14 259 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

順便說一句,你應該接受答案,因爲它已經回答你的問題。 –