Question

first post so be nice :)

My problem is that I have a class to open my phone Camera. However, if i press the back button on my phone, the app will crash.

Is there any way to prevent this? maybe just disable the back button?

Any help would be appreciated

This is the code I call to open the camera.

    btnpicture.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityIfNeeded(cameraIntent, CAMERA_REQUEST);
        }
    });

EDIT: This problem also occurs when I have another button to open the Gallery. The app just crashed if the Back button is pressed.

    btnsdpic.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent gallery = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityIfNeeded(gallery, SELECT_IMAGE);
        }
    });

Thanks Guys

Hemm.

Was it helpful?

Solution

Try:

 Intent gallery = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
 startActivityForResult(camera, SELECT_IMAGE);

And:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
       if (resultCode == RESULT_OK) {
        Toast.makeText(this, "Image Caputred", Toast.LENGTH_SHORT).show();
     } else if (resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Image was not Caputred", Toast.LENGTH_SHORT).show();
     } else {
        Toast.makeText(this, "Image was not Caputred", Toast.LENGTH_SHORT).show();
     }
    }    
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top