2014-07-14 3 views
-1

나는 이클립스의 DDMS 모드에서 그 내용을 볼 수 있도록 내가 만드는 데이터베이스를 내 휴대폰의 SD 카드로 내보내려고 시도하고있다.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

백업을 만들기 위해 progressDialog와 Toast가 필요한가요? – RYJava2014

답변

0

그것에게 AsyncTask를이, 당신은 단지 클래스를 instantieate 및 백업을 수행 할 때마다 전화를해야하므로 : 당신이 활동에서 백업 전화 (또는 그 토스트를 제거)해야하므로

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

onPostExecute()는 축배를 보여주기 위해 노력할 것입니다.

+0

고마워요. 내 최신 편집을 하단에서보실 수 있습니까? 그 코드 뒤에 명시된 것입니까? – RYJava2014

+0

예, 맞을 수 있습니다. 내가 말했듯이, 실패 할 수있는 유일한 것은 Toast ... show() 호출입니다. Activity 외부에서 호출하면 실패하지만, 토스트. –

+0

그런데 이미 질문에 대한 답변을 얻었으므로 답변을 수락해야합니다. –

관련 문제