I am trying to get into a simple Android application that uses the camera through Intents. The code is pretty much straight from the documentation for Android here , but it does not work for me.
The camera application opens, as expected, after calling the startActivityForResult () function, but never returns after I took the picture (?!). In particular, it does not return after I take the picture and select the accept icon (checkmark on Galaxy Nexus). But it returns after selecting the cancel icon ("X" on the same phone).
Here's the code (note, I'm working from a fragment, not from Activity):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
final Button btnCamera = (Button) view.findViewById(R.id.cameraid);
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
if (v == btnCamera) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
}
btnCamera.setOnClickListener(handler);
}
and
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(getActivity(), "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
}
else if (resultCode == Activity.RESULT_CANCELED) {
} else {
}
}
What do I need to change to make this work? Thank.