How can I set the wallpaper on android without having to manually type the image source?
<p>I have now been searching for hours to find a way to achieve this. I am basically trying to set the wallpaper depending on the image that was clicked by the user, but I can not find anywhere, how to get the image source int to place in the InputStream. I am always seeing the image source being written manually, and this doesn't seem very useful if you were to place more images into the drawable folder etc... </p>

<p>Thanks for any help,</p>

<p>Tom. </p>

<p>The tutorial I was following is: <a href="http://www.youtube.com/watch?v=gu4qa2zaZ3M&amp;feature=player_embedded" rel="nofollow">http://www.youtube.com/watch?v=gu4qa2zaZ3M&amp;feature=player_embedded</a></p>

<p>My activity class code is:</p>

<pre><code>package com.cgt.myfirstapp;


import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Tutorial3 extends Activity implements OnClickListener {


    ImageView display;
    int drawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wallpaper);

        display = (ImageView) findViewById(R.id.ivDisplay);

        ImageView image1 = (ImageView) findViewById(R.id.ivImage1);
        ImageView image2 = (ImageView) findViewById(R.id.ivImage2);
        ImageView image3 = (ImageView) findViewById(R.id.ivImage3);
        Button setWall = (Button) findViewById(R.id.bSetWallpaper);

        image1.setOnClickListener(this);
        image2.setOnClickListener(this);
        image3.setOnClickListener(this);
        setWall.setOnClickListener(this);


    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub



        switch(v.getId()){


        case R.id.ivImage1:
            display.setImageResource(R.drawable.image1);
            drawable = R.drawable.image1;
            break;

        case R.id.ivImage2:
            display.setImageResource(R.drawable.image2);
            drawable = R.drawable.image2;
            break;

        case R.id.ivImage3:
            display.setImageResource(R.drawable.image3);
            drawable = R.drawable.image3; // I don't want to have to hardcode this for each case.
            break;


        case R.id.bSetWallpaper:

            InputStream inputStream = getResources().openRawResource(drawable);         
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            try{

                getApplicationContext().setWallpaper(bitmap);

            }catch(IOException e){

                e.printStackTrace();

            }

            break;

        }


        //Below is how I was trying to do it without the switch case,
        //but then I could not find a way to obtain the image src without hardcoding it. 



        /*


        ImageView iv = (ImageView) v;
        int id = iv.getId();

        if(id != R.id.bSetWallpaper){

        display.setImageDrawable(iv.getDrawable());

        }

        if(id == R.id.bSetWallpaper){
            InputStream inputStream = getResources().openRawResource(drawable);         
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            try{
                getApplicationContext().setWallpaper(bitmap);
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        */
    }
}
</code></pre>
