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.

[pgsubscribe]

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="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:gravity="center_horizontal|center_vertical" >

        <TextView
            android:id="@+id/field"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="No Image" />

        <ImageView
            android:id="@+id/image"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Take Photo" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Set as Wallpaper" />

</LinearLayout>

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) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		image = (ImageView) findViewById(R.id.image);
		field = (TextView) findViewById(R.id.field);
		imgButton = (Button) findViewById(R.id.button);
		wallpapaerBtn = (Button) findViewById(R.id.button1);
		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
    startCameraActivity();
	}
}

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(
			android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
             //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");
		break;
        //When user captures image, onPhotoTaken an user-defined method
        //to assign the capture image to ImageView
            case -1:
		onPhotoTaken();
		break;
	}
}

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
        image.setImageBitmap(bitmap);
        //Make the TextView invisible
	field.setVisibility(View.GONE);
	}

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
					.getDrawable();
                //Get bitmap from drawable object
		Bitmap bitmap = drawable.getBitmap();
                //Create WallpaperManager object
		WallpaperManager wallpaperManager = WallpaperManager
				.getInstance(ClickPickSetActivity.this);
                //Set wallpaper with the extracted bitmap
		wallpaperManager.setBitmap(bitmap);
                //Have user informed with a simple toast message
		Toast.makeText(ClickPickSetActivity.this,
		   "Wallpaper set : )", Toast.LENGTH_SHORT).show();
           //If image is not captured yet
	   } else {
                 //Ask user to capture image
		Toast.makeText(
		ClickPickSetActivity.this,
	          "You must capture photo before
                       you try to set wallpaper!",
                          Toast.LENGTH_SHORT).show();
           }
           } catch (Exception e) {
		e.printStackTrace();
           }
	}
}

[pglinkadssmall1]

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()");
	if(savedInstanceState.getBoolean(ClickPickSetActivity.PHOTO_TAKEN)){
		onPhotoTaken();
	}
}

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" />

Demo

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.

 

[pglinkadssmall]

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

Suggested posts for further reading


[pgfeedback]
[pgwriteforus]

Related Articles

Author: Android Guru

Share This Post On
468 ad
  • Great tutorial. I’m looking for tutorial regarding store and retrieve image using sqlite with pick from photo gallery function, capture new photo by camera app and crop function like WhatsApp change profile photo function. Can you create this tutorial? Thanks in advance.

  • Krish

    Great tutorial…i was just looking for that…

  • Ash

    Great tutorial…actually i was loking some sorts of examples like this..Can u tell me how it is possible to understand the communication of these devices like camera,flash light etc., with android??I juz want to know how we can control these with a “delay”..Can you help me plzz

  • Anuj

    Nice Post!
    What if I want to crop Captured Image while setting as wallpaper?

  • Androidian

    Finally I’ve found something which helped me. Thanks!

  • jav

    Very good one, very useful for me. Thx!

  • manikanta

    good tutorial,i have a small doubt in this topic.suppose if i take a picture then it should be stored in server ,how the pic will be stored in server?

    • You need to post the captured image to server script(php) via http. Server script will place the uploaded image under the folder you specified in script. Will write down a post on image upload from Android mobile soon. Subscribe to the blog so that you will get an email once the new post is created.
      Thanks.

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

Shares