我称这个函数为:
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){
但这永远不会进入代码的“其他”部分。