Problems with get_blob using Cursor

Asked

Viewed 25 times

0

I saved an image by turning it into a byte array, in a column in the Sqlite data-type byte.
When I do a cursor to fetch this image from the bank using the class Cursor and the method get_blob(), says he can’t convert.

What I’m doing wrong?

public List<Locais> listaDeMemorias() {

    DataBaseApp dtbApp = new DataBaseApp( this );
    SQLiteDatabase dtb = dtbApp.getReadableDatabase();

    try {
        Cursor c = dtb.rawQuery("select idLocal,dsLocal,dtVisita,dsObservacoes,imagem from Locais", null);
        Date data = new Date(System.currentTimeMillis());

        while (c.moveToNext()) {
            System.out.println("Tem dados");

            int id = c.getInt( 0 );
            System.out.println( "ID: " + id );

            byte[] imgData = c.getBlob(4);

            Bitmap img = BitmapFactory.decodeByteArray( imgData, 0,  imgData.length);


            Locais locais = new Locais(c.getInt(0),c.getString(1),new Timestamp( c.getLong( 2 )),c.getString(3),img);

            System.out.println( "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" );
            listaLocais.add(locais);
        }


        return listaLocais;

    } catch ( Exception e ) {
        System.out.println( e.getMessage() );
    } finally {
        dtb.close();
    }

    System.out.println( "Enviando lista de locais: " + listaLocais.size() );

    return listaLocais;
}
  • Show how you are using the method get_blob.

  • A common error in the use of methods get class Cursor has to do with the correct indexing of the fields. In your case you are doing it correctly.

  • I don’t know what it might be! It falls on Exception right on that line q calls the get_blob

  • How are you declaring the column image

  • Type of data: bytea

  • Byte or Blob?

  • "bytea", is a data type of sqlite

  • I don’t know this guy. Look at my answer.

  • I will try to change to see c right! Warning c be!

  • The q da message is as follows: INTEGER data in nativeGetBlob

  • Show the code where you convert to byte array and the one that writes.

  • I did it! I changed the column in sqlite to blob. But the problem was not this, at the time of entering in the bank I was inserting other information in the column image. I reviewed the code and found.

Show 7 more comments

1 answer

1

There are two reasons, according to documentation, for the method getBlob() make an exception:

value is null or field is not Blob

Assuming that it is not null then it is because the column is ill-defined.

You say in the question "in a column in Sqlite with data byte type."

I do not know if it is possible to declare a column as byte but if that’s how you declared it the problem is there.

The column to receive a byte array must be declared as Blob

CREATE TABLE tabela (_id INTEGER PRIMARY KEY, imagem BLOB, ..., ...);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.