To use the inbuilt camera application in your android phone you need a few things
First:
- The device has to have a camera
- Your App must have permission to take pictures
- Your App must have permission to save the picture (if you
intend to)
To take care of the above you will need to add the below code in your manifest file for permission to use camera for picture taking and if the main feature of your app required the device to have a camera
<uses-feature android:name="android.hardware.camera" android:required="true" />
You will also need to add in your manifest file permission to write / save the image if needed this permission also allows your app to have read permission (This example no need)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Finally remember you are asking Android to do work for you hence it will return a result to you which is the image for this will need to use startActivityForResult and pass the code or think of it as an Identifier ID that both your App and Android will use say 123 it can be any integer so long its not used in the activity for any other purpose than for this purpose taking a picture:
Member variable
static final int REQUEST_TAKE_PHOTO = 123;
private void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
Add this to a button or image button so a click will call takePicture method
Final Step is that remember there will be that image or result data that Android will return you will need to accept and as per above Identifier code REQUEST_TAKE_PHOTO = 123 / requestCode. There is a method that you will need to override
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
// Sample data cast to thumbnail
Bitmap imageBitmap = (Bitmap) extras.get("data");
// Do whatever you want with the thumbnail such as setting it to image view
mImageView.setImageBitmap(imageBitmap);
}
}
Source: Android Take Photos