Namespaces
Variants
Actions

网络图片资源处理

Jump to: navigation, search
WP Metro Icon Chinese.png
文章信息

文章
翻译:
r60600
最后由 r60600 在 19 Apr 2013 编辑

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

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


相关链接

This page was last modified on 19 April 2013, at 07:02.
245 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved