tags:

views:

3805

answers:

4

hi,

How do I create and display an image in j2me application?

And in which folder can I put that image in my application?

+3  A: 

This link has exactly what you are looking for to get started.

Basically, to create the image, you call upon Image.createImage();

Image img = Image.createImage("/imageName.png");

If it is in a sub-folder in the Jar:

Image img = Image.createImage("/subDir/imageName.png");

To display the image, you need to paint it to a Canvas through a Graphics instance that is tied to the Canvas (better visualized in the link above).

public void paint(Graphics g) {
    ...
    g.drawImage(img, 0, 0, Graphics.TOP | Graphics.LEFT);
    ....
}

You could also use the Graphics.drawRegion function, but here is a link to the JavaDocs for J2ME for you to look through to see what is best for your needs.

Fostah
Using a backslash (\) will not work on most devices, instead you should use the forward slash (/) fir separating path names
Ram
A: 

To draw an Image on a JavaME MIDlet you need a Canvas to paint it on to. You can do as follow: Firs you have to place the original image file inside your package (usually inside "res" or one of his subdirectories). Secondly you need to create a class extending Canvas and implement the paint method:

import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class MyCanvas extends Canvas {
private Image image;
public MyCanvas(){
 try {
  image = Image.createImage("picture.png");
 } catch (IOException e) {
  e.printStackTrace();
 }
}

protected void paint(Graphics g) {
 g.drawImage(image, 10, 10, Graphics.TOP | Graphics.LEFT);
}
}

Now you need to create an instance of this class and tell the MIDlet di display it, for example:

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MyMIDlet extends MIDlet {
public MyMIDlet(){
}

protected void destroyApp(boolean unconditional)
  throws MIDletStateChangeException {
}

protected void pauseApp() {
}

protected void startApp() throws MIDletStateChangeException {
 Display.getDisplay(this).setCurrent(new MyCanvas());
}

}

Remember that this way the Canvas will be painted only one time and if you change something, you need to call the repaint() method.

Stefano Driussi
A: 

This source code builds on previously posted comments:

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class ImageLoader extends MIDlet
    implements CommandListener, Runnable {
  private Display mDisplay;
  private Form mForm;
  public ImageLoader() {
    mForm = new Form("Connecting...");
    mForm.addCommand(new Command("Exit", Command.EXIT, 0));
    mForm.setCommandListener(this);
  }
  public void startApp() {
    if (mDisplay == null) mDisplay = Display.getDisplay(this);
    mDisplay.setCurrent(mForm);     
    Thread t = new Thread(this);
    t.start();
  }
  public void pauseApp() {}
  public void destroyApp(boolean unconditional) {}
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT)
      notifyDestroyed();
  }
  public void run() {
    FileConnection fc = null;
    DataInputStream in = null;
    DataOutputStream out = null;    
    try {
      fc = (FileConnection)Connector.open("file:///root1/i.PNG");
      int length = (int)fc.fileSize();//possible loss of precision may throw error
      byte[] data = null;
      if (length != -1) {
        data = new byte[length];
        in = new DataInputStream(fc.openInputStream());
        in.readFully(data);
      }
      else {
        int chunkSize = 512;
        int index = 0;
        int readLength = 0;
        in = new DataInputStream(fc.openInputStream());
        data = new byte[chunkSize];
        do {
          if (data.length < index + chunkSize) {
            byte[] newData = new byte[index + chunkSize];
            System.arraycopy(data, 0, newData, 0, data.length);
            data = newData;
          }
          readLength = in.read(data, index, chunkSize);
          index += readLength;
        } while (readLength == chunkSize);
        length = index;
      }
      Image image = Image.createImage(data, 0, length);
      ImageItem imageItem = new ImageItem(null, image, 0, null);
      mForm.append(imageItem);
      mForm.setTitle("Done.");
      fc = (FileConnection)Connector.open("file:///root1/x.PNG");
      if(!fc.exists()){
          try{
          fc.create();
          }catch(Exception ce){System.out.print("Create Error: " + ce);}
      }
      out = new DataOutputStream(fc.openOutputStream());
      out.write(data);
    }
    catch (IOException ioe) {
      StringItem stringItem = new StringItem(null, ioe.toString());
      mForm.append(stringItem);
      mForm.setTitle("Done.");
    }
    finally {
      try {
        if (in != null) in.close();
        if (fc != null) fc.close();
      }
      catch (IOException ioe) {}
    }
  }
}

The code is modified from the link Fostah provided here.

It opens an image, displays it, then saves it as x.PNG instead of i.PNG using FileConnection. The tricky thing to watch for is where the file is being saved/loaded from. If your using J2meWTK with Netbeans, then the folder will be displayed in the output window when you run the mobile app. The folder will be something like temp.DefaultColorPhone/filesystem/root1 . That is where you will have to have an image. I'm not sure how to have the temp environment created with the image by default. That means you have to start the mobile app, check where the temp root1/ is located, in your IDE, then drop the image into the folder, then proceed with running the ImageLoader application. I'll try to find out how to automate this by posting a question. Also, Start with a small image, 50x50 (bigger images may cause problems).

A: 

do { if (data.length < index + chunkSize) { byte[] newData = new byte[index + chunkSize]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; } readLength = in.read(data, index, chunkSize); index += readLength; } while (readLength == chunkSize);

In the above code, am getting out of memory exception when am trying to read the images in phone gallery which are more than 230kb size. How do i solve this issue? I want to display and also send across the image which can be more than 230kb to the server over bluetooth connection. Please reply asap. Thanks in advance.