在android数据/数据/ .....只获得一半的数据库

时间:2012-03-07 12:55:22

标签: android database file

我为Android应用程序创建了数据库,其大小为30 kb我已将此数据库放入资产文件夹,并在代码下面用于将其粘贴到data/data/........ 在模拟器上运行后,我只获得了数据库的1/3。是否有人早些时候遇到过这个问题。

//在此处输入代码

 private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        // database does't exist yet.
        Log.i("database", "NOT EXIST");
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDataBase()  
{
    FileInputStream myInput = null;
    String outFileName;
    FileOutputStream myOutput = null;
        try{
        // Open your local db as the input stream
        //InputStream myInput = myContext.getResources().openRawResource(R.drawable.menu);
        myInput = (FileInputStream) myContext.getAssets().open(DB_NAME);
        // Path to the just created empty db
        outFileName = DB_PATH + DB_NAME;
        // Open the empty db as the output stream
        myOutput = new FileOutputStream(outFileName);
        // transfer bytes from the inputfile to the outputfile
        try{
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }
        }catch (Exception e) {
            Log.i("catch ", "Ecxeption"+e.getMessage());
            // TODO: handle exception
        }
    }catch (Exception e) {
        // TODO: handle exception
    }finally{
        try{
            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
        catch (Exception e2) {
            // TODO: handle exception
        }
    }
}

当我在模拟器中运行此代码时,我发现我的数据库在数据/数据/ ....... 但我发现只有7 kb的数据库。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

如何改变

      while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
      }

      while ((length = myInput.read(buffer)) != -1) {
        myOutput.write(buffer, 0, length);
      }

我认为直到它达到-1

才完成流