jennie788
jennie788

Reputation: 456

How to display online images in j2me?

I know how to display local images in J2me. How can I display an online image? The following code (the image URL below is just for demo purposes) does not yield anything.

Image logo = Image.createImage("http://whatever.com/img/whatever.png");

Thanks

Upvotes: 2

Views: 790

Answers (2)

powerMicha
powerMicha

Reputation: 2773

You need to load the image manually via a HttpConnection

Use this method to load the image:

public Image loadImage(String url) throws IOException {
    HttpConnection hpc = null;
    DataInputStream dis = null;
    try {
      hpc = (HttpConnection) Connector.open(url);
      int length = (int) hpc.getLength();
      byte[] data = new byte[length];
      dis = new DataInputStream(hpc.openInputStream());
      dis.readFully(data);
      return Image.createImage(data, 0, data.length);
    } finally {
      if (hpc != null)
        hpc.close();
      if (dis != null)
        dis.close();
    }
}

See also this tutorial

Upvotes: 1

funkybro
funkybro

Reputation: 8671

Your first port of call for questions like this should be the MIDP 2.0 Javadocs.

There you will see that createImage has an overload which accepts an InputStream; this will do what you need.

Alternatively, you can download the entire image into a byte array and use yet another alternative form of createImage.

Upvotes: 1

Related Questions