j2me彩信

发送MMS

MMS发送格式: MMS://5550000:MIDletName
其中MMS://是协议, 5550000是手机号码, MIDletName是MIDlet Application Name。

// 建立连接
MessageConnection conn = (MessageConnection) Connector.open(“MMS://5550000:MMSMIDlet”);

// 建立数据包和设计数据包类型
MultipartMessage mpMessage = (MultipartMessage) conn.newMessage(MessageConnection.MULTIPART_MESSAGE);

// 填入header信息
mpMessage.setSubject("MMSMIDlet Image");

InputStream is = getClass().getResourceAsStream(mImageToSend);
byte[] bImage = new byte[is.available()];
is.read(bImage); //把图片变为byte数组

//加载数据包
mpMessage.addMessagePart(new MessagePart(bImage, 0, bImage.length,"image/png", "id1", "location", null));
conn.send(mpMessage); //发送


接收MMS

// 创建接收连接
MessageConnection conn = (MessageConnection) Connector.open(“mms://:MMSMIDlet”);
conn.setMessageListener(this); //register with Message Listener, implemented by this class

//接收MMS    
Message msg = conn.receive();

//获得数据包
MultipartMessage mpm = (MultipartMessage)msg;

//获得数据包里的内容
MessagePart[] parts = mpm.getMessageParts();

//读取数据
MessagePart mp = parts;
byte[] ba = mp.getContent();
receivedImage = Image.createImage(ba, 0, ba.length); //重组为图片 J2ME彩信

你可能感兴趣的:(j2me彩信)