2011-10-11 30 views
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"); 
} 

}

+0

還,數據庫被覆蓋每一個應用程序被重新啓動 – user962339

+0

什麼做的意義(INT I時間= 0; i <5; i ++)Thread.sleep(1000); }?只要做Thread.sleep(5 * 1000); – gtiwari333

+0

謝謝。但我不認爲這解決了我的問題。 – user962339

回答

0

沒有看到你的數據庫適配器,我不能肯定,但可能是因爲您嘗試目錄存在之前複製的分貝它不工作的第一次。 通常,如果數據庫適配器不存在,數據庫適配器將創建一個數據庫。這將創建'數據庫'目錄。因此,當您打開新安裝的應用程序時,該目錄不存在,並且複製失敗。但是,在複製邏輯之後,立即創建一個數據庫適配器,並且它(可能)會創建數據庫(以及包含它的目錄)。因此,第二次運行時,複製成功。

這應該跑,你想(順序是什麼是重要的):

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    db=new DBAdapter(this); 
    db.open(); 

    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(); 
     } 
    } 
    } 

} 
+0

這是否回答您的查詢? – Ian

相關問題