Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the camera activity in Android

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?

like image 435
slrobert Avatar asked Feb 22 '10 23:02

slrobert


People also ask

What is the permission for using the camera in Android?

Camera Permission - Your application must request permission to use a device camera. Note: If you are using the camera by invoking an existing camera app, your application does not need to request this permission. For a list of camera features, see the manifest Features Reference.

How do I view a captured image on Android?

Step 2: Open “activity_main. xml” file and add following widgets in a Relative Layout: A Button to open the Camera. An ImageView to display the captured image.

How do I know if my Android camera is open?

You can check it using method Camera. open(cameraId) . Creates a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.


1 Answers

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!

like image 63
slrobert Avatar answered Oct 04 '22 00:10

slrobert