在Android 2.2上复制数据库时出错

时间:2013-01-27 12:34:45

标签: android sqlite android-sqlite

我在assets文件夹中有一个数据库。它在Android 4.0上工作正常,但在2.2或2.1时,它会在复制数据库时抛出错误。 这来自日志:

Data exceeds UNCOMPRESS_DATA_MAX (1532928 vs 1048576)

这是否意味着文件太大了?

OpenHelper代码:

public static class DictionaryOpenHelper extends SQLiteOpenHelper 
{
    private final Context mHelperContext;
    private SQLiteDatabase mDatabase;

    private static String DB_PATH="/data/data/com.example.enigmar/databases/";
    private static final String DB_NAME = "gesla.db";

    DictionaryOpenHelper(Context context) 
    {
        super(context, DB_NAME, null, 1);
        this.mHelperContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {

    }

    public void createDataBase() throws IOException
    {

        boolean mDataBaseExist = checkDataBase();

        if(!mDataBaseExist)
        {
            this.getReadableDatabase();
            this.close();
            try 
            {
                copyDataBase();
                Log.e(TAG, "createDatabase database created");
            } 
            catch (IOException mIOException) 
            {
                throw new Error("ErrorCopyingDataBase");
            }
        }

    }

    private boolean checkDataBase()
    {

        File dbFile = new File(DB_PATH + DB_NAME);
        return dbFile.exists();

    }

    private void copyDataBase() throws IOException
    {

        InputStream mInput = mHelperContext.getAssets().open(DB_NAME);

        String outFileName = DB_PATH + DB_NAME;

        OutputStream mOutput = new FileOutputStream(outFileName);

        byte[] mBuffer = new byte[1024];
        int mLength;
         while ((mLength = mInput.read(mBuffer))>0)
         {
             mOutput.write(mBuffer, 0, mLength);
         }
         mOutput.flush();
         mOutput.close();
         mInput.close();

        }

    public boolean openDataBase() throws SQLException
    {

        String mPath = DB_PATH + DB_NAME;
        mDatabase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        return mDatabase != null;
    }

    @Override
    public synchronized void close() {

    if(mDatabase != null)
        mDatabase.close();

    super.close();

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

1 个答案:

答案 0 :(得分:0)

您无法在资源文件夹中存储大型文件(超过1M)。将文件拆分为较小的部分或将其移动到原始文件夹。 Here the explanation how to split file.