I want to display images stored in my database,however my program is throwing an exception(CursorIndexOutOfBoundsException: Index -1 requested, with a size of 100).
I’m using this and in this tutorial I have managed to insert the images into the database but retreiving them is the problem.
Trying to find answers from google and StackOverflow,I have also come across the following questions.
Qustion1
Qustion2.However these questions do not help my case.Any help would be grateful.
Below is my code.
db = helper.getWritableDatabase();
//select the data
//while(cursor.moveToFirst());
cursor = db.query(DBHelper.TABLE, new String[] {DBHelper.C_PHOTOS },
null, null, null, null, null);
//get it as a ByteArray
byte[] imageByteArray=cursor.getBlob(1);
//the cursor is not needed anymore
cursor.close();
//convert it back to an image
ByteArrayInputStream imageStream = new ByteArrayInputStream(imageByteArray);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
Here is the clase which inserts the images
URL url = new URL("myurl");
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is,128);
ByteArrayBuffer baf = new ByteArrayBuffer(128);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
ContentValues v = new ContentValues();
v.put(DBHelper.C_PHOTOS,baf.toByteArray());
v.put(DBHelper.C_PHOTOS, photoThumbnailUrl);
db.insert(DBHelper.TABLE, null, v);
Thanks in advance
Try this: