Android App Development – Camera and Wallpaper

Android App Development – Camera and Wallpaper

Whats up Guys? Welcome to the 5th android app development tutorial on Androidnova. In this tutorial we are going to start building an application that basically can use a phone’s camera and then we can set that picture which we captured as phone’s background or Wallpaper. So, its gonna be a pretty cool application for most of you guys here.

First of all as always, set up your android project in eclipse and name the app and activity as camera (for setting up project, click here). Once you are done, open the xml layout (main.xml) and  add two buttons, capture button and set wallpaper button and one Imageview as shown in below figure

Camera and Wallpaper android dev

Now open your camera java class inside src folder(path: src/com/camera/camera.java) and copy the following code as shown below

Code:

public class Camera extends Activity  implements View.OnClickListener{
/** Called when the activity is first created. */

ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialize();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void initialize() {
// TODO Auto-generated method stub
iv = (ImageView) findViewById(R.id.)
ib = (ImageButton) findViewById (R.id.ibTakePic);
b = (Button) findViewById(R.id.bSetWall);
b.setOnClickListener(this);
ib.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
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 extras = data.getExtras();
bmp = (Bitmap) extras.get(“data”);
iv.setImageBitmap(bmp);
}
}
}
}

Don’t Forget to import the necessary packages if you are copying the code. Just press ctr+ shift + o once u have copied the above code..

Hope you guys have fun building this app. I’ll come up with a new tutorial very soon. Till then, later!

Note: This app may not work in emulator, you need to connect your physical android device via USB and run the application.

Below is the screen shot of application. Image Button is used to capture image and after capturing image, you can set wallpaper using “set wallpaper ” button.

Android Camera and Wallpaper development