1

I have tried to retrieve the byte[] from my SQLite DB using the code:

public byte[] getImageData() throws SQLException{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT AVATAR_IMAGE FROM AVATAR_TABLE WHERE AVATAR_ID = 1", null);
    cursor.moveToFirst();


    byte[] blob = cursor.getBlob(1);
    return blob;
}

Error returned:

 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:241292 flg=0x1 }} to activity {com.example.physicalactivity/com.example.physicalactivity.ProfileActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

1 Answer 1

1

Your query:

SELECT AVATAR_IMAGE FROM AVATAR_TABLE WHERE AVATAR_ID = 1

returns only 1 column and since the column indices in a cursor are 0 based, you should retrieve it with:

byte[] blob = cursor.getBlob(0);

Also, you should use moveToFirst() to check if the cursor returned any rows before retrieving the column's value:

byte[] blob = null;
if (cursor.moveToFirst()) blob = cursor.getBlob(0);
return blob;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.