Android Series: Taking photos with Android built in camera
Today I will describe how to take a photo in android application using the built in camera. The way to do it is to simply start ‘activity for result’ instead of simple ‘activity’ and specify android camera application as our activity.
Then, once a picture is taken with a nice built in camera application, we can get it back in our app for further processing:
Main activity class:
package com.softwarepassion.androcamera;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AndroCamera extends Activity {
private static final int IMAGE_CAPTURE = 0;
private Button startBtn;
private Uri imageUri;
private ImageView imageView;
/** Called when the activity is first created.
* sets the content and gets the references to
* the basic widgets on the screen like
* {@code Button} or {@link ImageView}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.img);
startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startCamera();
}
});
}
public void startCamera() {
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK){
Log.d("ANDRO_CAMERA","Picture taken!!!");
imageView.setImageURI(imageUri);
}
}
}
}
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class AndroCamera extends Activity {
private static final int IMAGE_CAPTURE = 0;
private Button startBtn;
private Uri imageUri;
private ImageView imageView;
/** Called when the activity is first created.
* sets the content and gets the references to
* the basic widgets on the screen like
* {@code Button} or {@link ImageView}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.img);
startBtn = (Button) findViewById(R.id.startBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startCamera();
}
});
}
public void startCamera() {
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK){
Log.d("ANDRO_CAMERA","Picture taken!!!");
imageView.setImageURI(imageUri);
}
}
}
}
and layout xml (main.xml):
<?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"
>
<Button android:text="Start Camera"
android:id="@+id/startBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Start Camera"
android:id="@+id/startBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<ImageView android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageView>
</LinearLayout>
Nice article bro. Is there any way to prevent the image from also being written to the SD card? I want to take the file from the camera and head off to my server with it, yet a duplicate shows on my phone.
Peace,
Scott
hi, really thanks bro
thanks..
this works good but when i set camera image on imageview it rotating from portrait to landscape ..means image is not set proper which i captured from camera…do u have any solution regarding this
hi, am getting exception unsupported operation execution,unknown uri
Hi, thanks for this tutorial, but onActivityResult the application pass directly to RESULT_CANCELED and my intent is NULL, how can I fix this problem please?
Thanks
Hi, can you plz write me a code that would after turning camera on shout photo automaticly and save it and go back to app ? I would be really thankful !
sorry, no time
really good job.. thanks buddy.. 🙂
Hey guys, it’s written “Andorid” on the title 😉 Bye
Very Nice example.its work for me fine.in that i want to store this image into database and retrieve.
Thanks.