×
Namespaces

Variants
Actions
Revision as of 08:17, 15 December 2011 by hamishwillee (Talk | contribs)

网络图片资源处理

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon Chinese.png
Article Metadata

文章
翻译:
r60600
最后由 hamishwillee 在 15 Dec 2011 编辑

编程中往往不仅需要处理本地的图片资源,还要处理网络中获取的图片数据,一般这样处理:

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);
}


相关链接

158 page views in the last 30 days.
×