By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

runtime error during downloading images from server to mobile

P: 5
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/IllegalArgumentException: ".
I cannot detect the necessary change I have to make to run my code succesfully.Please anyone can help me to solve this code

Expand|Select|Wrap|Line Numbers
  1. /code/
  2. import javax.microedition.midlet.*;
  3. import javax.microedition.lcdui.*;
  4. import javax.microedition.io.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import javax.microedition.io.HttpConnection;
  8.  
  9. public class DownloadImage extends MIDlet implements CommandListener
  10. {
  11.  private Command exit,start;
  12.  public Display display;
  13.  private Form form;
  14.  private StringItem stars;
  15.  
  16.  public DownloadImage()
  17.  {
  18.    display = Display.getDisplay(this);
  19.    exit = new Command("Exit",Command.EXIT,1);
  20.    start = new Command("start",Command.SCREEN,1);
  21.    form = new Form("Picture");
  22.    form.addCommand(exit);
  23.    form.addCommand(start);
  24.    form.setCommandListener(this);
  25.  
  26.  }
  27.  
  28.  
  29.  public void startApp() throws MIDletStateChangeException
  30.  {
  31.    //display.setCurrent(form);//
  32.    if(display==null)
  33.     display=Display.getDisplay(this);
  34.      display.setCurrent(form);
  35.      System.out.println("start");
  36.  
  37.      //Do network loading in separate thread
  38.  
  39.  
  40.  }
  41.  
  42.  public void pauseApp()
  43.  {
  44.  }
  45.  
  46.  public void destroyApp(boolean unconditional)
  47.  {
  48.  }
  49.  
  50.  public void commandAction(Command command,Displayable displayable)
  51.   {
  52.    if(command == exit)
  53.    {
  54.     destroyApp(false);
  55.     notifyDestroyed();
  56.    }
  57.     else if (command == start)
  58.     {
  59.         Form wform=new Form("picture1");
  60.         display.setCurrent(wform);
  61.         System.out.println("command");
  62.  
  63.         Thread t=new Thread()
  64.         {
  65.             public void run()
  66.               {
  67.                  connect();
  68.                  System.out.println("connect");
  69.                }
  70.          };t.start();
  71.  
  72.      }
  73.  
  74.    }
  75.    public void connect()
  76.    {
  77.      ContentConnection connection = null;
  78.  
  79.      //InputStream inputstream = null;
  80.           Image image = null;
  81.  
  82.  
  83.     try
  84.  
  85.       {
  86.           System.out.println("connection");
  87.     connection = (ContentConnection)Connector.open("http://172.24.182.155:8080/surja/logo.png");
  88.           System.out.println("retreive");
  89.      //InputStream inputstream = (InputStream)Connector.openInputStream(connection);
  90.           DataInputStream inputstream = connection.openDataInputStream();
  91.  
  92.         ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
  93.         int ch;
  94.         while ((ch = inputstream.read()) != -1)
  95.          {
  96.              bytearray.write(ch);
  97.              System.out.println("read");
  98.           }
  99.  
  100.          byte imagearray[] = bytearray.toByteArray();
  101.  
  102.          //Create image from Byte array
  103.             System.out.println("Create image from Byte array");
  104.          image = image.createImage(imagearray,0,imagearray.length);
  105.          //form.append(image);
  106.            System.out.println("Image created");
  107.        }
  108.        catch (IOException error)
  109.        {
  110.         Alert alert = new Alert("Error","Cannnot connect"+ error,null,null);
  111.         alert.setTimeout(Alert.FOREVER);
  112.         alert.setType(AlertType.ERROR);
  113.         display.setCurrent(alert);
  114.  
  115.        }
  116.     }
  117.  
  118.  
  119.    //}
  120.  }/CODE/

im grateful for every hint and solution!
surja
Dec 28 '07 #1
Share this Question
Share on Google+
4 Replies


RedSon
Expert 5K+
P: 5,000
We have [CODE] tags [/code] for a reason... Please use them.

