Tutorials J2ME How to Send MMS from J2ME

How to Send MMS from J2ME

Wireless Messaging API (JSR-120 or WMA) for Java, allows access to wireless communication resources on J2ME devices. MMS is a mulitpart message and WMA api support mulitpart messages.
Find the code snippet here:

package com.neel.managers;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.TextField;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessagePart;
import javax.wireless.messaging.MultipartMessage;
import com.neel.midlet.Paint;
import com.neel.ui.PaintScreen;

/**
*@author javatechig
* Class Name: MMSManager
* used By : PaintCanvas
*/
public class MMSManager implements Runnable, CommandListener{
PaintScreen paintScreen;
Paint midlet;
Form MMSForm;
String number=null;
String subject=null;
TextField mobileNumber_txt;
TextField subject_txt;

public MMSManager(Paint midlet,PaintScreen paintScreen){
      this.midlet=midlet;
      this.paintScreen=paintScreen;
      MMSForm=new Form("");
      midlet.getDisplay().setCurrent(MMSForm);
      mobileNumber_txt=new TextField("Mobile No:","",12,1);
      subject_txt=new TextField("Subject  :","",12,1);
      MMSForm.append(mobileNumber_txt);
      mobileNumber_txt.setLayout(Item.LAYOUT_CENTER);
      MMSForm.append(new Spacer(2,3));
      MMSForm.append(subject_txt);
      subject_txt.setLayout(Item.LAYOUT_CENTER);
      MMSForm.append(new Spacer(2,10));

      ImageItem imageitem=new ImageItem("",scaleImage(targetImage,170,200), 
                                 Item.LAYOUT_CENTER,"");
      MMSForm.append(imageitem);
      MMSForm.addCommand(new Command("Back",Command.BACK,0));
      MMSForm.addCommand(new Command("Send",Command.OK,0));
      MMSForm.setCommandListener(this);
}

public void commandAction(Command c, Displayable d) {
     if(d==MMSForm && c.getLabel().equals("Back")){
        midlet.getDisplay().setCurrent(paintScreen);
     }
    else if(d==MMSForm && c.getLabel().equals("Send")){
      sendMMS();
    }
}

private void sendMMS() {
      Thread t = new Thread(this);
      t.start();
}

public void run(){
    try { 
       String mmsUrl = "mms://+919535080650:com.test.MyApp";
       MessageConnection connection = (MessageConnection) Connector.open(mmsUrl);
       MultipartMessage message = (MultipartMessage) 
              connection.newMessage(MessageConnection.MULTIPART_MESSAGE);
       message.setAddress(mmsUrl);
       message.setSubject(subject);

       InputStream is = getClass().getResourceAsStream("/image.jpg");
       byte[] imageBytes = new byte[is.available()];
       is.read(imageBytes);

       message.addMessagePart(new MessagePart(imageBytes, 0, imageBytes.length,
                              "image/jpg", "card", "card image", null));
       connection.send(message);
       if (connection != null) {
           try {
               connection.close();
           } catch (IOException ioe) {
               ioe.printStackTrace();
           }
       }
       } catch (Exception e) {
           e.printStackTrace();
       }
}
Total
0
Shares
Previous Post
j2me_tutorials_javatechig_featured_image

Localization in LWUIT

Next Post
j2me_tutorials_javatechig_featured_image

How to scale Image in J2ME

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All