Hi all,
I am still struggling with saving image in the mobile device memory!
the procedure I am following (just simply to test reading an image from the memory and writing as a new file on the memory) is as below:
public void encode() {
String file_name = "file://localhost/root1/photos/App1.png";
String stegan = "file://localhost/root1/photos/App2.png";
InputStream is;
Image imImage, mImage;
byte img[];
try {
FileConnection fc = (FileConnection) Connector.open(file_name);
if (!fc.exists()) {
throw new IOException("File does not exist.");
}
System.out.println("file size : " + fc.fileSize());
is = fc.openInputStream();
imImage = Image.createImage(is); //immutable image
fc.close();
is.close();
//to create a mutable version of the image
mImage = Image.createImage(imImage.getWidth(), imImage.getHeight());
Graphics graphics = mImage.getGraphics();
graphics.drawImage(imImage, 0, 0, Graphics.TOP | Graphics.LEFT);
img = getByteArray(imImage);
System.out.println("img length : " + img.length);
FileConnection fc2 = (FileConnection) Connector.open(stegan);
if (!fc2.exists()) {
fc2.create();
}
OutputStream out;
out = fc2.openOutputStream();
out.write(img);
out.close();
fc2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public byte[] getByteArray(Image image) {
int raw[] = new int[image.getWidth() * image.getHeight()];
image.getRGB(raw, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
byte rawByte[] = new byte[image.getWidth() * image.getHeight() * 4];
int n = 0;
for (int i = 0; i < raw.length; i++) {
int ARGB = raw[i];
int a = (ARGB & 0xff000000) >> 24;
int r = (ARGB & 0xff0000) >> 16;
int g = (ARGB & 0xff00) >> 8;
int b = ARGB & 0xff;
rawByte[n] = (byte) b;
rawByte[n + 1] = (byte) g;
rawByte[n + 2] = (byte) r;
rawByte[n + 3] = (byte) a;
n += 4;
}
System.out.println("raw(int) length is: " + raw.length);
System.out.println("raw(Byte) length is: " + rawByte.length);
raw = null;
return rawByte;
}
The job it is doing is that it creates the new file App2.png, but the size is 4 times bigger than the original image and also the format is not correct. means that the image can not be displayed.
does anyone have any suggestions to make it work???
I am just waiting for any ideas....
thanks
