3

For my app I am making, I have the camera intent run when an activity is created. The problem is that when I click ok for the pic I captured, it just reopens the camera again to take a picture. Here is the code: taken is set to false from another activity. I can confirm that taken is false when this activity starts.

public class QuickEditActivity extends Activity {

    public static boolean taken;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quickedit);

        if(!QuickEditActivity.taken) {
            RunCam();
        }

    }

    private void RunCam() {
        QuickEditActivity.taken = true;
        Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(camIntent,1);

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data != null && data.getExtras() != null) {
            Bitmap bitmap = (Bitmap)data.getExtras().get("data");

            if(bitmap != null) {
            ImageView imgView = (ImageView)findViewById(R.id.CamView);

            imgView.setImageBitmap(bitmap);

            }
            else
            {

            this.finish();
            }

        }
        else {
            this.finish();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}     
Zack Marrapese
  • 12,072
  • 9
  • 51
  • 69
Cody
  • 29
  • 2
  • Wouldn't taken need to be true, indicating you handled it? Or are you talking about something else? Also post your other code, you talk about another activity, what is it doing? – Jack Sep 02 '11 at 15:56

2 Answers2

4

My psychic powers tell me you have a Nexus phone. Furthermore my psychic powers tell me that when you start the Camera Capture Activity via camIntent, your process is getting killed and then restarted when the Camera Capture Activity completes. Thus your static variable taken is being reset to false causing your onCreate method to always think this is the first time it's being launched, thus putting you into an infinite Camera Capture loop.

You can verify this by adding the following statement to your onCreate method:

Log.d("QuickEditActivity", "Value of taken = " + (taken ? "true" : "false"));

You can fix this problem by overriding onSaveInstanceState():

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean("taken", true);
}

Then check for this value in the Bundle passed to your onCreate(Bundle) method.

Be sure to let me know how my psychic powers did.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • Your powers were close. I have a Droid which I hear has similar problems as the Nexus. I found that it was always set to false, so that gets me somewhere. I even put the variable in a different class to make it more global and it still reset to false for some reason. I implemented the onSaveInstanceState() and it crashed and I can't figure out why. It crashes as I try to use savedInstanceState.getboolean(). I might have to embed a camera manully. Do you have any other suggestions psychic one? – Cody Sep 02 '11 at 21:50
  • What is the stack when it crashes? – i_am_jorf Sep 02 '11 at 22:23
  • I solved it but I will have to post the answer when my time runs up – Cody Sep 02 '11 at 22:25
  • Hey, Cody, how has you solved it? I have the same problem, and no solution so far... Also my psychic powers are not helping me... – Tom Burger Aug 09 '12 at 22:31
  • His solution is posted as a separate answer. – i_am_jorf Aug 09 '12 at 23:06
-1

I fixed it by doing it a different way, here: this went in the class that started QuickEditActivity

    private void startQuickEditActivity() {

    RunCam();

}


private void RunCam() {
    Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camIntent,1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Intent i = new Intent(this, QuickEditActivity.class);
    i.putExtras(data);
    startActivity(i);

    super.onActivityResult(requestCode, resultCode, data);
}

And here is the code in the QuickEdit

public class QuickEditActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quickedit);

    Intent data = this.getIntent();

    if(data != null && data.getExtras() != null) {
        Bitmap bitmap = (Bitmap)data.getExtras().get("data");


    if(bitmap != null) {
        ImageView imgView = (ImageView)findViewById(R.id.CamView);

        imgView.setImageBitmap(bitmap);



    }
    else
    {

        this.finish();
    }

    }
    else {
        this.finish();
    }

}

}

Cody
  • 29
  • 2