1

enter image description here This is image of my data base as you can see here image insert successfully in byte format. This structure of database

public void onCreate(SQLiteDatabase db) { 
    final String[] creatStatments = new String[]{"create table "
            + Image_handler
            + "(_id INTEGER PRIMARY KEY AUTOINCREMENT,"+ Image_handeler_column+" BLOB)" 
};

I am using following code to retrieve image from database

          System.out.println(" satrt image rertrive");
        cursor = dh.rawQuery("SELECT _id, image_byte FROM image_database",null);
                int i=0;
        if (cursor.moveToFirst()) {
            do {
                   String str_cursor =cursor.getString(1);
                   System.out.println("string value of cursor"+str_cursor);

               // get  the  data into array,or class variable   
                bb = cursor.getBlob(cursor.getColumnIndex(DatabaseHelper.Image_handeler_column));
                System.out.println("byte array value"+bb);
            i++;  
            } while (cursor.moveToNext());
            Bitmap bmp=BitmapFactory.decodeByteArray(bb,0,bb.length);
            System.out.println("bitmap value"+bmp);

             imageView.setImageBitmap(bmp);

        }

While i am working on above code value of byte array value is showing B@405889a8 for me and bmp ( Bitmap )value is null for me. so why i am getting bmp value as null . I am going to update my question. For inserting image in database from url i am using following code

         static private Bitmap downloadBitmap(String url) throws IOException {
    HttpUriRequest request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(request);

    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();  
    if (statusCode == 200) {
        System.out.println("kjklcmklxc");
      HttpEntity entity = response.getEntity();
      byte[] bytes = EntityUtils.toByteArray(entity);

      Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
          bytes.length);
      bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byt_aary_outpt_strm);
    dh.delete(DatabaseHelper.Image_handler, null, null);
      bitmapdata = byt_aary_outpt_strm.toByteArray();
      System.out.println("bitmap of image converted image");
      for(int i =0 ; i<bitmapdata.length;i++){
          convert_save_byte_str = convert_save_byte_str+bitmapdata[i];
      }
      System.out.println("njdsfnh"+convert_save_byte_str);
      ContentValues userdetailValues = new ContentValues();

        userdetailValues.put("image_byte", convert_save_byte_str);
        System.out.println("between put and insert");
        dh.insert(DatabaseHelper.Image_handler, null, userdetailValues); 
        cursor = dh.rawQuery("SELECT _id, image_byte FROM image_database",null);
        int i=0;
        if (cursor.moveToFirst()) {
            do {


               // get  the  data into array,or class variable   
                bb = cursor.getBlob(cursor.getColumnIndex(DatabaseHelper.Image_handeler_column));
                //System.out.println("productid"+data);
                //intent.putExtra("product_id", data);
                System.out.print("bytengkfgkjgk"+bb[i]);

            i++;
            } while (cursor.moveToNext());
        }

      return bitmap;    
    } else {
      throw new IOException("Download failed, HTTP response code "
          + statusCode + " - " + statusLine.getReasonPhrase());
    }
  }
AlexVogel
  • 10,601
  • 10
  • 61
  • 71
DJhon
  • 1,548
  • 3
  • 22
  • 39
  • `decodeByteArray` returns null when the byte array data is not in a valid bitmap format. Please show what and how you insert into the database. – Jong Jul 11 '13 at 11:10
  • You convert bytes to string incorrectly. Replace your code by correct one: http://stackoverflow.com/questions/5673059/converting-byte-array-to-string-java. Also I think you should use string.getBytes() instead of getBlob – vortexwolf Jul 11 '13 at 11:51
  • @vorrtex Thanks for your response i tried your suggestion but it does not work for me. – DJhon Jul 12 '13 at 04:37

0 Answers0