Android 4.4 tested in Eclipse.
I've followed the lines as developers said. So in my fragment I put:
public void tomarfoto() {
// Check Camera
if (MainActivity.if_cam) {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
this.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
} else {
//Toast.makeText(getActivity(), "Camera not supported", Toast.LENGTH_LONG).show();
}
}
After that, I set this:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(getActivity(), "Image saved to:\n" +data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == Activity.RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
The problem is that data intent is null and provokes this:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=1888, result=-1, data=null} to activity {com.myapp.MainActivity}: java.lang.NullPointerException
I don't understand so much but apparently the onActivityResult is disconnected from the fragment who has called it so, it can't receive data results. How can I solve this?. The launchMode="singleTask" or singleInstance is not my problem, I didn't settled this.
The pictures are saved properly in the directory held by fileUri. Help.