📄 downloadmidlet.java
字号:
//程序名DownloadMIDlet.java,项目DownloadMIDlet
//一个通过HTTP方式从网络下载图象的综合例子,并利用存储系统对图象进行管理
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class DownloadMIDlet extends MIDlet implements CommandListener{
Display display = null;
static final String rsName = "MyPicture";
static final Command downCmd = new Command("下载", Command.SCREEN, 1);
static final Command viewCmd = new Command("查看", Command.SCREEN, 1);
static final Command deleteCmd = new Command("删除", Command.SCREEN, 1);
static final Command deleteAllCmd = new Command("删除全部", Command.SCREEN, 1);
static final Command exitCmd = new Command("退出", Command.SCREEN, 1);
static final Command returnCmd = new Command("返回", Command.SCREEN, 1);
TextField uriField = new TextField("图象地址:", "", 100, TextField.URL);
TextField nameField = new TextField("保存名称:", "", 15, TextField.ANY);
String uri;
String[] picNames;
String picName;
Image listIcon;
Image[] listIcons;
RecordStore rs;
List viewList;
Form downloadForm;
public DownloadMIDlet() {
try {
listIcon = Image.createImage("/listIcon.png");
} catch (Exception e) {
}
display = Display.getDisplay(this);
}
public void startApp() {
getPicNames();
viewList = new List("浏览下载的图片",List.IMPLICIT,picNames,listIcons);
viewList.addCommand(downCmd);
viewList.addCommand(viewCmd);
viewList.addCommand(deleteCmd);
viewList.addCommand(deleteAllCmd);
viewList.addCommand(exitCmd);
viewList.setCommandListener(this);
downloadForm = new Form("下载图片");
downloadForm.append(uriField);
downloadForm.append(nameField);
downloadForm.addCommand(downCmd);
downloadForm.addCommand(returnCmd);
downloadForm.setCommandListener(this);
display.setCurrent(viewList);
}
public void pauseApp() {
}
public void destroyApp( boolean unconditional ) {
}
public void getPicNames() {
try {
rs= RecordStore.openRecordStore(rsName, true);
int numOfPic = rs.getNumRecords()/2;
picNames = new String[numOfPic];
listIcons = new Image[numOfPic];
for (int i = 0;i<numOfPic;i++) {
picNames[i] = new String(rs.getRecord(2*i+1));
listIcons[i] = listIcon;
}
} catch (Exception e) {
}
}
public void viewImage(int n) {
try {
//rs中的数据结构为:图象名1->图象1->图象名2->图象2->...
byte[] imageBytes = rs.getRecord(2*(n+1));
Image image = Image.createImage(imageBytes,0,imageBytes.length);
Alert alert = new Alert(picNames[n],null, image, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,viewList);
} catch( Exception e ){
Alert alert = new Alert(picNames[n],"读取图象时发生错误,请重新下载!",null, AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,viewList);
}
}
public void deletePic(int n) {
Alert alert = new Alert("发生错误","此功能还未完成,请读者实现!",null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,viewList);
}
public void deleteAll() {
while (true) {
try {
rs.closeRecordStore();
} catch(RecordStoreNotOpenException e ){
break;
} catch(RecordStoreException e ){
}
}
try {
RecordStore.deleteRecordStore(rsName);
rs= RecordStore.openRecordStore(rsName, true);
picNames = null;
int nums = viewList.size();
while(nums-->0) {
viewList.delete(nums);
}
} catch( Exception e ){
}
}
public void startDownload() {
uri = uriField.getString();
picName = nameField.getString();
if (uri == null | picName == null | uri.equals("") | picName.equals("")) {
Alert alert = new Alert("发生错误","URL或保存图象名不能为空!",null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert,downloadForm);
} else {
Thread downloadThread = new DownloadThread(this,uri,picName);
downloadThread.start();
}
}
public void commandAction(Command c, Displayable d) {
if(c == exitCmd) {
destroyApp(true);
notifyDestroyed();
} else if (c == returnCmd){
display.setCurrent(viewList);
} else if (c == deleteAllCmd) {
deleteAll();
} else if (c == deleteCmd) {
deletePic(viewList.getSelectedIndex());
} else if (c == downCmd) {
if (d == viewList) {
display.setCurrent(downloadForm);
} else {
startDownload();
}
} else if (c == List.SELECT_COMMAND | c == viewCmd) {
viewImage(viewList.getSelectedIndex());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -