Java 使用j2me和lwuit在后台下载图像
我有一个从互联网下载的图像列表,并在列表中填充,每个图像旁边都有描述文本 我现在遇到的问题是,当我理解的图像被下载时,应用程序会被冻结` 我创建了一个线程来做下载,类如下。我使用start方法来启动线程,但在使用run之前,映像将为nullJava 使用j2me和lwuit在后台下载图像,java,java-me,lwuit,Java,Java Me,Lwuit,我有一个从互联网下载的图像列表,并在列表中填充,每个图像旁边都有描述文本 我现在遇到的问题是,当我理解的图像被下载时,应用程序会被冻结` 我创建了一个线程来做下载,类如下。我使用start方法来启动线程,但在使用run之前,映像将为null 有人能帮忙吗?你得到的评论可能是正确的。这显示了一些与您试图实现的目标相似的东西。它在中,但大多数代码也应该适用于LWUIT。您得到的注释可能是正确的。这显示了一些与您试图实现的目标相似的东西。它在中,但大多数代码也应该适用于LWUIT。我唯一能想到的是,您
有人能帮忙吗?你得到的评论可能是正确的。这显示了一些与您试图实现的目标相似的东西。它在中,但大多数代码也应该适用于LWUIT。您得到的注释可能是正确的。这显示了一些与您试图实现的目标相似的东西。它在中,但大多数代码也应该适用于LWUIT。我唯一能想到的是,您可能正在创建线程并调用run而不是start。start方法将使异步魔法发生。是的,我使用start,但在使用run之前,映像将为null。您收到的错误是因为您没有正确同步。重新检查-所有这些神秘的synchronized、wait、notify都是有原因的。我唯一能想到的是,您可能正在创建线程并调用run而不是start。start方法将使异步魔法发生。是的,我使用start,但在使用run之前,映像将为null。您收到的错误是因为您没有正确同步。重新检查-所有这些神秘的同步,等等,通知都是有原因的谢谢各位,你们的评论非常有用。我最终使用了lwuit EDT公开的CallSerialyandWait方法,它非常有效。谢谢大家,你们的评论非常有用。我最终使用了lwuit EDT公开的CallSerialyandWait方法,它真的很有效。
public class GetImage extends Thread {
public String imgString;
private String url;
private Label label;
public Image img = null;
public GetImage(String url, Label label){
this.url = url;
this.label = new Label();
this.label = label;
}
public Image getPic()
{
return img;
}
public void run()
{
this.getImage_();
this.label.setIcon(img.scaledHeight(60));
}
public void getImage_()
{
HttpConnection hc = null;
DataInputStream dis = null;
DataOutputStream dos = null;
StringBuffer messageBuffer = new StringBuffer();
try{
hc = (HttpConnection)Connector.open(this.url,Connector.READ);
dis = new DataInputStream(hc.openDataInputStream());
int ch;
long len = hc.getLength();
if(len != -1)
{
for(int i=0; i < len; i++)
if((ch = dis.read())!=-1)
messageBuffer.append((char)ch);
}
else
{
while((ch = dis.read()) != -1)
messageBuffer.append((char)ch);
}
this.img = Image.createImage(messageBuffer.toString().getBytes(),0,messageBuffer.toString().length());
dis.close();
}
catch(IOException ae){
messageBuffer.append("Error");
}
finally{
try {
if (hc != null) hc.close();
}
catch (IOException ignored) {}
try {
if (dis != null) dis.close();
}
catch (IOException ignored) {}
try {
if (dos != null) dos.close();
}
catch (IOException ignored) {}
}
//return this.img;
}
}
public class ListRenderer extends Label implements ListCellRenderer {
public ListRenderer()
{
super();
}
public Component getListCellRendererComponent(List list, Object o, int i, boolean bln) {
//cast the value object into a Content
Contents entry = (Contents)o;
//get the icon of the Content and set it for this label
this.setIcon(entry.getIcon());
//get the text of the Content and set it for this label
this.setText(entry.getText());
//set transparency
getStyle().setBgTransparency((byte)128);
//set background and foreground colors
//depending on whether the item is selected or not
if(bln)
{
getStyle().setBgColor(0xffcc33);
getStyle().setFgColor(0x000000);
}
else
{
getStyle().setBgColor(0xffffff);
}
return this;
}
public Component getListFocusComponent(List list) {
setText("");
setIcon(null);
getStyle().setBgColor(0xffcc33);
getStyle().setBgTransparency(80);
return this;
}
}