Download Code

Android ImageView Example – Camera Wallpaper

In android imageview example you will learn about Image View,Camera and how to combine these two things to build an Android App(Wallpaper App).

So i’ll give you some information about Image View and Camera first and then you will use these component within android imageview example application.

ImageView

Image View displays an arbitrary image or icon.Image View is basically used to display captured image from camera in an application like Retrica.

Camera

The android framework provides different support of cameras and camera according to the devices.Camera is basically used to captured image and video.You can read further detail about Camera from Android Developer Official Website.

So let’s start now create your android imageview example application project.

imageview,android imageview,android imageview example,imageview example,Camera Wallpaper

now next you will design graphical layout for that wallpaper application.You should place one Image View and Two Buttons.You must use Linear Layout to keep them in straight vertical position.Your final android imageview example xml code  will look this.

imageview,android imageview,android imageview example,imageview example,Camera Wallpaper

Graphical Layout like this.

imageview,android imageview,android imageview example,imageview example,Camera Wallpaper

Go to your MainActivity.java class.First Time your java class will look simple with one method i.e. onCreate().

public class MainActivity extends Activity{
             @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        }
}

Now create variables for Image View and Buttons like this.

public class MainActivity extends Activity{ 
        ImageView pic; // image view variable to set captured image
	Button camera,setwall;  // button variables to open camera and set wallpaper
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        }
}

Next create public method to get all id’s of UI Component and call it in onCreate() method.Then your code will look like this.

public class MainActivity extends Activity { 
        ImageView pic; // image view variable to set captured image
	Button camera,setwall;  // button variables to open camera and set wallpaper
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        }
         // public method to get all id's of UI Components
        public void initialize(){
    	 pic = (ImageView) findViewById(R.id.imageView1);
    	 camera = (Button) findViewById(R.id.button2);
    	 setwall  =(Button) findViewById(R.id.button1);
    	
       }
}

now implement click event on camera and set wallpaper button like this.

public class MainActivity extends Activity implements OnClickListener{ 
        ImageView pic; // image view variable to set captured image
	Button camera,setwall;  // button variables to open camera and set wallpaper
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        }
         // public method to get all id's of UI Components
        public void initialize(){
    	 pic = (ImageView) findViewById(R.id.imageView1);
    	 camera = (Button) findViewById(R.id.button2);
    	 setwall  =(Button) findViewById(R.id.button1);
    	 camera.setOnClickListener(this);   // click listener
    	 setwall.setOnClickListener(this);  // click listener
       }
        @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
}

furthermore you will deal with onClick method.You will create switch and cases for button’s like this.

public class MainActivity extends Activity implements OnClickListener{ 
        ImageView pic; // image view variable to set captured image
	Button camera,setwall;  // button variables to open camera and set wallpaper
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        }
         // public method to get all id's of UI Components
        public void initialize(){
    	 pic = (ImageView) findViewById(R.id.imageView1);
    	 camera = (Button) findViewById(R.id.button2);
    	 setwall  =(Button) findViewById(R.id.button1);
    	 camera.setOnClickListener(this);   // click listener
    	 setwall.setOnClickListener(this);  // click listener
       }
       @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
                // getId method is used to get reference id of UI element.
		switch(v.getId()){
                // set wallpaper button case
		case R.id.button1:
			
			break;
                // open camera button case
		case R.id.button2:
			
			break;
		}
	}
}

When you click open camera button it will start an activity for result means open camera using Intent with Media Store and it’s image capturing action looks like this.

public class MainActivity extends Activity implements OnClickListener{ 
        ImageView pic; // image view variable to set captured image
	Button camera,setwall;  // button variables to open camera and set wallpaper
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        }
         // public method to get all id's of UI Components
        public void initialize(){
    	 pic = (ImageView) findViewById(R.id.imageView1);
    	 camera = (Button) findViewById(R.id.button2);
    	 setwall  =(Button) findViewById(R.id.button1);
    	 camera.setOnClickListener(this);   // click listener
    	 setwall.setOnClickListener(this);  // click listener
       }
       @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
                // getId method is used to get reference id of UI element.
		switch(v.getId()){
                // set wallpaper button case
		case R.id.button1:
			
			break;
                // open camera button case
		case R.id.button2:
				// open camera to capture image and return
			cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
			// starting activity and accept return data second parameter will collect image which is our data
			startActivityForResult(cameraIntent, 0);  
			break;
		}
	}
}

