Problems with get_blob using Cursor

0

I saved an image by converting it to a array of bytes , in a column in Sqlite with data type byte . When I make a cursor to fetch this image from the bank using the Cursor class and the get_blob() method, it says it can not convert.

What am I 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;
}
    
asked by anonymous 07.04.2015 / 22:37

1 answer

1

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

  

The value is null or the field is not Blob

Starting from the principle that is not null then it is because the column is poorly defined.

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

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

The column to be able to receive a array of bytes must be declared as Blob

CREATE TABLE tabela (_id INTEGER PRIMARY KEY, imagem BLOB, ..., ...);
    
07.04.2015 / 23:37