2012-01-17 97 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);