36

If you want to use the built-in camera activity which uses the native Android camera, simply do the following.

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
        this.startActivityForResult(camera, PICTURE_RESULT);

You want to get the images back from the nifty camera you displayed -- but how?

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
slrobert
  • 599
  • 1
  • 5
  • 6
  • 1
    Well, my question is: whats PICTURE_RESULT? – Dänu Nov 11 '10 at 10:31
  • 2
    PICTURE_RESULT is a self-defined constant. The value that you pass to startActivityResult() as the requestCode will be the same value that is passed to onActivityResult() when your Intent is done so that you know what Intent is returning the result. – Joel McBeth Nov 17 '10 at 15:20

1 Answers1

23

If you want to get the image back in its full glory, pass in a uri to the Intent within the EXTRA_OUTPUT extra. If you're fine with a smallish bitmap (and you should be), just call the intent as normal.

Now you have two options, deal with the uri of the image that is returned in the EXTRA_OUTPUT extra, or do the following in your onActivityResult method:

if (requestCode == PICTURE_RESULT) //
             if (resultCode == Activity.RESULT_OK) {
                // Display image received on the view
                 Bundle b = data.getExtras(); // Kept as a Bundle to check for other things in my actual code
                 Bitmap pic = (Bitmap) b.get("data");

                 if (pic != null) { // Display your image in an ImageView in your layout (if you want to test it)
                     pictureHolder = (ImageView) this.findViewById(R.id.IMAGE);
                     pictureHolder.setImageBitmap(pic);
                     pictureHolder.invalidate();
                 }
             }
             else if (resultCode == Activity.RESULT_CANCELED) {...}
    }

And there you go!

slrobert
  • 599
  • 1
  • 5
  • 6
  • 2
    thanks slrobert this helped me a lot. So many other tutorials explained implementing the camera functionality from scratch rather than simply raising an `ActivityForResult` and having the default camera activity handle things! – wired00 Jul 10 '11 at 00:17