-1

I am able to save the images in the database. The issue i have now is with retrieve it and making use of it in the code.

Look at my DATABAse handler get method:

ImageClass getImage(int state_id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(DATABASE_TABLE_NAME, new String[]{STATE_ID, STATE_NAME, STATE_IMAGE}, STATE_ID + "=?",
            new String[]{String.valueOf(state_id)}, null, null, null, null);
    if (cursor != null);
    cursor.moveToFirst();

        ImageClass imageClass = new ImageClass(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getBlob(1));

        cursor.close();



    return imageClass;
}

Look at the code i used in main activity..

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageClass imageClass = db.getImage();
            byte[]in = imageClass._image;
                Bitmap imah = Utils.getImage(in);


                img.setImageBitmap(imah);

            }
        });
    } catch(IOException e) {
        System.out.println(e);
    }
}

This is my image Activity

public class ImageClass {
int _id;
byte[] _image;
String _state_name;


public ImageClass(){

}

public ImageClass(int state_id,String state_name, byte[] state_image) {
    this._id = state_id;
    this._state_name = state_name;
    this._image = state_image;

}

public ImageClass(String state_name, byte[]state_image){
    this._state_name = state_name;
    this._image = state_image;
}
public ImageClass(int state_id, byte[]state_image){
    this._id = state_id;
    this._image= state_image;
}

public int get_id() {
    return this._id;
}

public void set_id(int state_id) {
    this._id = state_id;
}

public byte[] get_image() {
    return this._image;
}

public void set_image(byte[] state_image) {
    this._image = state_image;
}

public String get_state_name() {
    return this._state_name;
}

public void set_state_name(String set_state_name) {
    this._state_name = set_state_name;
}

Its really been frustrating me ever since. Tried a lot of things online, just couldnt find the right way...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
drachomi
  • 17
  • 1
  • 4

1 Answers1

0

I believe that your issue is that you are specifying an offset/index value of 1 when retrieving the BLOB i.e. you have cursor.getBlob(1), this will retrieve the byte array from the STATE_NAME column not the STATE_IMAGE column.

The parameter for the Cursor's get????() methods is the offset/index to the column in the Cursor. As STATE_IMAGE is the third column in the Cursor it's offset will be 2 not 1. As such, the following would work:-

cursor.getBlob(2)

However, as you have found, using offsets can very easily result in errors. You will likely have fewer errors and more easily maintained code if you use the column name via the Cursors getColumnIndex method. Using this the above would be along the lines of :-

cursor.getBlob(csr.getColumnIndex(STATE_IMAGE)).

I'd suggest changing:-

    ImageClass imageClass = new ImageClass(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getBlob(1));

to:-

    ImageClass imageClass = new ImageClass(Integer.parseInt(cursor.getString(cursor.getColumnIndex(STATE_ID)),
            cursor.getString(cursor.getColumnIndex(STATE_NAME)), cursor.getBlob(cursor.getColumnIndex(STATE_IMAGE)));
MikeT
  • 51,415
  • 16
  • 49
  • 68