开发者

Camera intent staying open on android

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.onActivity开发者_如何学GoResult(requestCode, resultCode, data);
    }
}     


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 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();
    }

}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