Pages

Tuesday, June 11, 2013

Android camera .set wallapaper

Android Camera App Tutorial

             In this tutorial how to take a photo using the Android Phone camera and set the photo as wallpaper.Here we are using a Image View and 2 buttons for two actions.Lets start learning.
STEP BY STEP

1.Create an Android Project

2.Next we need to create UI to display the image and 2 button
  Lets take main.xml from res/layout folder.
  Here initialy am providing a camera image in the image view
The code is shown bellow.<?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:orientation="vertical" >

    <ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.81"
android:src="@drawable/ic_launcher" />

    <Button
android:id="@+id/tak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.24"
android:text= "Take Photo"
android:layout_margin="10dp" />

    <Button
android:id="@+id/set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.24"
android:text= "Set wallpaper"
android:layout_margin="10dp"/>

</LinearLayout>

3.Next we need to create activity file.
CameraAppActivity.java is shown bellow.
package com.androidituts.camera;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
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;
import android.widget.Toast;
public class CameraAppActivity extends Activity
implements View.OnClickListener
{
Button wall,take;
ImageView pic;
Intent i;
int cameraData = 0;
Bitmap bmp;
protected void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);
InputStream is = getResources().openRawResource(R.drawable.camera);
bmp = BitmapFactory.decodeStream(is);
wall = (Button) findViewById(R.id.set);
take = (Button) findViewById(R.id.tak);
pic = (ImageView) findViewById(R.id.image);
wall.setOnClickListener(this);
take.setOnClickListener(this);
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.set:
try
{
getApplicationContext().setWallpaper(bmp);
Toast.makeText(getApplicationContext(), “Wallpaper Set”, Toast.LENGTH_LONG).show();
}
catch (IOException e)
{
e.printStackTrace();
}
break;
case R.id.tak:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,cameraData);
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get(“data”);
pic.setImageBitmap(bmp);
}
}
}
4.Here we need to add uses-permissions for setting wallpaper.
“ <uses-permission android:name=”android.permission.SET_WALLPAPER”/> “
CameraAppManifest.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”
http://schemas.android.com/apk/res/android”
package=”com.androidituts.camera”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk android:minSdkVersion=”10″ />
<uses-permission android:name=”android.permission.SET_WALLPAPER”/>
<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name” >
<activity
android:name=”.CameraAppActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
  <category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
</manifest>
5.Now lets run the android application.
6.Output is shown bellow.
camera


No comments:

Post a Comment