你可以使用這樣的代碼下載圖像:
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()
並使用它來動態更新地圖 - 所需的數據流量的數量是highly inefficient,以及其他解決方案(如此問題here中所述)。