How to Open camera in ANDROID?

By | June 25, 2011

Hello everyone..
In today’s tutorial I will show you how to use camera in ANDROID in your program.
In this tutorial we will be having a button which will open the camera and after taking the photo it will show it in an imageView.

Note: This program will work only in the real device not in the emulator. So make sure to test it in the device itself. Also make sure to add the permission while using camera as shown below in your AndroidManifest file.

 <uses-feature android:name="android.hardware.camera"></uses-feature>  

Here is the main java file code

package com.coderzheaven;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class OpenCameraDemo extends Activity {

	private static final int CAMERA_PIC_REQUEST = 2500;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b = (Button)findViewById(R.id.Button01);
        b.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				 Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				 startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
			}
		});
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
        	  Bitmap image = (Bitmap) data.getExtras().get("data");
              ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
              imageview.setImageBitmap(image);
        }
    }
}

Now the main.xml file which contains the button and the imageview.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<ImageView
	android:id="@+id/ImageView01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</ImageView>
<Button
	android:text="Open Camera"
	android:id="@+id/Button01"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content">
</Button>
</LinearLayout>

The strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, OpenCameraDemo!</string>
    <string name="app_name">OpenCamera</string>
</resources>

And the AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">

      <uses-feature android:name="android.hardware.camera"></uses-feature>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenCameraDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Please leave your valuable comments on this post.

31 thoughts on “How to Open camera in ANDROID?

  1. ishan

    codes work fine
    but when i click cancel button
    the force close messabe will appear
    do u know why is that 😀

    Reply
  2. Liechty

    In onActivityResult() you need to check resultCode == RESULT_OK before trying to read the extras.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK) {
    Bitmap image = (Bitmap) data.getExtras().get(“data”);
    ImageView imageview = (ImageView) findViewById(R.id.ImageView01);
    imageview.setImageBitmap(image);
    }
    }
    }

    Reply
  3. idoudi

    It works.Thank u very much 🙂
    How can I get the path of teh image(Bitmap)???

    Reply
    1. James Post author

      idoudi :- check this post to get the image path. You will get it from the getPath() funtion by passing the image URI from the onActivityResult() function.

      Reply
  4. idoudi

    It works just in case of an image of the gallery.. But it does not work in case of an image taken from the camera.:(

    Reply
  5. Naresh Kaushik

    This code open the camera and let me capture just one image give option of saving and discarding the captured image and send me back to application. I want to capture more images without opening camera again and again, as like the phone camera works

    Reply
    1. James Post author

      Call the intent again to open the camera on “onActivityResult” function. Make sure you will not end up in a loop. Give a button in Menu or somewhere else to stop it.

      Reply
      1. Naresh kaushik

        Hi james,
        thanks for response.what if i open exactly the same camra app of phone as the user opens it from outside.i can open it if i get pkg name and launcher activity of camra installed in phone.so how to get the same

        Reply
        1. James Post author

          When you are opening the camera you are opening the same camera app that the user opens from the outside. Check this post to see how to get all installed applications with their package names

          Reply
  6. yasith

    Hi,
    This code is work correctly thanks for this. but want to know how to get image details from captured image. like latitude and longitude and title. pls give some example .
    thanks

    Reply
    1. James Post author

      Yasith:- we will check and reply that. I think for that you should enable GPS in the camera settings.

      Reply
  7. lwe

    they give me that error

    Activity class {com.coderzheaven/com.coderzheaven.OpenCameraDemo} does not exist.

    thax

    Reply
    1. James Post author

      check your package name , it is case sensitive, it may be a typo error.

      Reply
  8. pravin

    Hi, i want to Reduce the size of bitmap. Becoz in my case my app get force close if 3 to 4 time i take picture and set it on ImageView due to this i got… Bitmap Size Exceeds . VM related error….please tell me how can i avoid this issue.
    Thanks adv.
    pravin.

    Reply
  9. idoudi

    Thank u for the tuto.
    I have a probleme when I change device orientation(portrait);Ican’t view the picture:(((

    Reply
  10. nikunj

    Sir, You code is not working when i click the button the message show in my emulator the
    unfortunately camera is stooped.
    please help me…..

    Reply
  11. Raghav

    How Can i chooose photo from Gallery of my my phone if i dont wanto clikc the picture and want to save the existing one
    and set it in the ImageView Defined in your Application

    Reply
  12. boopathi

    i need to open front facing camera..i am using this code but its not working …

    Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);
    intent.putExtra(“android.intent.extras.CAMERA_FACING_FRONT”, 1);
    startActivityForResult(intent,0);

    please help me..

    Reply
  13. pradeep goswami

    sir,
    I am working on”android camera controlling throw laptop “but recourses are not available so my programming background java android so sir plz help me and send all recourses or all coding or its realited article plz sir help me

    Reply
  14. pradeep goswami

    control camera throw pc or laptop using java android programing plz send all code

    Reply
  15. Pingback: How to Open camera in ANDROID? « a little technology and hint

  16. Rajesh

    “Unfortunately camerahas stopped” plz help me to solve this error.

    Reply
    1. James Post author

      Please check whether you have added all permissions and also check the Logcat for errors.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *