title: How to display images from database
body: <p>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 <a href="http://www.helloandroid.com/tutorials/store-imagesfiles-database" rel="nofollow noreferrer">this</a> 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.
  <a href="https://stackoverflow.com/questions/7165227/cursorindexoutofboundsexception-problemindex-4-requested-with-a-size-of-4">Qustion1</a>
  <a href="https://stackoverflow.com/questions/6850295/android-android-database-cursorindexoutofboundsexception-index-2-requested-wit">Qustion2</a>.However these questions do not help my case.Any help would be grateful.</p>
  <p>Below is my code.</p>
  <pre><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);
  </code></pre>
  <p>Here is the clase which inserts the images</p>
  <pre><code>       URL url = new URL(&quot;myurl&quot;);
               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);
  </code></pre>
  <p>Thanks in advance</p>