×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    26

    Displaying image

    I tried to "continuosly" grab and display a jpg image from http address. But my code below does not work

    Anyone know how to fix this ?

    Thanks

    package jpgmidlet;

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import java.util.Vector;
    import java.io.IOException;
    import java.io.DataInputStream;
    import javax.microedition.io.HttpConnection;
    import javax.microedition.io.ContentConnection;
    import javax.microedition.io.Connector;

    public class JpgMidlet extends MIDlet implements Runnable
    {
    private Display display;
    private Thread thread;
    private Frame frame;

    public JpgMidlet() {
    display = Display.getDisplay(this);

    frame = new Frame();
    }

    protected void startApp() {
    run();
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional) {
    }

    private Image createImage(String name) throws IOException {

    // Load from a ContentConnection
    HttpConnection c = null;
    DataInputStream is = null;

    try {
    c = (HttpConnection)Connector.open(name);
    int status = c.getResponseCode();

    if (status != 200) {
    throw new IOException("HTTP Response Code = " + status);
    }

    int len = (int)c.getLength();
    String type = c.getType();

    if (len > 0) {
    is = c.openDataInputStream();
    byte[] data = new byte[len];
    is.readFully(data);
    return Image.createImage(data, 0, len);
    } else {
    throw new IOException("Content length is missing");
    }

    } finally {
    if (is != null)
    is.close();

    if (c != null)
    c.close();
    }

    }


    public void run() {
    Image theimage = null;

    String name = "http://127.0.0.1:11001/image.jpg";
    try {
    theimage = Image.createImage(name);
    } catch (IOException err) {
    }

    frame.setImages(theimage);
    display.setCurrent(frame);

    }
    }

    class Frame extends Canvas {

    private Image image;

    Frame() {
    image = Image.createImage(getWidth(), getHeight());
    }

    void setImages(Image theimage) {
    Image image = theimage;
    }

    protected void paint(Graphics g) {
    g.drawImage(image, 0, 0, Graphics.LEFT|Graphics.TOP);
    }

    }

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    You're passing the URL to Image.createImage(), which won't accept a URL as a parameter. You don't call your own createImage() method anywhere.

    Graham.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    1

    Displaying image

    Then, how do u solve this ? Cud u help ?

    Regards,
    Shilpa.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    The trick is, not to name your methods so that they have the same name as a different method in a different class that works a different way.

    Change the name of the createImage() method in this code to fetchImage(), and make sure the method actually gets called from somewhere.

    Graham.

Posting Permissions

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