📄 downloadfromurlform.java
字号:
/*
* Filename: DownloadFromURLForm.java
* - contains definition of the class DownloadFromURLForm, which is a Form used as user interface in downloading
* and saving images/descriptions into persistent storage on phone, it is also the corresponding CommandListener
* handling image download using HTTP protocol and images/descriptions save using methods defined in class
* RMSOperations.
*
*
* Application Name: Image Album Demo
* Version: 2.0
* Release Date: 27th September, 2002
* Author: Edmund Wong, Application Engineer, Metrowerks Hong Kong
*
*
* (c) Copyright. 2002. Metrowerks Corp. ALL RIGHTS RESERVED.
* This code is provided "AS IS" without warranty of any kind and subject to Metrowerks Sample Application
* End User License Agreement. NO EXTERNAL DISTRIBUTION OR COMMERCIALIZATION OF THE SOFTWARE IS PERMITTED
* EXCEPT BY WRITTEN AGREEMENT OF METROWERKS."
*/
import javax.microedition.lcdui.*;
import java.io.*;
public class DownloadFromURLForm extends Form implements CommandListener{
/**
* Create Items and Command for DownloadFromURLForm
*/
//for DownloadFromURLForm
Command downloadCommand = new Command("Get Image",Command.SCREEN,0);
TextField URLtextField = new TextField("Download from URL:", "http://localhost/samples/1.png"/*"http://www2.comp.polyu.edu.hk/~c1415307/1.png"*/, 60, TextField.URL);
//for downloadImageCanvas which display downloaded image
Command backCommand = new Command("Back",Command.BACK,0); // going back to DownloadFromURLForm
Command saveImageCommand = new Command("Save Image",Command.SCREEN,0); //for saving downloaded image to phone
//for saveOKForm which display 'Save image OK' information
Command saveEditedDescriptionsCommand = new Command("Save Description",Command.SCREEN,0); // for saving edited description
Command downloadAgainCommand = new Command("Download again", Command.BACK, 0); //for going back to DownloadFromURLForm
TextField descriptionsEntryField = null; // for user to enter of new description on Save Image OK screen
byte [] downloadedImageData; // data buffer for storing downloaded data
int addedDescriptionDBRecordID; // for storing the recordID of the new record created for storing description of newly
// saved image
Image img = null; // reference for image created
ImageAlbum imageAlbumReference; // reference for main object ImageAlbum
/**
* Constructor of DownloadFromURLForm
*/
public DownloadFromURLForm(ImageAlbum imageAlbumReference) {
super("Please input URL:");
this.imageAlbumReference = imageAlbumReference;
/**
* Add items for the Download URL Input Form
*/
append(URLtextField);
/**
* Add command and set CommandListener for the DownloadFromURLForm
*/
addCommand(downloadCommand);
addCommand(ImageAlbum.mainMenuCommand);
setCommandListener(this);
}
/**
* Implementation for CommandListener
*/
public void commandAction(Command c, Displayable s) {
/**
* download and display image from specified URL when download Command is chosen from downloadFromURLForm
*/
if (c==downloadCommand) {
try{
// download byte data and store in byte array
downloadedImageData = HttpDownloader.httpDownload(URLtextField.getString());
}
catch (IOException e){
System.out.println("IO Exception" + e.toString());
}
// create image from byte array
img = Image.createImage(downloadedImageData, 0, downloadedImageData.length);
// create downloadImageCanvas for displaying downloaded image, with description field showing URL
PhotoCanvas downloadImageCanvas = new PhotoCanvas(img, URLtextField.getString(), downloadedImageData.length);
downloadImageCanvas.downloadImageCanvasFlag = true; // set flag to stop drawing string 'image x of y'
downloadImageCanvas.addCommand(saveImageCommand);
downloadImageCanvas.addCommand(backCommand);
downloadImageCanvas.addCommand(ImageAlbum.mainMenuCommand);
downloadImageCanvas.setCommandListener(this);
ImageAlbum.myDisplay.setCurrent(downloadImageCanvas);
downloadImageCanvas = null;
}
/**
* add image data to RecordStore when saveImageCommand is chosen from downloadImageCanvas
*/
if (c==saveImageCommand) {
// open the RecordStore "ImageDB"
imageAlbumReference.rmsOperations.openRecordStore("ImageDB", false);
// add downloaded image data to the opened ImageDB RecordStore
imageAlbumReference.rmsOperations.addRecord(downloadedImageData);
//update variable storing total no. of images
ImageAlbum.totalImageNumber++;
//update variable storing no. of images in RecordStore
ImageAlbum.imageNumberInRecordStore++;
System.out.println(" total image no.: "+ImageAlbum.totalImageNumber);
// store the image into the vector imageVector in main ImageAlbum object for immediate showing use
imageAlbumReference.imageVector.addElement(img);
// create Form for showing Image Save OK notification and allow user to enter user's description
Form saveOKForm = new Form("Image Save OK!");
// text field for entering user's description
descriptionsEntryField = new TextField("To change default description -\nEnter your own description in text box below and choose 'Save Description' Command. (max. 100 characters):", URLtextField.getString(), 100, TextField.ANY);
saveOKForm.append(new StringItem("Image saved as Image number " + ImageAlbum.totalImageNumber +"!\nURL has been saved as default description for this image!", null));
saveOKForm.append(descriptionsEntryField);
saveOKForm.addCommand(saveEditedDescriptionsCommand);
saveOKForm.addCommand(ImageAlbum.mainMenuCommand);
saveOKForm.addCommand(downloadAgainCommand);
saveOKForm.setCommandListener(this);
ImageAlbum.myDisplay.setCurrent(saveOKForm);
saveOKForm = null;
// close RecordStore "ImageDB"
imageAlbumReference.rmsOperations.closeRecordStore();
img = null;
/**
* Saving default description of this image to Description database
*/
byte [] byteArray = null;
// open the RecordStore "DescriptionsDB"
imageAlbumReference.rmsOperations.openRecordStore("DescriptionsDB", false);
// convert default description string into byte array
try {
byteArray = StringByteOperations.stringToByteConvertion(URLtextField.getString());
}
catch(Exception e) {
System.out.println("Exception: "+e.toString());
}
// write the byte array of the string into RecordStore "DescriptionsDB"
addedDescriptionDBRecordID = imageAlbumReference.rmsOperations.addRecord(byteArray);
System.out.println("added description record #" + addedDescriptionDBRecordID);
// store the description into the Vector descriptionVector in main ImageAlbum for immediate showing use
imageAlbumReference.descriptionsVector.addElement(descriptionsEntryField.getString());
// close RecordStore "DescriptionsDB"
imageAlbumReference.rmsOperations.closeRecordStore();
byteArray = null;
}
/**
* jump back to DownloadFromURLForm when backCommand is chosen from downloadImageCanvas
*/
if (c==backCommand) {
img = null;
ImageAlbum.myDisplay.setCurrent(this);
}
/**
* jumping back to Main Menu when mainMenuCommand is chosen from DownloadFromURLForm,
* downloadImageCanvas or saveOKForm
*/
if (c==ImageAlbum.mainMenuCommand) {
img = null;
ImageAlbum.myDisplay.setCurrent(imageAlbumReference.selectList);
}
/**
* jump back to DownloadFromURLForm when downloadAgainCommand is chosen from saveOKForm
*/
if (c == downloadAgainCommand) {
ImageAlbum.myDisplay.setCurrent(this);
}
/**
* store user's edited description when saveEditedDescriptionsCommand is chosen from saveOKForm
*/
if (c==saveEditedDescriptionsCommand) {
byte [] byteArray = null;
// open the RecordStore "DescriptionsDB"
imageAlbumReference.rmsOperations.openRecordStore("DescriptionsDB", false);
// convert edited description string into byte array
try{
byteArray = StringByteOperations.stringToByteConvertion(descriptionsEntryField.getString());
}
catch(Exception e) {
System.out.println("Exception: " + e.toString());
}
imageAlbumReference.rmsOperations.setRecord(addedDescriptionDBRecordID, byteArray, "Edited Description Save OK!", this);
System.out.println("edited description record #" + addedDescriptionDBRecordID);
System.out.println("total description record #" + ImageAlbum.totalImageNumber);
// store the description into the Vector descriptionVector for immediate showing use
imageAlbumReference.descriptionsVector.setElementAt(descriptionsEntryField.getString(), (imageAlbumReference.descriptionsVector.size() - 1));
// close RecordStore "DescriptionsDB"
imageAlbumReference.rmsOperations.closeRecordStore();
byteArray = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -