2012-11-09 42 views
0

用於複製數據庫的代碼複製數據庫時:SQLiteDiskIOException:磁盤I/O錯誤的Android

 public static final String DB_PATH = Environment.getDataDirectory() + "/data/MyPackageName/databases/"; 
     public static final String DB_NAME = "MyDB.sqlite"; 
     private void copyDataBase(String dbPath){ 
      try{ 

       InputStream inputStream = context.getAssets().open(dbName); 

       OutputStream appDB = new FileOutputStream(dbPath,false); 

       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = inputStream.read(buffer)) > 0) { 
        appDB.write(buffer, 0, length); 
       } 

       appDB.flush(); 
       appDB.close(); 
       inputStream.close(); 
      }catch(IOException e){ 
       e.printStackTrace(); 
      } 

     } 

這個代碼是在每一個電話,每android版做工精細,但我得到了一些電話的SQLiteDiskIOException(例如Galaxy S Plus)在上面的代碼或這一行:

SQLiteDatabase db = super.getWritableDatabase(); 

大家可以幫我解決這個問題嗎?

+1

介紹請使用'getDatabasePath()'而不是爲'DB_PATH'您當前的代碼。另外,請考慮使用'SQLiteAssetHelper'而不是滾動自己的:https://github.com/jgilfelt/android-sqlite-asset-helper – CommonsWare

+0

+1 for SQLiteAssetHelper! – leocadiotine

回答