so when camera started and captured an image now you need a method to collect that image which you will call generally as data.For that purpose onActivityResult() method is used.In this method we will use bundle to get image using getExtra() method and then store it in Bitmap variable.Then at last set that Bitmap image to Image View which you design in xml.Now your code will look like this.

public class MainActivity extends Activity implements OnClickListener{ 
        ImageView pic; // image view variable to set captured image
	Button camera,setwall;  // button variables to open camera and set wallpaper
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        }
         // public method to get all id's of UI Components
        public void initialize(){
    	 pic = (ImageView) findViewById(R.id.imageView1);
    	 camera = (Button) findViewById(R.id.button2);
    	 setwall  =(Button) findViewById(R.id.button1);
    	 camera.setOnClickListener(this);   // click listener
    	 setwall.setOnClickListener(this);  // click listener
       }
       @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
                // getId method is used to get reference id of UI element.
		switch(v.getId()){
                // set wallpaper button case
		case R.id.button1:
			
			break;
                // open camera button case
		case R.id.button2:
				// open camera to capture image and return
			cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
			// starting activity and accept return data second parameter will collect image which is our data
			startActivityForResult(cameraIntent, 0);  
			break;
		}
	}
// this method is used to get data which is our image right now in this project
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		// RESULT_OK means operation succeeded
		if(resultCode == RESULT_OK){
			// Bundle is used to get intent data using getExtra method
			Bundle b = data.getExtras();
			// getting bitmap data and store in wall bitmap variable
			wall = (Bitmap) b.get("data");
			// setting bitmap to image view displaying
			pic.setImageBitmap(wall);
		}
	}
}

Now image will display in Image View right in front of you.Next to set wallpaper call this method setWallpaper().

public class MainActivity extends Activity implements OnClickListener{ 
        ImageView pic; // image view variable to set captured image
	Button camera,setwall;  // button variables to open camera and set wallpaper
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        }
         // public method to get all id's of UI Components
        public void initialize(){
    	 pic = (ImageView) findViewById(R.id.imageView1);
    	 camera = (Button) findViewById(R.id.button2);
    	 setwall  =(Button) findViewById(R.id.button1);
    	 camera.setOnClickListener(this);   // click listener
    	 setwall.setOnClickListener(this);  // click listener
       }
       @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
                // getId method is used to get reference id of UI element.
		switch(v.getId()){
                // set wallpaper button case
		case R.id.button1:
			try {
				getApplicationContext().setWallpaper(wall);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
                // open camera button case
		case R.id.button2:
				// open camera to capture image and return
			cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
			// starting activity and accept return data second parameter will collect image which is our data
			startActivityForResult(cameraIntent, 0);  
			break;
		}
	}
// this method is used to get data which is our image right now in this project
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		// RESULT_OK means operation succeeded
		if(resultCode == RESULT_OK){
			// Bundle is used to get intent data using getExtra method
			Bundle b = data.getExtras();
			// getting bitmap data and store in wall bitmap variable
			wall = (Bitmap) b.get("data");
			// setting bitmap to image view displaying
			pic.setImageBitmap(wall);
		}
	}
}

Now one last thing.To set wallpaper using android imageview exmaple application you must have SET_WALLPAPER permission in your Android Manifest like this.

imageview,android imageview,android imageview example,imageview example,Camera Wallpaper

That’s it.

Run android imageview example application project and open camera,capture image.It will display within the imageview.

I hope you have got every step clearly of android imageview example tutorial.

Don’t forget to leave your feedback thanks.