2014-02-22 33 views
1

我有一個奇怪的問題我想通過下面的代碼將數據庫從資產文件夾複製到應用程序數據庫文件夾「/ data/data/YOUR_PACKAGE/databases /」。三星手機不工作的金立手機,這是工作的罰款,我不知道爲什麼,請幫助android數據庫通過編程複製錯誤

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; 

    //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

你如何生成DB_PATH?也許它在金立移動設備上不可用(因爲製造商改變了操作系統)。

你應該使用下面的代碼,以確定outFileName

String outFileName = context.getDatabasePath(DB_NAME).getPath(); 

如果仍然拋出一個異常,請更多的細節更新您的文章。

+0

嗨我的數據庫路徑是「/ data/data/YOUR_PACKAGE/databases /」我想這是android的默認數據庫路徑。我想添加一件事,如果我寫getreadabledatabase()我得到錯誤無法打開數據庫錯誤14 – user3048027

+0

那麼,路徑是「/ data/data/YOUR_PACKAGE/databases /」在每個設備上,在每個和每個Android版本以及每個Android分支都是一個非常強大的假設。不是說這必須是問題的根源,但我強烈建議使用「getDatabasePath」代替。這也將使你的代碼更加健壯和未來的證明,因爲你更少依賴於'魔術常量',更多地依賴於框架。 – Chris

+0

好@ @ chris會做出更改,並讓你知道星期一,因爲這個項目是我的辦公室項目和源代碼在辦公室 – user3048027