2012-01-17 84 views
2

在onCreate事件我做一个异步请求Facebook的API:更新的ImageView的Android

Utility.mAsyncRunner.request(null, params1, 
        new myListener()); 

在myListener的公司的onComplete事件中,我想设置图像中的ImageView

public class PhotoDisplayListener extends BaseRequestListener { 

     @Override 
     public void onComplete(final String response, final Object state) { 
     //set my image with the url provided in the response 
     ...parse json.... 
ImageView img = (ImageView)findViewById(R.id.img); 


       URL ulrn = new URL(photourl); 
       HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
       InputStream is = con.getInputStream(); 
       Bitmap bmp = BitmapFactory.decodeStream(is); 
       if (null != bmp){ 
        img.setImageBitmap(bmp); 
        setImage(bmp); 
        } 
       else 
        System.out.println("The Bitmap is NULL"); 

       }catch(Exception e){} catch (FacebookError e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        showToast(e.getMessage()); 
       } 

} 

}

问题是imageview没有刷新我设定的新图片。 它设置它,它不会刷新它。如果我锁定并解锁手机,则图像应该显示。 我该如何强制刷新imageview? 谢谢!

回答

1

您无法从外部(另一个线程)更新UI线程。我建议你调用一个方法来更新ImageView。

一篇关于这个话题:Painless Threading

+0

它不见了404 :( – dinigo 2012-10-02 22:26:32

+1

@ demil133我刚更新了链接,希望它有帮助 – SERPRO 2012-10-03 08:57:41

4

使用img.invalidate();使设置图像后ImageView的刷新。

这样的:

img.setImageBitmap(bmp); 
img.invalidate(); 

希望这可以帮助你

0

我看到过几个职位之前,我问的问题在这里。这不是原因,因为我之前尝试过。我认为问题是我显示图像的方式。

我有一台服务器上的图像,我用从客户端收到的图像替换。问题是一旦我从客户端发送图像,服务器图像消失,即使图像没有完全收到。

我想我必须找到一种方法来完全接收图像第一,然后才做img.SetBitmap(bmp);