0

我在资产文件夹中有一个数据库。它在 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) {

    }

}
4

1 回答 1

0

您不能在资产文件夹中存储大文件(超过 1M)。将文件拆分为较小的部分或将其移动到原始文件夹。这里解释如何分割文件。

于 2013-01-27T12:45:43.663 回答