J2ME – How to write – read image in – from RMS
The example below saves image file into RMS and shows it on mobile.
Data like image, image name, width, height can be stored in RMS.
The size of the image file do matters if phone capacity is small.
Sample.java
package freesrc.com;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.*;
/**
* @author freesrc.com
*/
public class Sample extends MIDlet {
private Display display = null;
private static final String recordName = "recordName";
public Sample(){
display = Display.getDisplay(this);
}
public void startApp() {
Image a = Image.createImage(100,100);
int[] rgbData = new int[100 * 100];
a.getRGB(rgbData, 0, 100, 0, 0, 100, 100);
Event ev = new Event();
ev.setImage(rgbData);
ev.setName("Test");
ev.setImgWid(100);
ev.setImgHei(100);
RecordStoreWriter.writeEvent(ev, recordName);
Event readEvent = RecordStoreWriter.readEvent(recordName);
MainCanvas c = new MainCanvas();
c.prepareCanvas(readEvent);
c.setFullScreenMode(true);
display.setCurrent(c);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
[/]
RecordStoreWriter.java
[ lang="java"]
package freesrc.com;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
/**
*
* @author freesrc.com
*/
public class RecordStoreWriter {
public static final synchronized Event readEvent(String recordName) {
Event result = null;
RecordStore rec = null;
DataInputStream dis = null;
try {
rec = RecordStore.openRecordStore(recordName, true);
if (rec.getNumRecords() > 0) {
dis = new DataInputStream(new ByteArrayInputStream(rec.getRecord(1)));
result = new Event();
result.setName(dis.readUTF());
result.setImgWid(dis.readInt());
result.setImgHei(dis.readInt());
int[] imgData = new int[result.getImgWid() * result.getImgHei()];
for (int i = 0; i < imgData.length; i++) {
imgData[i] = dis.readInt();
}
result.setImage(imgData);
} else {
result = null;
}
} catch (Exception ex) {
ex.printStackTrace();
result = null;
} finally {
if (rec != null) {
try {
rec.closeRecordStore();
} catch (Exception e) {
result = null;
}
rec = null;
}
if (dis != null) {
try {
dis.close();
} catch (Exception e) {
}
rec = null;
}
}
return result;
}
public static synchronized void writeEvent(Event event, String recordName) {
RecordStore rec = null;
ByteArrayOutputStream bos = null;
DataOutputStream dos = null;
try {
deleteRecord(recordName);
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
int[] imgData = event.getImage();
dos.writeUTF(event.getName());
dos.writeInt(event.getImgWid());
dos.writeInt(event.getImgHei());
//write image
for (int i = 0; i < imgData.length; i++) {
dos.writeInt(imgData[i]);
}
dos.flush();
rec = RecordStore.openRecordStore(recordName, true);
byte[] b = bos.toByteArray();
rec.addRecord(b, 0, b.length);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (rec != null) {
try {
rec.closeRecordStore();
} catch (Exception e) {
}
rec = null;
}
if (dos != null) {
try {
dos.close();
} catch (IOException ex) {
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ex) {
}
}
}
}
//===delete record if exits
public static synchronized void deleteRecord(String name) {
String[] array = RecordStore.listRecordStores();
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (name.equals(array[i])) {
try {
RecordStore.deleteRecordStore(array[i]);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
}
Download NetBeans Project Files
Leave a Reply
You must be logged in to post a comment.
RSS