to save an image into the mobile file system?

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development to save an image into the mobile file system?

Postby dargazi » Sun Jun 22, 2008 10:41 am

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);
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

//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);
if (!fc2.exists()) {


OutputStream out;
out = fc2.openOutputStream();

} catch (Exception e) {


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....
Posts: 1
Joined: Sun Jun 22, 2008 10:34 am

Re: to save an image into the mobile file system?

Postby DevelopmentTeam » Wed Jun 25, 2008 4:43 am

Firstly, I would like to know what problem you have got in this code. If it fails due to memory usage, you have to understand that J2Me cannot allow you to handle large memory in some devices. Otherwise, If the problem is with writing the file, please check the JSR-75 support of the device. Meanwhile, please post the error you have got so that we can proceed further in our research. ;)
User avatar
Site Admin
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 2 guests