当我尝试卸载我的Android应用并再次重新安装以获取新的更新数据库时,数据库有时无法更新,我不知道为什么。 尝试安装/卸载的随机尝试次数后,数据库会更新
有人可以帮我解决这个问题。
我尝试的是: 卸载时,首先强制停止应用程序,然后清除数据然后卸载。
这是应用程序启动时在MainActivity中执行的代码。
try {
File dbFile = getDatabasePath("MyDatabase.db");
if (!dbFile.exists()) {
Log.d(TAG, "Datbase does not exist");
this.copy("MY.db",dbFile.getAbsolutePath());
}
}
catch (Exception e) {
Log.e(TAG, "Error in Copying: "+e.toString());
try {
AppLogging(TAG, "Error in copying Database:" + e.toString());
}
catch(Exception l) {
}
}
复制方法
private void copy(String file, String folder) throws IOException {
try {
File CheckDirectory;
CheckDirectory = new File(folder);
String parentPath = CheckDirectory.getParent();
File filedir = new File(parentPath);
if (!filedir.exists()) {
if (!filedir.mkdirs()) {
return;
}
}
InputStream in = this.getApplicationContext().getAssets().open(file);
File newfile = new File(folder);
OutputStream out = new FileOutputStream(newfile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
catch (Exception e) {
Log.e(TAG, "Error in Copy: "+e.toString());
try {
AppLogging(TAG, "ERROR In Copy Method (Database) " + e.toString());
}
catch(Exception l) {
}
}
}