Download Code

Android Studio ImageView Example – Camera Wallpaper

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

So First i’ll give you some information about Image View and Camera.After that you will use these componenst within android studio 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 camera according to the devices.Camera is basically used to captured image and video.You can read further detail about Camera from Android DeveloperOfficial Website.

Create new android studio imageview example application project.After that go to your activity_main.xml file.Add imageview and two buttons.

activity_main.xml

android studio imageview,android studio imageview example,imageview example,imageview,camera,

After adding those UI components go to your MainActivity.java class.Do the following steps.

  • Create variables for Image View and Buttons.
  • Create public method to get all id’s of UI Component and call it in onCreate() method.
  • Implement click event on camera and set wallpaper button.

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.

When camera started and captured an image.After that 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 to Image View which you add in xml file.

Next image will display within Image View.Then set wallpaper by calling this method setWallpaper().

MainActivity.java

package com.example.anonymous.studiowallpaper;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    ImageView pic; // image view variable to set captured image
    Button camera,setwall;  // button variables to open cam and set wallpaper
    Intent cameraIntent; // intent variable for starting cam
    Bitmap wall;         // variable to store data which image
    @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
        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 cam button case
            case R.id.button2:
                // open cam 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);
        }
    }

}

Next you must have SET_WALLPAPER permission in your Android Manifest.

android studio imageview,android studio imageview example,imageview example,imageview,camera,

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

android studio imageview,android studio imageview example,imageview example,imageview,camera,

 

That’s it.

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

Don’t forget to leave your feedback thanks.