异常解决:android.database.sqlite.SQLiteBlobTooBigException: Row too big to fit into CursorWindow

异常log如下:

09-08 18:21:35.093 23269 23269 E SQLiteQuery: exception: Row too big to fit into CursorWindow requiredPos=0, totalRows=1; query: SELECT * FROM music_table WHERE id = ?
09-08 18:21:35.093 23269 23269 W System.err: android.database.sqlite.SQLiteBlobTooBigException: Row too big to fit into CursorWindow requiredPos=0, totalRows=1
09-08 18:21:35.094 23269 23269 W System.err:    at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
09-08 18:21:35.094 23269 23269 W System.err:    at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:1024)
09-08 18:21:35.094 23269 23269 W System.err:    at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:838)
09-08 18:21:35.094 23269 23269 W System.err:    at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
09-08 18:21:35.094 23269 23269 W System.err:    at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:145)
09-08 18:21:35.094 23269 23269 W System.err:    at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:132)
09-08 18:21:35.095 23269 23269 W System.err:    at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:238)
09-08 18:21:35.095 23269 23269 W System.err:    at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:277)
09-08 18:21:35.096 23269 23269 W System.err:    at android.view.View.performClick(View.java:7511)
09-08 18:21:35.096 23269 23269 W System.err:    at android.view.View.performClickInternal(View.java:7484)
09-08 18:21:35.096 23269 23269 W System.err:    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
09-08 18:21:35.096 23269 23269 W System.err:    at android.view.View$PerformClick.run(View.java:29487)
09-08 18:21:35.096 23269 23269 W System.err:    at android.os.Handler.handleCallback(Handler.java:942)
09-08 18:21:35.096 23269 23269 W System.err:    at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 18:21:35.096 23269 23269 W System.err:    at android.os.Looper.loopOnce(Looper.java:201)
09-08 18:21:35.096 23269 23269 W System.err:    at android.os.Looper.loop(Looper.java:288)
09-08 18:21:35.097 23269 23269 W System.err:    at android.app.ActivityThread.main(ActivityThread.java:8061)
09-08 18:21:35.097 23269 23269 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
09-08 18:21:35.097 23269 23269 W System.err:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:703)
09-08 18:21:35.097 23269 23269 W System.err:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)

问题如何引起:解析出音乐专辑图片时,需要把图片转byte数组存入数据库,当下次查询数据库该专辑数据时报上面错误,行太大而无法写入CursorWindow(CursorWindow默认的最大值为2MB)

解决方法1:

Cursor cursor = db.query(TABLE, null, "id=?", new String[]{id}, null, null, null);
设置一下CursorWindow的大小即可
CursorWindow cw = new CursorWindow("test", 5000000);
AbstractWindowedCursor ac = (AbstractWindowedCursor) cursor;
ac.setWindow(cw);

第二个参数cursorWindowSize设置大小, 这里修改CursorWindow的大小为5M

解决方法2:

图片入库数据库时,先对图片进行压缩处理,尽量控制其大小在2M内

private byte[] imagemTratada(byte[] imagem_img){

    while (imagem_img.length > 500000){
        Bitmap bitmap = BitmapFactory.decodeByteArray(imagem_img, 0, imagem_img.length);
        Bitmap resized = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth()*0.8), (int)(bitmap.getHeight()*0.8), true);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        resized.compress(Bitmap.CompressFormat.PNG, 100, stream);
        imagem_img = stream.toByteArray();
    }
    return imagem_img;

}

你可能感兴趣的:(Android,android,数据库)