2014-05-15 55 views
0

我的問題是,當數據庫從資源文件夾複製到手機的路徑,我的應用總是失敗:加載數據庫 - FileOutputStream中失敗

/data/data/at.atn.android/databases/ 

我的數據庫名稱:

atnRoutenplaner.sqlite3 

我轉讓代碼:

private void copyDataBase() throws IOException{ 

    //Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(DB_NAME); 

    // Path to the just created empty db 
    String outFileName = DB_PATH + DB_NAME; 
    File sampleFile = new File(outFileName); 
    //Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    //transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer)) > 0) { 
     myOutput.write(buffer, 0, length); 
    } 

    //Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

} 

回答

0

試試這個,

public void CopyDataBaseFromAsset() throws IOException{ 
     InputStream in = ctx.getAssets().open("mycontacts"); 
     Log.e("sample", "Starting copying"); 
     String outputFileName = DATABASE_PATH+DATABASE_NAME; 
     File databaseFile = new File("/data/data/com.copy.copydatabasefromasset/databases"); 
     // check if databases folder exists, if not create one and its subfolders 
     if (!databaseFile.exists()){ 
      databaseFile.mkdir(); 
     } 

     OutputStream out = new FileOutputStream(outputFileName); 

     byte[] buffer = new byte[1024]; 
     int length; 


     while ((length = in.read(buffer))>0){ 
       out.write(buffer,0,length); 
     } 
     Log.e("sample", "Completed"); 
     out.flush(); 
     out.close(); 
     in.close(); 

    } 
+0

對不起,沒有工作。我不明白爲什麼。我在atnRoutenplaner.sqlite中使用了一個以前的數據庫。也許這是結局? – user3552619