import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
public class ConnectionImage extends MIDlet implements CommandListener
{
Display tela;
Form imageForm;
ImageItem foto;
Command buscarCommand, sairCommand;
String url = "http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Firefox_LiNsta.png/50px-Firefox_LiNsta.png";
public ConnectionImage()
{
this.imageForm = new Form("");
this.foto = new ImageItem("foto",null,0,"");
this.sairCommand = new Command("Sair", Command.EXIT, 0);
this.buscarCommand = new Command("Buscar", Command.SCREEN, 1);
this.imageForm.addCommand(this.sairCommand);
this.imageForm.addCommand(this.buscarCommand);
this.imageForm.setCommandListener(this);
}
public void startApp()
{
this.tela = Display.getDisplay(this);
this.tela.setCurrent(this.imageForm);
}
public void pauseApp() {
}
public void destroyApp(boolean bool) {
}
public void commandAction(Command c, Displayable d) {
if (c == this.sairCommand)
{
this.destroyApp(true);
this.notifyDestroyed();
}
else if (c == this.buscarCommand)
{
if (imageForm.size() > 0){
for(int i=0; i < imageForm.size();i ++)
imageForm.delete(i);
}
try
{
Image im;
//chama o método que busca a imagem
im = getImage(this.url);
this.foto.setImage(im);
imageForm.append(this.foto);
//Display the form with the image
this.tela.setCurrent(this.imageForm);
}
catch (Exception e)
{
}
}
}
private Image getImage(String url) throws IOException
{
ContentConnection connection = (ContentConnection) Connector.open(url);
DataInputStream iStrm = connection.openDataInputStream();
Image im = null;
try
{
byte imageData[];
int length = (int) connection.getLength();
if(length != -1)
{
imageData = new byte[length];
iStrm.readFully(imageData);
}
else
{
ByteArrayOutputStream bStrm = new ByteArrayOutputStream();
int ch;
while ((ch = iStrm.read()) != -1)
bStrm.write(ch);
imageData = bStrm.toByteArray();
bStrm.close();
}
im = Image.createImage(imageData, 0, imageData.length);
}
finally
{
if (iStrm != null)
iStrm.close();
if (connection != null)
connection.close();
}
return (im == null ? null : im);
}
}
O que estou fazendo de errado?
Running in the identified_third_party security domain
Warning: To avoid potential deadlock, operations that may block, such as
networking, should be performed in a different thread than the
commandAction() handler.