You could download your image using code like this:
private Image getImage(String url) {
ContentConnection c = null;
DataInputStream dis = null;
try {
try {
c = (ContentConnection) Connector.open(url);
int len = (int) c.getLength();
dis = c.openDataInputStream();
if (len > 0) {
byte[] data = new byte[len];
dis.readFully(data);
im = Image.createImage(data, 0, data.length);
}
} catch (IOException ioe) {
// Failed to read the url. Can't do anything about it, just don't
// update the image.
} finally {
// Regardless of whether we are successful, we need to close
// Connections behind us. Basic Housekeeping.
if (dis != null) {
dis.close();
}
if (c != null) {
c.close();
}
}
} catch (IOException ioe) {
// closure of connections may fail, nothing we can do about it.
}
}
The Image
can be displayed in a Form
as an ImageItem
for example:
ImageItem imgItem =
new ImageItem("Default: ", getImage(url),
Item.LAYOUT_CENTER, null,
Item.BUTTON);
By the way, the above code snippet should only be used for a single static map Image - don't be tempted to override Canvas.paint()
and use this to update the map dynamically - the amount of data traffic required is to do this is highly inefficient, and alternative solutions (as described in this question here) are available.