runtime error during downloading images from server to mobile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • surja
    New Member
    • Dec 2007
    • 5

    #1

    runtime error during downloading images from server to mobile

    Hi,
    I have written a code to download images from a server end desktop,
    but while running the code ,WTK is showing a runtime error " Create image from Byte array
    Uncaught exception java/lang/IllegalArgument Exception: ".
    I cannot detect the necessary change I have to make to run my code succesfully.Ple ase anyone can help me to solve this code

    [CODE=java]/code/
    import javax.microedit ion.midlet.*;
    import javax.microedit ion.lcdui.*;
    import javax.microedit ion.io.*;
    import java.io.*;
    import java.util.*;
    import javax.microedit ion.io.HttpConn ection;

    public class DownloadImage extends MIDlet implements CommandListener
    {
    private Command exit,start;
    public Display display;
    private Form form;
    private StringItem stars;

    public DownloadImage()
    {
    display = Display.getDisp lay(this);
    exit = new Command("Exit", Command.EXIT,1) ;
    start = new Command("start" ,Command.SCREEN ,1);
    form = new Form("Picture") ;
    form.addCommand (exit);
    form.addCommand (start);
    form.setCommand Listener(this);

    }


    public void startApp() throws MIDletStateChan geException
    {
    //display.setCurr ent(form);//
    if(display==nul l)
    display=Display .getDisplay(thi s);
    display.setCurr ent(form);
    System.out.prin tln("start");

    //Do network loading in separate thread


    }

    public void pauseApp()
    {
    }

    public void destroyApp(bool ean unconditional)
    {
    }

    public void commandAction(C ommand command,Display able displayable)
    {
    if(command == exit)
    {
    destroyApp(fals e);
    notifyDestroyed ();
    }
    else if (command == start)
    {
    Form wform=new Form("picture1" );
    display.setCurr ent(wform);
    System.out.prin tln("command");

    Thread t=new Thread()
    {
    public void run()
    {
    connect();
    System.out.prin tln("connect");
    }
    };t.start();

    }

    }
    public void connect()
    {
    ContentConnecti on connection = null;

    //InputStream inputstream = null;
    Image image = null;


    try

    {
    System.out.prin tln("connection ");
    connection = (ContentConnect ion)Connector.o pen("http://172.24.182.155: 8080/surja/logo.png");
    System.out.prin tln("retreive") ;
    //InputStream inputstream = (InputStream)Co nnector.openInp utStream(connec tion);
    DataInputStream inputstream = connection.open DataInputStream ();

    ByteArrayOutput Stream bytearray = new ByteArrayOutput Stream();
    int ch;
    while ((ch = inputstream.rea d()) != -1)
    {
    bytearray.write (ch);
    System.out.prin tln("read");
    }

    byte imagearray[] = bytearray.toByt eArray();

    //Create image from Byte array
    System.out.prin tln("Create image from Byte array");
    image = image.createIma ge(imagearray,0 ,imagearray.len gth);
    //form.append(ima ge);
    System.out.prin tln("Image created");
    }
    catch (IOException error)
    {
    Alert alert = new Alert("Error"," Cannnot connect"+ error,null,null );
    alert.setTimeou t(Alert.FOREVER );
    alert.setType(A lertType.ERROR) ;
    display.setCurr ent(alert);

    }
    }


    //}
    }/CODE/[/CODE]


    i´m grateful for every hint and solution!
    surja
    Last edited by RedSon; Dec 28 '07, 04:51 PM. Reason: [CODE] tags.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    We have [CODE] tags [/CODE] for a reason... Please use them.

    Moved from the mobile forum.
    Last edited by RedSon; Dec 28 '07, 06:36 PM.

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Check: What is the value of imagearray.leng th? Is it > 0?

      Comment

      • surja
        New Member
        • Dec 2007
        • 5

        #4
        runtime error during downloading images from server to mobile

        runtime error during downloading images from server to mobile
        --------------------------------------------------------------------------------

        Hi,
        I have written a code to download images from a server end desktop,
        but while running the code ,WTK is showing a runtime error " Create image from Byte array
        Uncaught exception java/lang/IllegalArgument Exception: ".
        I cannot detect the necessary change I have to make to run my code succesfully.Ple ase anyone can help me to solve this code


        Code: ( java )


        Code:
        import javax.microedition.midlet.*;
        import javax.microedition.lcdui.*;
        import javax.microedition.io.*;
        import java.io.*;
        import java.util.*;
        import javax.microedition.io.HttpConnection;
         
        public class DownloadImage extends MIDlet implements CommandListener
        {
         private Command exit,start;
         public Display display;
         private Form form;
         private StringItem stars;
         
         public DownloadImage()
         {
           display = Display.getDisplay(this);
           exit = new Command("Exit",Command.EXIT,1);
           start = new Command("start",Command.SCREEN,1);
           form = new Form("Picture");
           form.addCommand(exit);
           form.addCommand(start);
           form.setCommandListener(this);
         
         }
         
         
         public void startApp() throws MIDletStateChangeException
         {
           //display.setCurrent(form);//
           if(display==null)
            display=Display.getDisplay(this);
             display.setCurrent(form);
             System.out.println("start");
         
             //Do network loading in separate thread
         
         
         }
         
         public void pauseApp()
         {
         }
         
         public void destroyApp(boolean unconditional)
         {
         }
         
         public void commandAction(Command command,Displayable displayable)
          {
           if(command == exit)
           {
            destroyApp(false);
            notifyDestroyed();
           }
            else if (command == start)
            {
                Form wform=new Form("picture1");
                display.setCurrent(wform);
                System.out.println("command");
         
                Thread t=new Thread()
                {
                    public void run()
                      {
                         connect();
                         System.out.println("connect");
                       }
                 };t.start();
         
             }
         
           }
           public void connect()
           {
             ContentConnection connection = null;
         
             //InputStream inputstream = null;
                  Image image = null;
         
         
            try
         
              {
                  System.out.println("connection");
            connection = (ContentConnection)Connector.open("http://172.24.182.155:8080/surja/logo.png");
                  System.out.println("retreive");
             //InputStream inputstream = (InputStream)Connector.openInputStream(connection)  ;
                  DataInputStream inputstream = connection.openDataInputStream();
         
                ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
                int ch;
                while ((ch = inputstream.read()) != -1)
                 {
                     bytearray.write(ch);
                     System.out.println("read");
                  }
         
                 byte imagearray[] = bytearray.toByteArray();
         
                 //Create image from Byte array
                    System.out.println("Create image from Byte array");
                 image = image.createImage(imagearray,0,imagearray.length);  
                 //form.append(image);
                   System.out.println("Image created");
               }
               catch (IOException error)
               {
                Alert alert = new Alert("Error","Cannnot connect"+ error,null,null);
                alert.setTimeout(Alert.FOREVER);
                alert.setType(AlertType.ERROR);
                display.setCurrent(alert);
         
               }
            }
         
         
           //}
         }


        i´m grateful for every hint and solution!
        surja
        Last edited by RedSon; Dec 31 '07, 05:11 PM. Reason: Fixed code tag, nice try.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Come on surja!

          Are you just not paying attention or something? First you posted in the wrong forum, then you don't use code tags, then you double post in the wrong form again. Now I have merged your posts! I am losing patience.

          Please be warned to not muck up the forums again by reposting this question again in the wrong forum.

          Comment

          Working...