意图拍照,如果我按回,应用程序崩溃

时间:2013-02-08 12:50:40

标签: android android-intent camera uri image

我称之为此功能:

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

}

接下来的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();
        }


    }

}

这样可行,但是如果我打算拍照,然后按后退按钮,如果我之前已经拍过照片,它会加载那张照片,如果没有,它会崩溃,因为按回来,它不会拍照。我能做些什么来逃避这种情况?

我试过测试:

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

但是这永远不会进入代码的“else”部分。

1 个答案:

答案 0 :(得分:1)

使用这些条件:

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

}