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.
codes work fine
but when i click cancel button
the force close messabe will appear
do u know why is that 😀
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);
}
}
}
@Liechty.. Thanks for the information.
great, Its work thankz 😀
Thanks a lot…..
It works.Thank u very much 🙂
How can I get the path of teh image(Bitmap)???
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.
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.:(
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
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.
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
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
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
Yasith:- we will check and reply that. I think for that you should enable GPS in the camera settings.
they give me that error
Activity class {com.coderzheaven/com.coderzheaven.OpenCameraDemo} does not exist.
thax
check your package name , it is case sensitive, it may be a typo error.
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.
Hello Pravin , for avoiding outofmemory issue please check these posts.
http://coderzheaven.com/2011/07/how-to-avoid-outofmemory-exception-in-android/
http://coderzheaven.com/2011/07/how-to-avoid-outofmemory-exception-in-android-second-method/
Thank u for the tuto.
I have a probleme when I change device orientation(portrait);Ican’t view the picture:(((
when device orientation changes all views are redrawn.
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…..
How Should i implement it for augmented reality application
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
Search for the same in Coderzheaven.com
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..
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
You can just copy the code and it will work
control camera throw pc or laptop using java android programing plz send all code
Pingback: How to Open camera in ANDROID? « a little technology and hint
“Unfortunately camerahas stopped” plz help me to solve this error.
Please check whether you have added all permissions and also check the Logcat for errors.
Thanks man!
how save the image lacally or in sql
Amrita,
Check this tutorial…
http://www.coderzheaven.com/2012/12/23/store-image-android-sqlite-retrieve-it/