Pr.Pg Next Pg

Camera Application tutorials

  • In this tutorial we are going to make a camera Application, which will click an image and set it as the wallpaper.

  • As usual we create a new layout with two buttons and an ImageView.

  • You can set any image as the resource.

  • We have chosen the default image as the resource.

  • Then align and set up the layout the way you like it using the attributes discussed before.

  • After doing all those things, we create a new class named Camera.java.

  • In this class we set up the basic variables and all add this class name to our Menu List, since we will be running our application from there.

 

 

 

package com.example.helloandroid;

 

import java.io.IOException;

import java.io.InputStream;

 

import android.app.Activity;

import android.app.WallpaperManager;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

 

public class Camera extends Activity implements View.OnClickListener {

Button wall;

Button click;

ImageView iv;

Intent in;

int camData;

Bitmap bm;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.camera);

initializeVars();

InputStream is = getResources().openRawResource(R.drawable.ic_launcher);

bm = BitmapFactory.decodeStream(is);

}

private void initializeVars() {

// TODO Auto-generated method stub

click = (Button)findViewById(R.id.button1);

wall = (Button)findViewById(R.id.button2);

iv = (ImageView)findViewById(R.id.imageView1);

wall.setOnClickListener(this);

click.setOnClickListener(this);

}

 

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch(v.getId()){

case R.id.button1:

in = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(in,camData);

break;

case R.id.button2:

WallpaperManager w = WallpaperManager.getInstance(this);

try {

w.setBitmap(bm);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

break;

}

}

 

 

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

if(resultCode==RESULT_OK){

Bundle ex = data.getExtras();

bm = (Bitmap)ex.get("data");

iv.setImageBitmap(bm);

}

}

 

}

 

 

  • We then set up the onClick() for both the Buttons using a switch case, so that the write code is implemented depending upon which Button was clicked.

  • In this code for the Click Image button which clicks an image we make a new Intent to start the activity with the action name android.provider.MediaStore.ACTION_IMAGE_CAPTURE.

  • This activity is already there in the AndroidManifest but the user cant see it and the above action name is already defined.

  • We then start the activity by calling the method startActivityForResult().

  • public void startActivityForResult (Intent intent, int requestCode, Bundle options)

  • Added in API level 16

  • Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode. Using a negative requestCode is the same as calling startActivity(Intent) (the activity is not launched as a sub-activity).

  • We pass in the parameters the request Code and the intent which will start the activity.

  • The requestCode if greater or equal to zero will be returned in onActivityResult() method when the activity exits.

  • For the second button which is used to set the wallpaper, we need to get the result back when the image capture activity is finished.

  • This is provided by the onActivityResult() method.

  • It send the resultCode, the original requestCode sent by the activity and the Intent which can return the result data to the caller.

  • The resultCode will be RESULT_OK if the operation was successful, else It will be RESULT_CANCELED.

  • We then use the getExtras() method of the Intent class to get the data from the Intent and store in a Bundle.

  • Bundle is generally used for passing data between various Activities of android.

  • It depends on you what type of values you want to pass but bundle can hold all types of values and pass to the new activity.

  • We then extract a Bitmap from the Bundle using the get() method.

  • The get() method takes in a key.

  • This Bitmap is then set into our ImageView.

  • Now we go back to our the onClick() for the Set Wallpaper button and there set the wallpaper by using an instance (object ) of WallpaperManager and setting the wallpaper of the phone as the image which we have clicked.

  • We also have to add permission to allow our appliaction to set the wallpaper of the device.

  • We do that by clicking on AndroidManifest.xml.

  • The clicking on the permissions tab at the bottom of the editor.

  • Then Add > Uses Permission .

  • Then set the name as android.permission.SET_WALLPAPER .

  • You can also confirm it by seeing in the XML code of the file.

  • Before the variable initialiazation, we have used InputStream to set image as default in case the user clicks on Set Wallpaper without clicking the picture. In our case the image ic_launcher in the drawable folder will be se t as wallpaper if the scenario arises.

  • The BitmapFactory class has decodeStream() method which decodes the stream into a Bitmap.

  • Also we have to set the orientation of our activity as portrait using the andoid:screenOrientation attribute of the activity.

  • This has to be done because our application would force close in case if it rotated to keep in horizontal mode.

  • The Application wont work perfectly fine on the emulator.

  • It is better to try out on the phone.

  • Below are the screenshots of the application.

 

 

Pr.Pg border                                              Next Pg