本源代码包括J2ME开发精解 该书的全部章节的示例代码
源代码在线查看: powermodel.java
package com.j2medev.ch8.mmapi;
import java.io.*;
import javax.microedition.lcdui.AlertType;
import javax.microedition.rms.*;
public class PowerModel {
private RecordStore rs = null;
private PowerCamera pc = null;
public PowerModel(PowerCamera pc) {
this.pc = pc;
try{
rs = RecordStore.openRecordStore("camera",true);
}catch(RecordStoreException ex){
ex.printStackTrace();
}
}
public void savePicture(Picture pic){
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
pic.serialize(dos);
dos.close();
byte[] data = baos.toByteArray();
rs.addRecord(data, 0, data.length);
}catch(IOException ex){
pc.showInfo(ex.toString(), AlertType.ERROR);
}catch(RecordStoreException ex){
pc.showInfo(ex.toString(), AlertType.ERROR);
}
}
public Picture[] getAllPicture(){
try{
int num = rs.getNumRecords();
if(num == 0){
return null;
}else{
RecordEnumeration re = rs.enumerateRecords(null,null,false);
int length = re.numRecords();
Picture[] pic = new Picture[length];
int i = 0;
while(re.hasNextElement()){
byte[] data = re.nextRecord();
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
pic[i] = Picture.deserialize(dis);
i++;
dis.close();
}
return pic;
}
}catch(IOException ex){
pc.showInfo(ex.toString(), AlertType.ERROR);
} catch(RecordStoreException ex){
pc.showInfo(ex.toString(), AlertType.ERROR);
}
return null;
}
public static String isVideoCapture(){
return System.getProperty("supports.video.capture");
}
public void release(){
if(rs != null){
try{
rs.closeRecordStore();
}catch(RecordStoreException ex){
//不做处理
}
rs = null;
}
}
}