您可以使用如下代码下载图像:
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.
}
}
Image
可以显示为Form
例如ImageItem
:
ImageItem imgItem =
new ImageItem("Default: ", getImage(url),
Item.LAYOUT_CENTER, null,
Item.BUTTON);
顺便说一句,上面的代码片段应该只用于单个静态地图图像 - 不要试图覆盖Canvas.paint()
并使用它来动态更新地图 - 这样做所需的数据流量非常低效,并且可以使用替代解决方案(如这里问题中所述)。