Vishwa Madushan Vishwa Madushan - 1 year ago 77
Android Question

How to use Intent to get external image and set as wallpaper

I used intent to open gallery of user's phone and now I need to set selected image as wallpaper for my activity.or set image to ImageView on my Activity.not the set as phone's wallpaper.
can somebody help me?
Here's the code I used so far.. i can select image.but nothing happens after that.

protected void onCreate(Bundle savedInstanceState) {
Button chooseImg=(Button) findViewById(;
chooseImg.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

Intent img=new Intent();
(img, "Select Picture"),SELECT_PICTURE);

Object tag=v.getTag();
Integer rId=(Integer) tag;
img.putExtra("bgImage", rId);

imgLayout=(ImageView) findViewById(;

int imgId=img.getExtras().getInt("bgImage");

catch(Exception e){
Toast.makeText(getApplicationContext(), "nope", Toast.LENGTH_SHORT).show();



Answer Source

Implement onActivityResult like this

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

    if (requestCode == SELECT_PICTURE&& resultCode == RESULT_OK && data != null && data.getData() != null) {

        Uri uri = data.getData();

        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            // Log.d(TAG, String.valueOf(bitmap));

            ImageView imageView = (ImageView) findViewById(;
        } catch (IOException e) {

For setting button background create a BitmapDrawable like this

BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);

and for layout you can use setBackgroundDrawable method

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download