tags:

views:

1032

answers:

3

hello,

i am programming a mobile application in j2me in which i need to send an image from one mobile to another via sms. the problem is being encountered at the receiving end.the image is not being decoded properly.it is throwing ioexception....i m posting the code here..plz help me.

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.IOException;
import javax.microedition.lcdui.game.*;
import java.lang.*;
import java.io.*;

public class receive_mms extends MIDlet implements CommandListener
 {
    Display disp;
    //TextBox txtbox;
    MessageConnection msgConn; 
    Message msg;
    Form frm=null;
    byte[] msgrev;
    byte[] data;
    //String msgrev;
    Image im=null;
    Image im1=null;
    ImageItem img=null;
    int i,j;
    ByteArrayInputStream bais = null;
    Command cmd_exit;


public receive_mms(){
 disp=Display.getDisplay(this);
 frm=new Form("photo dikho");
 i=frm.getWidth();
 j=frm.getHeight();
 cmd_exit=new Command("exit",Command.EXIT,1);
 frm.addCommand(cmd_exit);
 frm.setCommandListener(this);
 disp.setCurrent(frm);

 Thread t1 = new Thread()
 {
  public void run()
  {recieve();}
 };
 t1.start();


 //txtbox=new TextBox("Recieve Text","",100,TextField.ANY);  

}

public void commandAction(Command c,Displayable d)
{   
          if(c==cmd_exit)
       {  
           notifyDestroyed();       
    }
}




public void startApp(){/*
disp.setCurrent(frm);

 Thread t1 = new Thread()
 {
  public void run()
  {recieve();}
 };
 t1.start();


 */
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}

public void recieve(){
 //while(true)
 //{
  String mSenderAddress="";
  try{

  msgConn = (MessageConnection) Connector.open("sms://:1234");
  System.out.println("11");
  msg = msgConn.receive();// start listening and stuck here until a msg is received
  System.out.println("12");
  mSenderAddress = msg.getAddress();// Get info from message, from where da msg is beign sent
  System.out.println("3");
  System.out.println("add"+ mSenderAddress);
  System.out.println("msg aya:" + msg);
 msgConn.close();
  }catch(Exception e){System.out.println(e);}


  if (msg instanceof BinaryMessage) {
  //try{
   msgrev = ((BinaryMessage)msg).getPayloadData(); 
   data=msgrev.toByteArray();
            String val= new String(data);   
   System.out.println("yahoo");

   System.out.println("yahoo1");

 System.out.println(val);
   create(data); 


  }

}
public void create(byte[] bs)
{
 try
 {         
            String str=bs.toString();
            /*
   StringBuffer d=new StringBuffer();
          bais=new ByteArrayInputStream(bs);
    DataInputStream ds=new DataInputStream(bais);
    int len=bs.length;
    System.out.println("len="+len);
    if(len!=0)
    {
     int ch=0;
     while((ch=ds.read())!=-1)
     {
      d.append((char)ch);
     }

    }
  System.out.println(d);
  str=d.toString();
  */
  //str=bs.toString();
  InputStream is= this.getClass().getResourceAsStream(str);
        System.out.println("string is"+str);           
  im = (Image)Image.createImage(is);
  System.out.println("line");
  im1 = (Image)Image.createImage(im, 0, 0, i, j, Sprite.TRANS_NONE);
  img = new ImageItem("yeh photo snd hui", im1, Item.LAYOUT_CENTER, "kyu nhi dikh rhi", Item.BUTTON);
  frm.append(img);



 }
 catch (Exception e)
 {
  System.out.println(e);
 }

}
}
A: 

You're doing a few very odd things:

  1. converting the byte array to a String, particularly using byte[].toString()
  2. attempting to get an InputStream by calling Class.getResourceAsStream() with a String that has been created from the byte array.
  3. using SMS to send an Image

Class.getResourceAsStream() is intended to take a String identifying a resource file within the MIDlet's jar file.

The correct way to do this is to get the byte[] from the BinaryMessage and use this to create an Image using Image.createImage(bytes, 0, bytes.length);

Although, as you're sending it using SMS, I'd hope it was a very small image indeed or anybody using this app will incur high costs from splitting a large image over several SMSs. Beware also that some networks limit the number of parts that an SMS can be split into.

You would be much better off researching the MMS sending functionality provided by JSR 205.

funkybro
A: 

Hello,

You are getting wrong the data stream, here is how you must do it:

public void create(byte[] bs)
{
    try
    {      
            im = (Image)Image.createImage(bs, 0, bs.length);                
            im1 = (Image)Image.createImage(im, 0, 0, i, j, Sprite.TRANS_NONE);
            img = new ImageItem("yeh photo snd hui", im1, Item.LAYOUT_CENTER, "kyu nhi dikh rhi", Item.BUTTON);
            frm.append(img);
    }
    catch (Exception e)
    {
            System.out.println(e);
    }
}

This should work.

Lucas S.
A: 

hai, i am supposed to do almost the same thing .. can you help me pls...

i need to send a text output of a j2me apllication as an sms to another mobile.

i am an absolute beginner in j2me..pls help me.

also can you pls tell me how to add a notepad to a j2me application..

pls mail me at [email protected]

thanks in advance.