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;

    public void onCreate(Bundle savedInstanceState) {

        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);

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:text="Open Camera"

The strings.xml file

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

And the AndroidManifest.xml file.

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

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

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

Please leave your valuable comments on this post.



  1. ishan June 26, 2011 at 5:11 pm

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

  2. Liechty June 27, 2011 at 6:50 am

    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);

    • James June 27, 2011 at 8:01 pm

      @Liechty.. Thanks for the information.

    • ishan June 30, 2011 at 9:26 pm

      great, Its work thankz 😀

  3. Aditya July 14, 2011 at 3:45 pm

    Thanks a lot…..

  4. idoudi August 2, 2011 at 6:47 pm

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

    • James August 2, 2011 at 8:39 pm

      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.

  5. idoudi August 3, 2011 at 1:50 pm

    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.:(

  6. Naresh Kaushik August 6, 2011 at 6:42 pm

    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

    • James August 6, 2011 at 9:48 pm

      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.

      • Naresh kaushik August 7, 2011 at 9:38 pm

        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

        • James August 8, 2011 at 7:09 am

          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

  7. yasith August 10, 2011 at 10:49 am

    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 .

    • James August 10, 2011 at 9:24 pm

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

  8. lwe September 4, 2011 at 2:20 pm

    they give me that error

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


    • James September 4, 2011 at 10:03 pm

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

  9. pravin December 28, 2011 at 4:00 pm

    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.

  10. idoudi February 13, 2012 at 9:53 pm

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

    • James February 13, 2012 at 9:56 pm

      when device orientation changes all views are redrawn.

  11. nikunj March 7, 2012 at 11:28 am

    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…..

  12. Kishore October 26, 2012 at 11:59 am

    How Should i implement it for augmented reality application

  13. Raghav December 7, 2012 at 6:49 am

    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

    • James December 9, 2012 at 4:52 pm

      Search for the same in Coderzheaven.com

  14. boopathi March 21, 2013 at 6:05 am

    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(“android.intent.extras.CAMERA_FACING_FRONT”, 1);

    please help me..

  15. pradeep goswami September 27, 2013 at 11:20 am

    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

    • James September 28, 2013 at 6:56 am

      You can just copy the code and it will work

  16. pradeep goswami September 27, 2013 at 11:27 am

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

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

  18. Rajesh July 8, 2014 at 5:46 pm

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

    • James July 24, 2014 at 1:53 pm

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

  19. ajeeth November 21, 2017 at 10:40 am

    Thanks man!

  20. amrita February 9, 2018 at 9:12 am

    how save the image lacally or in sql


Leave a Reply

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




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.