How to save a microedition.lcdui.Image object into storage?

123 Views Asked by At

How can you save an Image (microedition.lcdui.Image) object into storage?

I have this Image object:

Image outImg; //Contains image data

I tried the code below to save it to storage but without success:

int[] rgb = new int[240*320];
outImg.getRGB(rgb, 0, 240, 0, 0, 240, 320);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeInt(240);
dout.writeInt(320);

for (int i = 0; i < rgb.length; i++) {
    dout.writeInt(rgb[i]);
}
bytes = bout.toByteArray();

FileConnection fcOut = (FileConnection) Connector.open("file:///root1/img.png", Connector.READ_WRITE);
if (!fcOut.exists()) {
    fcOut.create();
}
DataOutputStream out = fcOut.openDataOutputStream();
out.write(bytes);

I searched for a solution but didn't find anything useful. How can I achieve this?

Alternatively, is there a way I can convert an Image object to a byte array? So I can use DataOutputStream to write the byte array to memory.

0

There are 0 best solutions below