title: How can I convert an Image to byte array in J2ME?
body: <p>My requirement is like this. I need to read a file from the Mobile phone using a file connection, create a thumbnail of that image and post to the server. I am able to read the image using the FileConnection API, and also able to create the thumbnail.</p>
  
  <p>After creating the thumbnail, I am not able find a method to convert back that image to byte[]. Is it possible?</p>
  
  <p>Code for thumbnail conversion:</p>
  
  <pre><code>    private Image createThumbnail(Image image) {
          int sourceWidth = image.getWidth();
          int sourceHeight = image.getHeight();
  
          int thumbWidth = 128;
          int thumbHeight = -1;
  
          if (thumbHeight == -1)
              thumbHeight = thumbWidth * sourceHeight / sourceWidth;
  
          Image thumb = Image.createImage(thumbWidth, thumbHeight);
          thumb.getGraphics();
          Graphics g = thumb.getGraphics();
  
          for (int y = 0; y &lt; thumbHeight; y++) {
              for (int x = 0; x &lt; thumbWidth; x++) {
                  g.setClip(x, y, 1, 1);
                  int dx = x * sourceWidth / thumbWidth;
                  int dy = y * sourceHeight / thumbHeight;
                  g.drawImage(image, x - dx, y - dy);
              }
          }
  
          Image immutableThumb = Image.createImage(thumb);
  
          return thumb;
      }
  </code></pre>