【问题标题】:How to read a blob type stored in sqlite database?如何读取存储在 sqlite 数据库中的 blob 类型?
【发布时间】:2021-09-18 19:35:35
【问题描述】:

我成功地在 SQLite 数据库中存储了一个 blob 类型,但我以后无法正确检索它:

我是这样做的:

byte[] miniatura= cursor.getBlob(cursor.getColumnIndex(MySqliteHelper.thumbnail));

但它失败了:

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

尽管有消息,但我认为光标不是问题,好像我将提到的指令更改为:

String cadena= cursor.getString(cursor.getColumnIndex(MySqliteHelper.fieldofTypeText));

cadena 具有应有的价值。

那么,我该怎么做才能正确获得 Blob 值?

【问题讨论】:

    标签: java android sqlite blob


    【解决方案1】:

    我已将图像存储在 SQLite 数据库中并显示在图像视图中

    为此,我已将此代码添加到数据库类中

     public byte [] get(String query){
            try {
    
                SQLiteDatabase db = this.getReadableDatabase();
                Cursor cursor = db.rawQuery(query,null);
    
                if (cursor.moveToFirst()){
                    return  cursor.getBlob(0);
                }
                return null;
    
            } catch (Exception e){
                e.printStackTrace();
                return null;
            }
        }
    
    

    为了在图像视图中显示图像,我使用了这个。

        db_class = new DatabaseClass(MainActivity.this);
                byte [] img = db_class.get("SELECT IMG FROM data WHERE id = 1");
                if (img != null){
                    Bitmap bitmap = convertbytearyrtobitmap(img);
                    imageView.setImageBitmap(bitmap);
                }
    

    我为我的作业做了这个,你可以在 github 上看到我的代码

    https://github.com/dharmik953/krishi_network_internship_project/blob/master/app/src/main/java/com/dharmik953/krishinetwork/MainActivity.java

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 2017-08-19
      • 2016-04-22
      • 1970-01-01
      相关资源
      最近更新 更多