课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——多线程获取网络图片


  共有15754人关注过本帖树形打印复制链接

主题:程序代码——多线程获取网络图片

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——多线程获取网络图片  发帖心情 Post By:2008/10/28 20:19:38 [只看该作者]

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.ContentConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {
        Display display;
        Form form = new Form("练习");
        TextField tf;
        Command ok = new Command("开始", Command.OK, 1);
        Command cancel = new Command("取消", Command.OK, 1);
        Command back = new Command("再来", Command.BACK, 1);
        Alert info = new Alert("消息", "正在下载...", null, AlertType.INFO);
        Form imageForm = new Form("图片");
        boolean cacncelFlag = false;

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                tf = new TextField("URL:", "", 50, TextField.ANY);
                form.append(tf);
                form.addCommand(ok);
                form.setCommandListener(this);
                display.setCurrent(form);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        public void commandAction(Command com, Displayable disp) {
                if (com == ok) {
                        cacncelFlag = false;
                        Downloader downloader = new Downloader(tf.getString(), this);
                        Thread thread = new Thread(downloader);
                        thread.start();
                        info.setTimeout(10000);
                        info.addCommand(cancel);
                        info.setCommandListener(this);
                        display.setCurrent(info);
                } else if (com == cancel) {
                        cacncelFlag = true;
                        display.setCurrent(form);
                } else if (com == back) {
                        display.setCurrent(form);
                }
        }

        public void showImage(Image image) {
                ImageItem it = new ImageItem(null, image, ImageItem.LAYOUT_DEFAULT,
                                null);
                if (imageForm.size() == 0)
                        imageForm.append(it);
                else
                        imageForm.set(0, it);
                imageForm.addCommand(back);
                imageForm.setCommandListener(this);
                display.setCurrent(imageForm);
        }

        public boolean isCacncelFlag() {
                return cacncelFlag;
        }

}

class Downloader implements Runnable {
        String url;
        Exec midlet;

        public Downloader(String url, Exec midlet) {
                this.url = url;
                this.midlet = midlet;
        }

        public void run() {
                ContentConnection con = null;
                InputStream is = null;
                Image image = null;
                try {
                        con = (ContentConnection) Connector.open(url);
                        is = con.openInputStream();
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] dataArray = null;
                        int ch = 0;
                        while ((ch = is.read()) != -1 && !midlet.isCacncelFlag()) {
                                baos.write(ch);
                        }
                        if (!midlet.isCacncelFlag()) {
                                dataArray = baos.toByteArray();
                                image = Image.createImage(dataArray, 0, dataArray.length);
                                midlet.showImage(image);
                        }
                } catch (Exception e) {
                        System.out.println(e.getMessage());

                } finally {
                        if (is != null)
                                try {
                                        is.close();
                                } catch (IOException e) {
                                        System.out.println(e.getMessage());
                                }
                        if (con != null)
                                try {
                                        con.close();
                                } catch (IOException e) {
                                        System.out.println(e.getMessage());
                                }
                }
        }

}

[此贴子已经被作者于2010-12-12 18:41:33编辑过]

 回到顶部