一个下载图片的例子,为以后铺路。
2007-04-12 19:16
281 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.microedition.io.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.microedition.lcdui.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.microedition.midlet.*;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class HelloMIDlet extends MIDlet implements CommandListener, Runnable ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Display mDisplay;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Form mForm;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public HelloMIDlet() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mForm = new Form("Connecting...");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mForm.addCommand(new Command("Exit", Command.EXIT, 0));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mForm.setCommandListener(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void startApp() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (mDisplay == null) mDisplay = Display.getDisplay(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mDisplay.setCurrent(mForm);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// Do network loading in a separate thread.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Thread t = new Thread(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
t.start();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void pauseApp() ...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void destroyApp(boolean unconditional) ...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void commandAction(Command c, Displayable s) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (c.getCommandType() == Command.EXIT)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
notifyDestroyed();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void run() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpConnection hc = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataInputStream in = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String url = "http://images.163.com/homepage/logo.gif";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
hc = (HttpConnection)Connector.open(url);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int length = (int)hc.getLength();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] data = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (length != -1) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
data = new byte[length];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in = new DataInputStream(hc.openInputStream());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in.readFully(data);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
else ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// If content length is not given, read in chunks.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int chunkSize = 512;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int index = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int readLength = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
in = new DataInputStream(hc.openInputStream());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
data = new byte[chunkSize];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
do ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
if (data.length < index + chunkSize) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
byte[] newData = new byte[index + chunkSize];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.arraycopy(data, 0, newData, 0, data.length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
data = newData;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
readLength = in.read(data, index, chunkSize);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
index += readLength;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
} while (readLength == chunkSize);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
length = index;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Image image = Image.createImage(data, 0, length);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ImageItem imageItem = new ImageItem(null, image, 0, null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mForm.append(imageItem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mForm.setTitle("Done");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
catch (IOException ioe) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
StringItem stringItem = new StringItem(null, ioe.toString());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mForm.append(stringItem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
mForm.setTitle("Done.");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
finally ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
try ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (in != null) in.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (hc != null) hc.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
catch (IOException ioe) ...{}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- 一个链式存储的例子(图片)
- 一个httpwebrequest异步下载的例子
- Flash8帮助中一个加载外部图片的例子(有进度条)
- opencv实现图片无边框的一个例子
- nodeJs学习过程之一个图片上传显示的例子
- 昨天晚上写了个 Python 程序,下载了一个福利网站 5000 来张妹子图片
- IOS第二十九天——HTTP下载一个图片
- 一个简单的WebSerivce传递图片的例子(转贴)
- 这是一个转载,关于python的下载 图片视频
- 一个简单的JavaScript图片显示与隐藏例子
- 写一个判断单点触摸某图片区域的例子。
- 一个图片切割的例子
- PHP--封装一个下载图片的方法
- 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-) Python3 图片 爬取 下载 到 本地
- 一个Flex鼠标拖拽图片的简单例子(附源码)
- Afinal图片下载例子
- 一个Hibernate 的例子,还有jsp上传和下载较为复杂的例子
- 通过代码从网上下载一个图片及图片的复制
- 老罗Android(19)AsyncTask下载图片例子
- 封装的一个用来下载图片的类