2012-10-05 34 views
2

我有一個小問題。關閉相機意圖後無法更改imageview src

代碼目標:點擊打開相機的imageview,然後在獲取圖片後更改imageview。 imageview不是照片的縮略圖。

問題:imageview不刷新。

我已經試過:Android imageView Switching ProblemChanging ImageView on button clickImageView onClickListener changing the image source (不能使用viewSwitcher B/C有兩個以上的可能視圖組合), how to change the background or the image source when a clickable imageview in android is clicked?

我的代碼:

public class CacTestActivity extends Activity { 

private static final int CAMERA_PIC_REQUEST = 2500; 
private Bitmap bmp; 
private ImageView ivPic; 
private Thread thread; 

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.cactest); 

    ivPic = (ImageView) findViewById(R.id.imgCacSetPhoto); 

    ivPic.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      try { 
       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(intent, CAMERA_PIC_REQUEST); 
      } catch (Exception e) { 
      } 
     } 
    }); 
    thread = new Thread(){ 
     @Override 
     public void run(){ 
      ivPic = (ImageView) findViewById(R.id.imgCacSetPhoto); 
      ivPic.setImageResource(R.drawable.takephotoyes); 
      ivPic.invalidate(); 
     } 
    }; 
} 

protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT){ 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    } 
    if (resultCode == RESULT_OK){ 
     if (requestCode == CAMERA_PIC_REQUEST){ 
      //If getting pic bitmap is successful, then change imageview source 
      //thread.start();  //Does not work 
     } 
    } 
} 
} 

這個問題以前已經問過很多次了,但經過多次搜索後我還沒有找到解決方案。我錯過了什麼?謝謝

+0

你爲什麼要叫'setImageResource'?改變UI這樣的操作應該在UI線程上運行,因此不需要在另一個線程中運行它。 –

+0

@James:使用線程是對SO的一個建議。浪費了幾個小時後,我找到了解決方案 - 它在模擬器上無法正常工作。在我的實際手機上更改onActivityResult中的imageview src。 [如果有人想知道,我使用了ivPic.setImageResource()而不是我的代碼中的線程]這有點讓我擔心。那麼我如何徹底測試?模擬器的目標是API級別10.這是一個已知問題嗎? –

+0

您的清單中的目標API是什麼? eclipse中你的項目屬性中的目標android sdk是什麼? –

回答

0

確保您在Eclipse中的項目屬性中的目標Android版本和清單中的targetSdk匹配。

  • 右鍵單擊項目>屬性> Android的選項卡,並確保所選擇的Android的目標相匹配的targetSdk在另一個線程