Moved from the mobile forum.
Dec 28 '07 #2

BigDaddyLH
Expert 100+
P: 1,216
Check: What is the value of imagearray.length? Is it > 0?
Dec 28 '07 #3

P: 5
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/IllegalArgumentException: ".
I cannot detect the necessary change I have to make to run my code succesfully.Please anyone can help me to solve this code


Code: ( java )


Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.microedition.midlet.*;
  3. import javax.microedition.lcdui.*;
  4. import javax.microedition.io.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import javax.microedition.io.HttpConnection;
  8.  
  9. public class DownloadImage extends MIDlet implements CommandListener
  10. {
  11.  private Command exit,start;
  12.  public Display display;
  13.  private Form form;
  14.  private StringItem stars;
  15.  
  16.  public DownloadImage()
  17.  {
  18.    display = Display.getDisplay(this);
  19.    exit = new Command("Exit",Command.EXIT,1);
  20.    start = new Command("start",Command.SCREEN,1);
  21.    form = new Form("Picture");
  22.    form.addCommand(exit);
  23.    form.addCommand(start);
  24.    form.setCommandListener(this);
  25.  
  26.  }
  27.  
  28.  
  29.  public void startApp() throws MIDletStateChangeException
  30.  {
  31.    //display.setCurrent(form);//
  32.    if(display==null)
  33.     display=Display.getDisplay(this);
  34.      display.setCurrent(form);
  35.      System.out.println("start");
  36.  
  37.      //Do network loading in separate thread
  38.  
  39.  
  40.  }
  41.  
  42.  public void pauseApp()
  43.  {
  44.  }
  45.  
  46.  public void destroyApp(boolean unconditional)
  47.  {
  48.  }
  49.  
  50.  public void commandAction(Command command,Displayable displayable)
  51.   {
  52.    if(command == exit)
  53.    {
  54.     destroyApp(false);
  55.     notifyDestroyed();
  56.    }
  57.     else if (command == start)
  58.     {
  59.         Form wform=new Form("picture1");
  60.         display.setCurrent(wform);
  61.         System.out.println("command");
  62.  
  63.         Thread t=new Thread()
  64.         {
  65.             public void run()
  66.               {
  67.                  connect();
  68.                  System.out.println("connect");
  69.                }
  70.          };t.start();
  71.  
  72.      }
  73.  
  74.    }
  75.    public void connect()
  76.    {
  77.      ContentConnection connection = null;
  78.  
  79.      //InputStream inputstream = null;
  80.           Image image = null;
  81.  
  82.  
  83.     try
  84.  
  85.       {
  86.           System.out.println("connection");
  87.     connection = (ContentConnection)Connector.open("http://172.24.182.155:8080/surja/logo.png");
  88.           System.out.println("retreive");
  89.      //InputStream inputstream = (InputStream)Connector.openInputStream(connection)  ;
  90.           DataInputStream inputstream = connection.openDataInputStream();
  91.  
  92.         ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
  93.         int ch;
  94.         while ((ch = inputstream.read()) != -1)
  95.          {
  96.              bytearray.write(ch);
  97.              System.out.println("read");
  98.           }
  99.  
  100.          byte imagearray[] = bytearray.toByteArray();
  101.  
  102.          //Create image from Byte array
  103.             System.out.println("Create image from Byte array");
  104.          image = image.createImage(imagearray,0,imagearray.length);  
  105.          //form.append(image);
  106.            System.out.println("Image created");
  107.        }
  108.        catch (IOException error)
  109.        {
  110.         Alert alert = new Alert("Error","Cannnot connect"+ error,null,null);
  111.         alert.setTimeout(Alert.FOREVER);
  112.         alert.setType(AlertType.ERROR);
  113.         display.setCurrent(alert);
  114.  
  115.        }
  116.     }
  117.  
  118.  
  119.    //}
  120.  }


im grateful for every hint and solution!
surja
Dec 31 '07 #4

RedSon
Expert 5K+
P: 5,000
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.
Dec 31 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.