import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class ImageLoaderMIDlet extends MIDlet implements Runnable, CommandListener { private Display display; private Thread imageThread; private Item mItem; private Form mainForm, imageForm; public void startApp() { this.display = Display.getDisplay(this); this.mainForm = new Form("慢慢等吧,别急..."); this.imageForm = new Form("图片"); this.imageForm.addCommand(new Command("离开", Command.EXIT, 0)); this.imageForm.setCommandListener(this); this.display.setCurrent(mainForm); if (mItem == null) { this.imageThread = new Thread(this); this.imageThread.start(); //启动线程,会自动调用Runnable接口的run()方法 } } public void commandAction(Command c, Displayable s) { this.notifyDestroyed(); } public void run() { //必须实现Runnable中的抽象方法run()方法 try { String URL = "http://www.yiyim.com/image/qinglv.jpg"; Image image = loadImage(URL); //获取服务器图片 //四个参数分别表示: //标签;图像,必须是不可变的;布局(0-3);替代图像的文本 mItem = new ImageItem(null, image, 0, null); } catch (IOException ioe) { mItem = new StringItem(null, ioe.toString()); } //追加一个包含图像的item对象到form中 //其效果相当于调用 //append(new ImageItem(null,img,ImageItem.LAYOUT_DEFAULT,null))方法 //如果参数img为null,会抛出NullPointerException //如果参数image是可变图像,会抛出IllegalArgumentException imageForm.append(mItem); display.setCurrent(imageForm); } //获取服务器图片 public Image loadImage(String url) throws IOException { HttpConnection hpc = null; DataInputStream dis = null; // try { //打开一个连接返回一个Connection接口类的对象, //之后转型为HttpConnection接口类的对象 hpc = (HttpConnection) Connector.open(url); int length = (int) hpc.getLength(); //获取数据长度 byte[] data = new byte[length]; //创建一个字节数组对象 //openInputStream()打开并返回一个输入流 //DataInputStream类对象: //数据输入流允许应用程序以与机器无关方式从基础输入流中读取基本 Java 数据类型。 //应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。 dis = new DataInputStream(hpc.openInputStream()); //从输入流(dis)中读取一些字节,并将它们存储到缓冲区数组 data中。 //读取的字节数等于 data 的长度 dis.readFully(data); //用于创建一个用字节数组存储的不可变图像。 //数组中存放的图像必须是系统(J2ME系统环境)支持的图像格式 //比如PNG格式 //该方法主要用于从外部资源中装入图像,比如数据库和网络中 //三个参数表示: //存储的系统支持格式的图像字节数组; //起始数据的位置(范围0-data.length-1); //数据的长度(0-data.length); //原型为: //public static Image createImage(byte[] imageData,int imageOffset,int imageLength) return Image.createImage(data, 0, data.length); } finally { //close()方法用于关闭连接 //当连接被关闭后,除了该方法外,其他任何方法的调用都会 //抛出IOException异常 //关闭一个已经关闭的连接不会产生任何影响 if (hpc != null) { hpc.close(); } if (dis != null) { dis.close(); } } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
====================================
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import java.io.*; public class loadimage extends MIDlet implements Runnable, CommandListener { private Display display; private Form mainForm, imageForm; private Item mItem; private Thread imageThread; public void startApp() { this.display = Display.getDisplay(this); this.mainForm = new Form("图片下载中..."); this.imageForm = new Form("图片"); this.imageForm.addCommand(new Command("离开", Command.EXIT, 0)); this.imageForm.setCommandListener(this); this.display.setCurrent(mainForm); if (this.mItem == null) { this.imageThread = new Thread(this); this.imageThread.start(); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { this.notifyDestroyed(); } public void run() { try { String URL = "http://www.yiyim.com/image/qinglv.jpg"; Image image = loadImage(URL); //获取服务器图片 this.mItem = new ImageItem(null, image, 0, null); } catch (IOException ioe) { System.out.println("出错了:" + ioe.toString()); } imageForm.append(mItem); display.setCurrent(imageForm); } public Image loadImage(String url) throws IOException { InputStream is = Connector.openInputStream(url); byte[] imagearray = null; ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); try { int ch; while ( (ch = is.read()) != -1) { bytestream.write(ch); } imagearray = bytestream.toByteArray(); return Image.createImage(imagearray, 0, imagearray.length); } finally { if (is != null) { is = null; } if (bytestream != null) { bytestream = null; } if (imagearray != null) { imagearray = null; } } } }
=======================================