网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > J2EE/J2ME
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.J2EE程序中的SQL语句自动构造方法.
.搭建OTA下载服务器.
.KVM的执行引擎(下) — 指令集.
.J2EE入门教程之一.
.使用Timer和Canvas制作动画效果.
.利用Filter压缩HTTP响应.
.使用JSR 184技术在3D空间里选中物.
.在J2ME/MIDP中实现图像旋转(一).
.EnterpriseJavaBeansDistilled....
.测试遗留代码.
.利用Filter实现IP过滤.
.创建Mascot Capsule v3烟雾效果.
.RMS从入门到精通之一.
.MIDP中对日期时间的简单转换.
.JSP中tomcat的SQLServer2000数据.
.J2EEFrameworks介绍.
.MIDP中处理文字的换行.
.windy‘s j2ee1.4 tutorial (chi.
.Java中的cookie管理方案(2)-与J2.
.Java远程方法调用(一).

下载和显示PNG图片

发表日期:2007-12-23


下载和显示PNG图片

import Javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;

public class ViewPng extends MIDlet implements CommandListener
{
  private Display display;
  private TextBox tbMain;
  private Form fmViewPng;
  private Command cmExit;
  private Command cmView;
  private Command cmBack;

  public ViewPng()
  {
    display = Display.getDisplay(this);

    // Create the Main textbox with a maximum of 75 characters
    tbMain = new TextBox("Enter url", "/code/UploadFiles_1700/200605/20060519104731935.png", 75, 0);

    // Create commands and add to textbox
    cmExit = new Command("Exit", Command.EXIT, 1);
    cmView = new Command("View", Command.SCREEN, 2);    
    tbMain.addCommand(cmExit);
    tbMain.addCommand(cmView );    

    // Set up a listener for textbox
    tbMain.setCommandListener(this);

    // Create the form that will hold the png image
    fmViewPng = new Form("");

    // Create commands and add to form
    cmBack = new Command("Back", Command.BACK, 1);
    fmViewPng.addCommand(cmBack);

    // Set up a listener for form
    fmViewPng.setCommandListener(this);
  }

  public void startApp()
  {
    display.setCurrent(tbMain);
  }

  public void pauseApp()
  { }

  public void destroyApp(boolean unconditional)
  { }

  /*--------------------------------------------------
  * Process events
  *-------------------------------------------------*/
  public void commandAction(Command c, Displayable s)
  {
    // If the Command button pressed was "Exit"
    if (c == cmExit)
    {
      destroyApp(false);
      notifyDestroyed();
    }
    else if (c == cmView)
    {
      // Download image and place on the form
      try
      {
        Image im;
        if ((im = getImage(tbMain.getString())) != null)
        {
          ImageItem ii = new ImageItem(null, im, ImageItem.LAYOUT_DEFAULT, null);
          // If there is already an image, set (replace) it
          if (fmViewPng.size() != 0)
            fmViewPng.set(0, ii);
          else  // Append the image to the empty form
            fmViewPng.append(ii);
        }
        else
          fmViewPng.append("UnsUCcessful download.");
        // Display the form with the image
        display.setCurrent(fmViewPng);
      }
      catch (Exception e)
      { 
        System.err.println("Msg: " + e.toString());
      }
    } 
    else if (c == cmBack) {
      display.setCurrent(tbMain);
    }
  }

  /*--------------------------------------------------
  * Open connection and download png into a byte array.
  *-------------------------------------------------*/
  private Image getImage(String url) throws IOException
  {
    ContentConnection connection = (ContentConnection) Connector.open(url);
    // * There is a bug in MIDP 1.0.3 in which read() sometimes returns
    //   an invalid length. To work around this, I have changed the 
    //   stream to DataInputStream and called readFully() instead of read()
//    InputStream iStrm = connection.openInputStream();
    DataInputStream iStrm = connection.openDataInputStream();    
    ByteArrayOutputStream bStrm = null;    
    Image im = null;

    try
    {
      // ContentConnection includes a length method
      byte imageData[];
      int length = (int) connection.getLength();
      if (length != -1)
      {
        imageData = new byte[length];

        // Read the png into an array
//        iStrm.read(imageData);        
        iStrm.readFully(imageData);
      }
      else  // Length not available...
      {       
        bStrm = new ByteArrayOutputStream();
        int ch;
        while ((ch = iStrm.read()) != -1)
          bStrm.write(ch);
        imageData = bStrm.toByteArray();
        bStrm.close();                
      }

      // Create the image from the byte array
      im = Image.createImage(imageData, 0, imageData.length);        
    }
    finally
    {
      // Clean up
      if (iStrm != null)
        iStrm.close();
      if (connection != null)
        connection.close();
      if (bStrm != null)
        bStrm.close();                              
    }
    return (im == null null : im);
  }
}

(出处:)


上一篇:基于MIDP实现ResourceBundle类 人气:637
下一篇:从远程装载PNG图片实例 人气:705
浏览全部J2EE/J2ME的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