0
如果應用程序正在運行,此活動所做的就是將數據庫從assets文件夾複製到應用程序數據庫文件夾。但數據庫僅在應用程序第二次運行後被複制!爲什麼此代碼僅在第二次運行應用程序後才能使用?
package fifth3.sem;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
@SuppressWarnings("unused")
public class Splash extends Activity {
static DBAdapter db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String destPath="/data/data/"+getPackageName()+"/databases/cryptdb2zx";
File f=new File(destPath);
File f2=new File("emptyfile");
if(!f2.exists()){
//do nothing
{
try {
Log.w("akash", "file does not exist");
CopyDB(getBaseContext().getAssets().open("cryptdb2"),new FileOutputStream(destPath));
f2.createNewFile();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
db=new DBAdapter(this);
db.open();
Thread t=new Thread(){
public void run()
{
try{
for(int i=0;i<5;i++){
Thread.sleep(1000);
}
}
catch(Exception e){
}
finally{
startActivity(new Intent("login.screen"));
}
}
};
t.start();
}
public void CopyDB(InputStream inputStream,OutputStream outputStream)throws IOException{
Log.w("akash", "copying");
byte[] buffer=new byte[1024];
int length;
while((length=inputStream.read(buffer))>0){
outputStream.write(buffer,0,length);
}
inputStream.close();
outputStream.close();
Log.w("akash", "copied");
}
}
還,數據庫被覆蓋每一個應用程序被重新啓動 – user962339
什麼做的意義(INT I時間= 0; i <5; i ++)Thread.sleep(1000); }?只要做Thread.sleep(5 * 1000); – gtiwari333
謝謝。但我不認爲這解決了我的問題。 – user962339