Receive MMS images and make album using iamge using j2me

Posted by Abdul Basit on Stack Overflow See other posts from Stack Overflow or by Abdul Basit
Published on 2010-05-12T06:08:30Z Indexed on 2010/05/12 6:14 UTC
Read the original article Hit count: 496

Filed under:
|

I am trying to made application which receive MMS images and make a album from them user can view the pictures while running this application. I am facing problem while running application on mobile. while this application is fully working in wireless tookit emulator. Please guide me to fix this problem.`//package hello;

import javax.microedition.midlet.; import javax.microedition.lcdui.; import javax.wireless.messaging.*; import java.io.IOException; import java.util.Vector; import javax.microedition.io.Connector; import javax.microedition.lcdui.Display; //, ItemStateListener public class MMSS extends MIDlet implements CommandListener, Runnable, MessageListener { //-----------------------------------Receive MMS --------------------------- private Thread mReceiver = null; private boolean mEndNow = false; private Message msg = null; String msgReceived = null; private Image[] receivedImage = new Image[5]; private Command mExitCommand = new Command("Exit", Command.EXIT, 2); private Command mRedCommand = new Command("Back", Command.SCREEN, 1); private Command mBlueCommand = new Command("Next", Command.SCREEN, 1); private Command mPlay = new Command("Play", Command.SCREEN, 1);

protected static final String DEFAULT_IMAGE = "/MMSS_logo.jpg";
//protected static final String DEFAULT_IMAGE = "/wait.png";
private Display mDisplay = null;
//protected ImageItem mColorSquare = null;
protected Image mInitialImage = null;
private String mAppID = "MMSMIDlet";

private TextField imageName = null;
//private Form mForm = null;
private int count = 0;
private int next = 0;
private Integer mMonitor = new Integer(0);
//----------------------------------- End Receive MMS ---------------------------
private boolean midletPaused = false;
private Command exitCommand;
private Command exitCommand1;
private Command backCommand;
private Form form;
private StringItem stringItem;
private ImageItem imageItem;
private Image image1;
private Alert alert;
private List locationList;
private Alert cannotAddLocationAlert;

public MMSS() { }

/** * Initilizes the application. * It is called only once when the MIDlet is started. The method is called before the startMIDlet method. */ private void initialize() { }

/** * Performs an action assigned to the Mobile Device - MIDlet Started point. */ public void startMIDlet() { // write pre-action user code here switchDisplayable(null, getForm()); // write post-action user code here }

/** * Performs an action assigned to the Mobile Device - MIDlet Resumed point. */ public void resumeMIDlet() { }

/** * Switches a current displayable in a display. The display instance is taken from getDisplay method. This method is used by all actions in the design for switching displayable. * @param alert the Alert which is temporarily set to the display; if null, then nextDisplayable is set immediately * @param nextDisplayable the Displayable to be set / public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch // write pre-switch user code here Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch if (alert == null) { display.setCurrent(nextDisplayable); } else { display.setCurrent(alert, nextDisplayable); } } /* * Called by a system to indicated that a command has been invoked on a particular displayable. * @param command the Command that was invoked * @param displayable the Displayable where the command was invoked */ public void commandAction(Command command, Displayable displayable) { // write pre-action user code here if (displayable == form) { if (command == exitCommand) { // write pre-action user code here exitMIDlet(); // write post-action user code here } } // write post-action user code here }

/** * Returns an initiliazed instance of exitCommand component. * @return the initialized component instance */ public Command getExitCommand() { if (exitCommand == null) { // write pre-init user code here exitCommand = new Command("Exit", Command.EXIT, 0); // write post-init user code here } return exitCommand; }

/** * Returns an initiliazed instance of form component. * @return the initialized component instance */ public Form getForm() { if (form == null) { // write pre-init user code here form = new Form("Welcome to MMSS", new Item[] { getStringItem(), getImageItem() }); form.addCommand(getExitCommand()); form.setCommandListener(this); // write post-init user code here } return form; }

/** * Returns an initiliazed instance of stringItem component. * @return the initialized component instance */ public StringItem getStringItem() { if (stringItem == null) { // write pre-init user code here stringItem = new StringItem("Hello", "Hello, World!"); // write post-init user code here } return stringItem; }

/** * Returns an initiliazed instance of exitCommand1 component. * @return the initialized component instance / public Command getExitCommand1() { if (exitCommand1 == null) { // write pre-init user code here exitCommand1 = new Command("Exit", Command.EXIT, 0); // write post-init user code here } return exitCommand1; } /* * Returns an initiliazed instance of imageItem component. * @return the initialized component instance */ public ImageItem getImageItem() { if (imageItem == null) { // write pre-init user code here imageItem = new ImageItem("imageItem", getImage1(), ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER | Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND, "");//GEN-LINE:|26-getter|1|26-postInit // write post-init user code here } return imageItem; } /** * Returns an initiliazed instance of image1 component. * @return the initialized component instance */ public Image getImage1() { if (image1 == null) { // write pre-init user code here try { image1 = Image.createImage("/B.jpg"); } catch (java.io.IOException e) { e.printStackTrace(); } // write post-init user code here } return image1; }

/**
 * Returns a display instance.
 * @return the display instance.
 */
public Display getDisplay () {
    return Display.getDisplay(this);
}

/**
 * Exits MIDlet.
 */
public void exitMIDlet() {
    switchDisplayable (null, null);
    destroyApp(true);
    notifyDestroyed();
}

/**
 * Called when MIDlet is started.
 * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
 */
public void startApp() {
    if (midletPaused) {
        resumeMIDlet ();
    } else {
        initialize ();
        startMIDlet ();
    }
    midletPaused = false;
    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////

    try {
    conn = (MessageConnection) Connector.open("mms://:" + mAppID);
    conn.setMessageListener(this);
    } catch (Exception e) {
    System.out.println("startApp caught: ");
    e.printStackTrace();
    }
    if (conn != null) {
    startReceive();
    }

    ///////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////
}

/**
 * Called when MIDlet is paused.
 */
public void pauseApp() {
    midletPaused = true;
    mEndNow = true;
    try {
        conn.setMessageListener(null);
        conn.close();
    } catch (IOException ex) {
        System.out.println("pausetApp caught: ");
        ex.printStackTrace();
    }
}

/**
 * Called to signal the MIDlet to terminate.
 * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
 */
public void destroyApp(boolean unconditional) {
    mEndNow = true;
    try {
        conn.close();
    } catch (IOException ex) {
        System.out.println("destroyApp caught: ");
        ex.printStackTrace();
    }
}

private void startReceive() {
    mEndNow = false;

//---- Start receive thread mReceiver = new Thread(this); mReceiver.start(); }

protected MessageConnection conn = null;
protected int mMsgAvail = 0;

// -------------------- Get Next Images ------------------------------------
private void getMessage() {
    synchronized(mMonitor) {
    mMsgAvail++;
    mMonitor.notify();
    }
}
// -------------------- Display Images Thread ------------------------------
public void notifyIncomingMessage(MessageConnection msgConn) {
    if (msgConn == conn)
    getMessage();
}

public void itemStateChanged(Item item) {
    throw new UnsupportedOperationException("Not supported yet.");
}

class SetImage implements Runnable {
    private Image img = null;

    public SetImage(Image inImg) {
    img = inImg;
    }

    public void run() {
    imageItem.setImage(img);
    imageName.setString(Integer.toString(count));
    }
}


public void run() {

    mMsgAvail = 0;

    while (!mEndNow) {

    synchronized(mMonitor) { // Enter monitor
    if (mMsgAvail <= 0)
    try {
       mMonitor.wait();
    } catch (InterruptedException ex) {
    }
    mMsgAvail--;
    }

    try {

    msg = conn.receive();
    if (msg instanceof MultipartMessage) {
    MultipartMessage mpm = (MultipartMessage)msg;
    MessagePart[] parts = mpm.getMessageParts();

    if (parts != null)
    {
        for (int i = 0; i < parts.length; i++) {
        MessagePart mp = parts[i];
        byte[] ba = mp.getContent();
        receivedImage[count] = Image.createImage(ba, 0, ba.length);

    }


    Display.getDisplay(this).callSerially(new SetImage(receivedImage[count]));


             }
         }
      } catch (IOException e) {

          System.out.println("Receive thread caught: ");
       e.printStackTrace();
       }
    count++;
    } // of while
}

} `

© Stack Overflow or respective owner

Related posts about mms

Related posts about j2me