2011-11-16 119 views
0

當我按下加載按鈕,我想假設去廚房,選擇一張圖片,然後將imageview img設置爲該圖片。問題是我在廚房中選擇了一張照片後,imageview沒有更新。但是,如果我第二次按加載按鈕,則需要一兩秒鐘才能加載廚房,在此期間,imageview將加載我之前選擇的圖片。有人能幫助我,讓我能得到的ImageView刷新正確加載圖片後,imageview不更新

load.setOnClickListener(new OnClickListener(){ 
      @Override 
      public void onClick(View arg0) {   
       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); 
       try { 

        FileInputStream in = new FileInputStream(selectedImagePath); 
        bMap = BitmapFactory.decodeStream(in); 
        img.setImageBitmap(bMap); 
        if (in != null) { 
         in.close(); 
        } 
        img.invalidate(); 

       } catch (Exception e) {} 
      }  
}); 

回答

2

好了,這不以這種方式工作:) 你應該將你的代碼加載和設置位圖到onActivityResult方法。另外,捕獲異常實例非常糟糕 - 只嘗試捕獲相應的檢查異常 - 例如FileNotFoundException或某事 - 您可以刪除您的try-catch clasuse,然後在Eclipse中選擇FileInputStream in = new ...行時提示ctrl + 1 - >用try-catch選項選擇環繞聲,eclipse會爲你自動生成適當的catch子句(這裏我的意思是恰當的被檢查的異常處理,而不是catch子句體:))。

+0

謝謝,我把加載和設置位圖到我的活動結果後,它的工作 – help