Tuesday, March 06, 2007

Code:How to write an image in mmapi

How to write an image in mmapi?

I installed and executed this code in my old fashion nokia 6600.
String name;
int recId; // returned by addRecord but not used
TextField tmp = (TextField)get_saveForm().get(0);
name = tmp.getString();
int width = image.getWidth();
int height = image.getHeight();
byte [] rgbData = pngData;//
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baos);
try{
imageStore = RecordStore.openRecordStore("myimages",true);//image store name
outputStream.writeUTF(name);
outputStream.writeInt(width);
outputStream.writeInt(height);
outputStream.writeInt(rgbData.length);
for(int i=0;i TO rgbData.length;i++)
outputStream.writeByte(rgbData[i]);//write byte and forget out of memory error!

outputStream.flush();
outputStream.close();
byte[] b = baos.toByteArray();
recId = imageStore.addRecord(b, 0, b.length);
// Alert alert = new Alert("ImageSaved" ,name +" Saved",null,AlertType.INFO);
// Display.getDisplay(midlet).setCurrent(alert,this);
Display.getDisplay(midlet).setCurrent(this);
// System.out.println("Image Saved");
}catch(IOException ioe){
Alert alert = new Alert("Image" ,name +"IOException",null,AlertType.ERROR);
Display.getDisplay(midlet).setCurrent(alert,this);
}
catch (Exception e) {
// Log the exception
Alert alert = new Alert("Image" ,name +" Not Saved",null,AlertType.ERROR);
Display.getDisplay(midlet).setCurrent(alert,this);
} finally {
try {
// Close the Record Store
if (imageStore != null) imageStore.closeRecordStore();
} catch (Exception ignore) {
// Ignore
Alert alert = new Alert("Exception" ,ignore.getMessage(),null,AlertType.ERROR);
Display.getDisplay(midlet).setCurrent(alert,this);
}
}

}

TO will be '<' i cant write because of HTML error of silly blogspot editor

No comments: