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

    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
Working...