Often, in your Android application, you will want to incorporate photos that the user takes from his camera. Just like audio and video, it is possible and rather fairly easy to include camera functionality in your Android application.
In this post I will show you how to take the user to the default camera application from our application, click a photo and then return that photo to our application. We can then make use of that photo any way we want. So start up an activity and start coding. Also remember that when it comes to testing camera related features, we are going to do this on an actual Android Device and not an emulator for obvious reasons. Complete Source Code is at the bottom.
- For the layout of the activity, have a Button and an ImageView. I would advise you to make the ImageView stretch through the entire width and height of the layout, left after placing the button. We would be placing the photo taken from the camera here.
- Switch over to the Java file and declare both, the Button and the ImageView in there. Remember to declare the ImageView inside the class and not inside the onCreate() method. This is because we will be using it in other methods as well.
- Inside the onClick() method of the Button, write the following
[java]
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
[/java] - Now, we need to get the clicked photo back from the camera to our application. For this we will override the onActivityResult() method of the Android.Activity class. Write the following code just after the onCreate() method.
[java]
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Bitmap bm = (Bitmap)data.getExtras().get("data");
iv.setImageBitmap(bm);
}
[/java] - Save your work and execute it on an Android device.
- When you hit the button and get into the camera application, click a photo and you will be redirected to the original application with the photo you just clicked placed in the ImageView in your activity.
Understanding the Code
Let’s take some time and try to understand the code we just wrote
- The Intent we used while calling the camera application is quite understandable. If you have read my previous posts, you will know what we use an intent for. The argument for the intent however, is a string constant that tells the Android system that we want to use the camera application – android.provider.MediaStore.ACTION_IMAGE_CAPTURE.
- android.provider refers to the content provider of Android. I will write about this in a later post.
- MediaStore is the part of the content provider that we want to refer to.
- ACTION_IMAGE_CAPTURE is the actual string we want.
- Normally, while using Intents, we call the method startActivity(). Here, you can see, we have called startActivityForResult(Intent intent, int requestCode). Since, we need the result of the following Activity back in this activity, so we use the above method.
- requestCode is useful when we have several requests being made from the activity. This helps us to uniquely identify the request.
- Bitmap is used because the data that has been sent back to our activity has a Bitmap object. Hence we use the setImageBitmap() method.
- data.getExtras().get(“data”) : The data passed to our Activity from the Camera is in the form of Extras. The name of the data is data.
- An interesting thing to know here, is that we do not need to add permissions for the use of camera in our Manifest file. Why? Because once we a re switching to the default camera application, we are letting it do all the work. The permissions are granted for the Camera Application. However, if you would, sometime want to build you own camera UI, you will need to access the raw camera surface. In that case you will need to include the android.permission.CAMERA in your manifest file.
COMPLETE SOURCE CODE
[java]
package com.nero.myfirstapp;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.graphics.Bitmap
public class Main extends Activity {
@Override
ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.but);
iv = (ImageView)findViewById(R.id.imageView1);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Bitmap bm = (Bitmap)data.getExtras().get("data");
iv.setImageBitmap(bm);
}
}
[/java]