Intent Android camera does not return in case of RESULT_OK

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

                // create a file to save the image
                File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
                imagesFolder.mkdirs();
                File image = new File(imagesFolder, "image_001.jpg");
                Uri uriSavedImage = Uri.fromFile(image);

                // start the image capture Intent
                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) {
            // 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
        }
    }

What do I need to change to make this work? Thank.

+5
1

, , :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>

, , . ! .

+12

All Articles