Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android camera Intent doesn't return in RESULT_OK case

I'm trying to stand up a simple Android app that uses a camera via Intents. The code is pretty much straight from the Android documentation here, but it isn't working for me.

The camera app opens as expected after the call to startActivityForResult(), but it never returns after I've taken a picture (?!). Specifically, it doesn't return after I take a photo and pick the accept icon (check mark on Galaxy Nexus). But it does return after I pick the the cancel icon ('X' on the same phone).

Here's the code (note, I'm working from a Fragment, not an 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? Thanks.

like image 400
gcl1 Avatar asked Sep 18 '12 20:09

gcl1


1 Answers

Oops, it worked when I added the manifest lines:

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

I mistakenly thought these lines were not necessary if your app relies on an external camera app. But I was wrong about that! Thanks.

like image 95
gcl1 Avatar answered Oct 15 '22 23:10

gcl1