Click-Pick-Set – Simple Camera Activity

This post is all about how to use camera activity in your Android application. Let me give a quick glance of what we are going to develop today. We will have one image view and two buttons (one for taking picture and other for setting wallpaper) in our application. When you click Take photo button, will take you to default camera present in your device and capture the image. You can set the clicked image as your wallpaper just by clicking Set as Wallpaper button. Does it sound interesting?.. I hope YES.. 🙂

Let’s begin…

Quick Links

Code Listings

Layout creation:

Create a simple layout which has a TextView and ImageView placed in relative layout where the controls are placed in relative position to the parent window.


TextView – Displays the text ‘No image’ when there is no image captured yet.

ImageView – Displays the captured image

Create two Buttons – One to take picture and other to set captured image as wallpaper

Final Layout XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:gravity="center_horizontal|center_vertical" >

            android:text="No Image" />

            android:layout_height="wrap_content" />

        android:text="Take Photo" />

        android:text="Set as Wallpaper" />


Application layout:

We are done with design, yet to start code the logic. I would recommend you to download the source code right away to proceed with the listings given below. Create following objects right under the Activity class to refer the controls:

protected Button imgButton;//To refer image button
protected ImageView image;//To refer image view
protected TextView field;//To refer textview	
protected Button wallpapaerBtn;//To refer wallpaper button

Create following instance variables to support the logic under the objects we created:

private boolean imgCapFlag = false;
protected boolean taken;
protected static final String PHOTO_TAKEN = "photo_taken";
protected String path;

Assign the controls to the respective objects we created above. Set the OnClickListener for Take Photo and Set as Wallpaper buttons. Set the path where the capture image will be stored. You need to explicitly create a folder named ‘images’ under the root of SD card.

public void onCreate(Bundle savedInstanceState) {
		image = (ImageView) findViewById(;
		field = (TextView) findViewById(;
		imgButton = (Button) findViewById(;
		wallpapaerBtn = (Button) findViewById(;
		imgButton.setOnClickListener(new ButtonClickHandler());
		wallpapaerBtn.setOnClickListener(new ButtonClickHandler1());
		path = Environment.getExternalStorageDirectory()
                                + "/images/make_machine_example.jpg";

Create ButtonHandler class for Take photo button:

public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
    //Below log message will be logged when you click Take photo button
    Log.i("AndroidProgrammerGuru", "ButtonClickHandler.onClick()");
    //Call startCameraActivity, an user defined method, going to be created

Create startCameraActivity method which initiates the camera activity:

protected void startCameraActivity() {
             //Log message
	     Log.i("AndroidProgrammerGuru", "startCameraActivity()");
	     //Create new file with name mentioned in the path variable
             File file = new File(path);
             //Creates a Uri from a file
	     Uri outputFileUri = Uri.fromFile(file);
             //Standard Intent action that can be sent to have the 
             //camera application capture an image and return it.
             //You will be redirected to camera at this line
	     Intent intent = new Intent(
             //Add the captured image in the path
	     intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
             //Start result method - Method handles the output 
             //of the camera activity
	     startActivityForResult(intent, 0);

Create onActivityResult method to handle the result of camera activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	//Log message
        Log.i("AndroidProgrammerGuru", "resultCode: " + resultCode);
	switch (resultCode) {
        //When user doesn't capture image, resultcode returns 0
	    case 0:
		Log.i("AndroidProgrammerGuru", "User cancelled");
        //When user captures image, onPhotoTaken an user-defined method
        //to assign the capture image to ImageView
            case -1:

onPhotoTaken – User defined method to set captured image in the ImageView

protected void onPhotoTaken() {
        //Log message
	Log.i("AndroidProgrammerGuru", "onPhotoTaken");
	//Flag used by Activity life cycle methods
        taken = true;
	//Flag used to check whether image captured or not
        imgCapFlag = true;
	//BitmapFactory- Create an object
        BitmapFactory.Options options = new BitmapFactory.Options();
	//Set image size
        options.inSampleSize = 4;
	//Read bitmap from the path where captured image is stored
        Bitmap bitmap = BitmapFactory.decodeFile(path, options);
	//Set ImageView with the bitmap read in the prev line
        //Make the TextView invisible

Create ButtonHandler class for Set as Wallpaper button:

public class ButtonClickHandler1 implements View.OnClickListener {
  public void onClick(View view) {
	//Log message
        Log.i("AndroidProgrammerGuru", "ButtonClickHandler.onClick()");
	try {
        //Check if image is captured which sets
        //imgCapFlag in onPhotoTaken method
           if (imgCapFlag) {	
                //Create drawable object to convert ImageView to bitmap
		BitmapDrawable drawable = (BitmapDrawable) image
                //Get bitmap from drawable object
		Bitmap bitmap = drawable.getBitmap();
                //Create WallpaperManager object
		WallpaperManager wallpaperManager = WallpaperManager
                //Set wallpaper with the extracted bitmap
                //Have user informed with a simple toast message
		   "Wallpaper set : )", Toast.LENGTH_SHORT).show();
           //If image is not captured yet
	   } else {
                 //Ask user to capture image
	          "You must capture photo before
                       you try to set wallpaper!",
           } catch (Exception e) {


When you put this application in sleep mode which means you press ‘Home’ button, below method will be called which set the state with key ‘photo_taken’ to true.

protected void onSaveInstanceState(Bundle outState) {
	outState.putBoolean(ClickPickSetActivity.PHOTO_TAKEN, taken);

When you open this application(after you pressed ‘Home’ button), application will be resumed which calls the below method:

onRestoreInstanceState method calls the onPhotoTaken method
protected void onRestoreInstanceState(Bundle savedInstanceState) {
	Log.i("AndroidProgrammerGuru", "onRestoreInstanceState()");

You must add following permission in AndroidManifest.xml (Just above application tag) which gives permission to this application to change wallpaper of a device.

<uses-permission android:name="android.permission.SET_WALLPAPER" />


Let us check whether the code we typed is working..
Make sure you created folder named ‘images’ right under the root of the SD card.
We can only test this application in real device, so connect your device to the system through USB cable.
Right click on the project >> Run as >> Android application
Here is the final output:

Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.



Did you enjoy the post??…. :)..
Share your thoughts below!!

Suggested posts for further reading


Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest