编程中往往不仅需要处理本地的图片资源,还要处理网络中获取的图片数据,一般这样处理:
private String url;
private byte[] data;
private int datalength;
private DataInputStream dis;
private HttpConnection hc;
url = "http://xx/xx.gif" /* 或者"http://xx/xx.jpg" */
try {
hc = (HttpConnection)Connector.open(url);
datalength = (int)hc.getLength();
dis = hc.openDataInputStream();
data = new byte[datalength];
dis.readFully(data);
if (datalength > 0)
image = Image.createImage(data, 0, datalength);
}catch (java.io.IOException x){}
只要Image对象创建成功,就可以象显示普通Image对象那样显示出来。
例如在Canvas中显示:
protected void paint(Graphics g)
{
g.drawImage(image,0, 0, Graphics.LEFT|Graphics.TOP);
}