2

I'm have a small problem.

objective of code: click an imageview that opens the camera, then after getting a picture change the imageview. the imageview is not a thumbnail of the photo.

the problem: the imageview does not refresh.

what I've tried: Android imageView Switching Problem, Changing ImageView on button click, ImageView onClickListener changing the image source (Can’t use viewSwitcher b/c have more than two possible view combinations), how to change the background or the image source when a clickable imageview in android is clicked?

My code:

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
        }
    }
}
}

This question has been asked many times before, but after much searching I've yet to find a solution. What am I missing? thanks

Community
  • 1
  • 1
Carolyn B.
  • 23
  • 2
  • Why are you calling `setImageResource` on another thread? operations that change the UI such as this one should be run on the UI thread so there is no need to run it in another thread. – James McCracken Oct 05 '12 at 20:08
  • @James: using a thread was a suggestion on SO. After wasting hours, I've found the SOLUTION - it simply does not work on the emulator. Changing the imageview src in the onActivityResult works on my actual phone. [In case anyone is wondering, I used ivPic.setImageResource() and not the thread in my code] This concerns me a little. How do I test thoroughly, then? The emulator targets API level 10. Is this a known issue? – Carolyn B. Oct 05 '12 at 21:06
  • what is your target API in your manifest? what is your target android sdk in your project properties in eclipse? – James McCracken Oct 05 '12 at 22:05
  • How did you ever make that deduction? Brilliant! That's the problem. When I changed the project build target to match (it was lower) the manifest target api level, the image src changed in the emulator. Thank you @James. I read [this](http://stackoverflow.com/questions/8938348/difference-between-build-target-sdk-in-eclipse-and-androidtargetsdkversion-in) but are there any other good sources for project build target vs. targetSdkVersion? – Carolyn B. Oct 08 '12 at 17:06
  • I would like to give James credit for the solution or mark my question as answered, but I don't see how that is done. – Carolyn B. Oct 09 '12 at 14:31
  • I put my comment up as an answer if you want to accept it. – James McCracken Oct 09 '12 at 15:03

1 Answers1

0

Make sure your target Android version in project properties in Eclipse and your targetSdk in your manifest match.

  • Right click project > Properties > Android tab, and make sure the selected Android target matches your targetSdk
James McCracken
  • 15,488
  • 5
  • 54
  • 62