rosu alin
rosu alin

Reputation: 5830

Intent to take picture, if i press back, the application crashes

I call this function:

  private void TakePhoto() {
    LogService.log(TAG, "inTakePicture");
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/BlueSkyBio/media/", "test.jpg");

    outputFileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, TAKE_PICTURE);

}

Which takes me on the next onActivityResult:

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PICTURE) {
        if(outputFileUri != null){
            LogService.log("MainFragment", outputFileUri.toString());
            String path = outputFileUri.toString();
            selectedVideoPath = path.substring(7);
            LogService.log("in take pic", "selectedImagePath: " + selectedVideoPath);
            Intent paintActivity = new Intent(getActivity(), PaintActivity.class);
            paintActivity.putExtra("selectedImagePath", selectedVideoPath);
            paintActivity.putExtra("isVideo", false);
            startActivity(paintActivity);
            ((FragmentActivity) getActivity()).finish();
        } else{
            //                Toast.makeText(getActivity(), "No picture taken", Toast.LENGTH_SHORT).show();
            Intent main = new Intent(getActivity(), FragmentActivity.class);
            startActivity(main);
            ((FragmentActivity) getActivity()).finish();
        }


    }

}

This works ok, but if i call the intent to take a picture, and then press the back button, if i already took a picture before, it will load that picture, if not, it will crash, because by pressing back, it will not take a picture. What can i do to escape this situation?

I have tried to test:

  if(data != null) // instead of:  if(outputFileUri != null){

But this will never enter the "else" part of the code.

Upvotes: 0

Views: 1652

Answers (1)

SKK
SKK

Reputation: 5271

use these conditions:

private static final int CAMERA_PIC_REQUEST = 1337;  

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode==CAMERA_PIC_REQUEST && resultCode == RESULT_OK){

         log.d("something","something");          
    }
    else if (resultCode == Activity.RESULT_CANCELED) 
    {
        log.d("something","something");
    }

}

Upvotes: 1

Related Questions