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
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
Comment