我正在尝试在数据提交到数据库时创建内部数据库文件。当用户输入文本字段的值并单击按钮时,'SAMPLE.db'数据库文件应该在内部存储上的'MYTEST'文件内创建。这是我的代码。但它没有用。
- 主要活动oncreate -
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String Id=mEDitTxt.getText().toString();
String Unm=mEDitTxt2.getText().toString();
insertconfig(Id, Unm);
DbBackup.copyDatabase(getApplicationContext());
}
});
-DBbackup -
public class DbBackup {
public static void copyDatabase(Context c) {
String DATABASE_NAME = "SAMPLE.db";
String databasePath = c.getDatabasePath(DATABASE_NAME).getPath();
File f = new File(databasePath);
OutputStream myOutput = null;
OutputStream myOutputInternal = null;
InputStream myInput = null;
Log.d("testing", " lots db path " + databasePath);
Log.d("testing", " lots db exist " + f.exists());
if (f.exists()) {
try {
File directoryIntenal = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MYTEST/");
if (!directoryIntenal.exists())
directoryIntenal.mkdir();
myOutputInternal = new FileOutputStream(directoryIntenal.getAbsolutePath()
+ "/" + DATABASE_NAME);
myInput = new FileInputStream(databasePath);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutputInternal.write(buffer, 0, length);
}
myOutputInternal.flush();
} catch (Exception e) {
} finally {
try {
if (myOutputInternal != null) {
myOutputInternal.close();
myOutputInternal = null;
}
if (myInput != null) {
myInput.close();
myInput = null;
}
} catch (Exception e) {
}
}
}
}
}