【问题标题】:Retrieving image from SQLite Database (BLOB data type)从 SQLite 数据库中检索图像(BLOB 数据类型)
【发布时间】:2023-03-03 02:08:02
【问题描述】:

我尝试使用以下代码从我的 SQLite DB 中检索 byte[]:

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;
}

返回错误:

 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

【问题讨论】:

    标签: java android sqlite android-sqlite blob


    【解决方案1】:

    您的查询:

    SELECT AVATAR_IMAGE FROM AVATAR_TABLE WHERE AVATAR_ID = 1
    

    仅返回 1 列,并且由于游标中的列索引基于 0,您应该使用以下命令检索它:

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

    此外,您应该在检索列的值之前使用moveToFirst() 检查游标是否返回了任何行:

    byte[] blob = null;
    if (cursor.moveToFirst()) blob = cursor.getBlob(0);
    return blob;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2012-09-24
      • 2014-02-19
      • 2021-01-28
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多