Programming Java 2 Micro Edition for Symbian OS 2004 (779882), страница 31
Текст из файла (страница 31)
If the RecordStore to be opened hasAUTHMODE_PRIVATE access then the vendorName and suiteNamearguments must match the respective attributes of the MIDlet suite aslisted in the application descriptor or manifest file.We shall illustrate sharing record stores between MIDlet suites witha simple example.
The RMSWriter MIDlet creates an Image froma resource file and displays it. It then saves the image data into arecordstore. We shall then show how the image data can be retrievedfrom the recordstore by a different MIDlet suite using the RMSReaderMIDlet example.The source code for the RMSWriter class is listed below:importimportimportimportpublicjavax.microedition.midlet.*;javax.microedition.lcdui.*;javax.microedition.rms.*;java.io.*;class RMSWriter extends MIDlet implementsCommandListener {private static final String IMAGE_NAME = "/image.png";private static final int IMAGE_SIZE = 11222;private Display display;private Form form;private Command exitCommand;private Command startCommand;private Command saveCommand;private ImageCanvas imageCanvas;private byte[] data;public RMSWriter(){data = loadImage(IMAGE_NAME, IMAGE_SIZE);display = Display.getDisplay(this);Image image = Image.createImage(data, 0, data.length);MIDP 2.0imageCanvas = new ImageCanvas(image);form = new Form("RMS Writing Demo");exitCommand = new Command("Exit", Command.EXIT, 2);startCommand = new Command("Start", Command.SCREEN, 1);saveCommand = new Command("Save image", Command.SCREEN, 1);form.addCommand(exitCommand);form.addCommand(startCommand);form.setCommandListener(this);}public void startApp() {display.setCurrent(form);}public byte[] loadImage(String imageName, int imageSize) {byte[] data = new byte[imageSize];try {Class c = this.getClass() ;InputStream is = c.getResourceAsStream(imageName);DataInputStream dis = new DataInputStream(is);dis.readFully(data);is.close();}catch(IOException ioe) {ioe.printStackTrace();}return data;}public int saveToStore(byte[] data) {int recordID = 0;try {RecordStore store =RecordStore.openRecordStore("ImageStore",true, RecordStore.AUTHMODE_ANY, true);recordID = store.addRecord(data, 0, data.length);store.closeRecordStore();}catch(RecordStoreException rse) {rse.printStackTrace();}return recordID;}public void commandAction(Command c, Displayable d) {if (c == exitCommand) {notifyDestroyed();}else if (c == startCommand) {display.setCurrent(imageCanvas);imageCanvas.addCommand(saveCommand);imageCanvas.setCommandListener(this);}else if (c == saveCommand) {int recordID = saveToStore(data);imageCanvas.removeCommand(saveCommand);display.setCurrent(form);form.append(new StringItem(null, "Image saved as record "+ recordID));}}151152MIDP 2.0 AND THE JTWIpublic void pauseApp() {}public void destroyApp(boolean unconditional) {}}The image is loaded from the resources in the loadImage() method.This opens an input stream using the getResourceAsStream()method and stores the data in a byte array.
This byte array is usedto create the Image using one of the overloaded static createImage()methods. The image is then displayed in a Canvas. When the user selectsthe ”Save image” option, the image data is saved into the record store bythe saveToStore() method listed below:public int saveToStore(byte[] data) {int recordID = 0;try {RecordStore store = RecordStore.openRecordStore("ImageStore",true, RecordStore.AUTHMODE_ANY, true);recordID = store.addRecord(data, 0, data.length);store.closeRecordStore();}catch(RecordStoreException rse) {rse.printStackTrace();}return recordID;}A RecordStore is opened with AUTHMODE_ANY permission.
Thedata is saved to the RecordStore using the addRecord() method,which creates a new record and returns an integer identifying that recordwithin the recordstore. The RecordStore is then closed using thecloseRecordStore() method.Figure 3.20 shows the RMSWriter MIDlet running on a Nokia 6600.Figure 3.20 The RMSWriter MIDlet running on the Nokia 6600.MIDP 2.0153Now that we have created a RecordStore, we shall show how thiscan be accessed from a different MIDlet suite using the RMSReaderMIDlet.
The source code for the RMSReader class is listed below.importimportimportimportpublicjavax.microedition.midlet.*;javax.microedition.lcdui.*;javax.microedition.rms.*;java.io.*;class RMSReader extends MIDlet implements CommandListener {privateprivateprivateprivateprivateprivateprivateprivateDisplay display;Form form;Command exitCommand;Command startCommand;Command backCommand;TextField storeName;TextField recordNo;ImageCanvas imageCanvas;public RMSReader() {display = Display.getDisplay(this);form = new Form("RMS Reading Demo");exitCommand = new Command("Exit", Command.EXIT, 2);startCommand = new Command("Load image", Command.SCREEN, 1);backCommand = new Command("Back", Command.BACK, 1);storeName = new TextField("Enter recordstore name","ImageStore", 100, TextField.ANY);recordNo = new TextField("Enter record ID", "1", 10,TextField.NUMERIC);form.addCommand(exitCommand);form.addCommand(startCommand);form.append(storeName);form.append(recordNo);form.setCommandListener(this);}public void startApp() {display.setCurrent(form);}public byte[] loadFromStore(String storeName, int recordID) {byte[] data = null;try {RecordStore store =RecordStore.openRecordStore(storeName, false);data = store.getRecord(recordID);store.closeRecordStore();}catch(RecordStoreException rse) {rse.printStackTrace();}return data;}public Image constructImage() {Integer tempRecordID = Integer.valueOf(recordNo.getString());int recordID = tempRecordID.intValue();byte[] data = loadFromStore(storeName.getString(), recordID);Image image = null;if (data != null) {154MIDP 2.0 AND THE JTWIimage = Image.createImage(data, 0, data.length);}return image;}public void commandAction(Command c, Displayable d) {if (c == exitCommand) {notifyDestroyed();}else if (c == startCommand) {Image image = constructImage();if (image != null) {imageCanvas = new ImageCanvas(image);display.setCurrent(imageCanvas);imageCanvas.addCommand(backCommand);imageCanvas.setCommandListener(this);}else {Alert alert = new Alert("Application error","Unable to create image.", null,AlertType.ERROR);display.setCurrent(alert);}}else if (c == backCommand) {imageCanvas.removeCommand(backCommand);display.setCurrent(form);}}public void pauseApp() {}public void destroyApp(boolean unconditional) {}}The image data is loaded from the RecordStore by the loadFromStore() method:public byte[] loadFromStore(String storeName, int recordID) {byte[] data = null;try {RecordStore store = RecordStore.openRecordStore(storeName,false);data = store.getRecord(recordID);store.closeRecordStore();}catch(RecordStoreException rse) {rse.printStackTrace();}return data;}The RecordStore is opened and then the record is retrieved in theform of a byte array by the getRecord() method, using the recordidentifier.
The RecordStore is then closed. The image data in the formof the byte array can then be used to create an Image and display it asbefore. Figure 3.21 shows the RMSReader MIDlet.The full source code and JAR and JAD files for the RMSWriter andRMSReader MIDlets can be downloaded from the Symbian website atwww.symbian.com/books.OPTIONAL J2ME APIS IN THE JTWI155Figure 3.21 RMSReader MIDlet running on a Nokia 6600.The underlying operating system is responsible for maintaining theintegrity of record stores throughout the normal use of the platform, however, the RMS store provides no support for locking records accessedby multiple threads; this is the responsibility of the developer. However,the javax.microedition.rms package does provide a RecordListener interface to assist the developer in synchronizing access to records.Since record IDs are lost when the MIDlet that created the records shutsdown, the developer has to provide some process for searching throughrecords and selecting the required one.
The javax.microedition.rmspackage provides the RecordComparator, RecordEnumerator andRecordFilter interfaces to assist this process.For more information on using the RMS APIs, consult the MIDPdocumentation. Further examples of using the RMS are presented in thecase studies discussed in Chapter 5.3.4 Optional J2ME APIs in the JTWIIn this section we consider the optional J2ME APIs that are indicated for aJTWI-compliant device.