나는 이클립스의 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()));
이 새로운 'ProgressDialog (null);'는 그렇게 작동하지 않을 것입니다. – njzk2
(이건'Toast.makeText (null')도 아닙니다. – njzk2
백업을 만들기 위해 progressDialog와 Toast가 필요한가요? – RYJava2014