×

Discussion Board

Results 1 to 7 of 7

Thread: image via GPRS

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    21

    image via GPRS

    hi guys!

    id just like to ask who knows how to send an image from a server servlet to a midlet?

    thanks guys!

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    You don't really need a servlet for this. Just put the image on teh web somewhere, and go to that address from your MIDlet, read all the data and use Image.createImage(byte[] imageData, int imageOffset, int imageLength) with the data you downloaded.

    shmoove

  3. #3
    Registered User
    Join Date
    May 2003
    Location
    Helsinki, FInland
    Posts
    6
    Can that be done with midp 1.0 or does it require 2.0?

  4. #4
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    MIDP 1.0 will do. The data comes into the stream as an array of bytes and you use that array in the createImage() method:
    Code:
      /*-----------------------------------------------------------------
      * Open an http connection and download a png file
      * into a byte array.
      *-----------------------------------------------------------------*/
      private Image getImage(String url) throws IOException
      {
        ContentConnection connection = (ContentConnection) Connector.open(url);
        DataInputStream iStrm = connection.openDataInputStream();
    
        Image im = null;
    
        try
        {
          // ContentConnection includes a length method
          byte imageData[];
          length = (int) connection.getLength();
          if (length != -1)
          {
            imageData = new byte[length];
    
            // Read the png into an array
            iStrm.readFully(imageData);
          }
          else  // Length not available...
          {
            ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
    
            int ch;
            while ((ch = iStrm.read()) != -1)
              bStrm.write(ch);
    
            imageData = bStrm.toByteArray();
            bStrm.close();
          }
    
          // Create the image from the byte array
          im = Image.createImage(imageData, 0, imageData.length);
        }
        finally
        {
          // Clean up
          if (iStrm != null)
            iStrm.close();
          if (connection != null)
            connection.close();
        }
        return (im == null ? null : im);
      }
    shmoove

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    21

    THANKS!

    i havent tried it yet, but thanks..ill try it now..thanks for your time man!

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    thanks much for the nice code example, shmoove. I somehow have missed readFully() in the past. d'uh.

    I don't want to pick code nits, but I was curious about your return statement. In this case, that's just the same as "return im;" right?

    jim

  7. #7
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Looks like you're write.
    And I don't want to take credit for something I didn't do. This code isn't a shmoove original, I downloaded it from the web (I don't remember exactly where).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×