0

I have a number of images in an array called pics.Where do i call the wallpapermanager?? Please tell me how to create a wallpaper when the image view is clicked. I know i have to create an OnCickLisener on the imageview and then use the wallpapermanager to call the image.but since the images are in an array how do i call that particular image which is being clicked?Since it wont be in the R.drawable. Here is my code:

public class Rd extends Activity  {
    Integer [] pics= {
        R.drawable.rd1,
        R.drawable.rd2,
        R.drawable.rd3,
        R.drawable.rd4,
        R.drawable.rd5,


    };
    ImageView imageView;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dravid);
        Gallery ga = (Gallery)findViewById(R.id.Gallery01);
    ga.setAdapter(new ImageAdapter(this));

    imageView = (ImageView)findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getBaseContext(), 
            "You have selected picture " + (arg2+1) +"of Rd", 
                        Toast.LENGTH_SHORT).show();
                imageView.setImageResource(pics[arg2]);



            }

        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_dd, menu);
        return true;
    }
     public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
            ctx = c;
            TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
            ta.recycle();
        }

        @Override
    public int getCount() {

        return pics.length;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageResource(pics[arg0]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(200,120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

   }
}

Here is the stack trace from the force close:

2-26 21:22:02.817: I/Process(440): Sending signal. PID: 440 SIG: 9
02-26 21:23:04.677: D/dalvikvm(472): GC_EXTERNAL_ALLOC freed 53K, 52% free 2586K/5379K, external 1012K/1038K, paused 70ms
02-26 21:23:09.967: D/AndroidRuntime(472): Shutting down VM
02-26 21:23:09.967: W/dalvikvm(472): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-26 21:23:09.990: E/AndroidRuntime(472): FATAL EXCEPTION: main
02-26 21:23:09.990: E/AndroidRuntime(472): java.lang.SecurityException: Access denied to process: 472, must have permission android.permission.SET_WALLPAPER
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.os.Parcel.readException(Parcel.java:1322)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.os.Parcel.readException(Parcel.java:1276)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.app.IWallpaperManager$Stub$Proxy.setWallpaper(IWallpaperManager.java:179)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.app.WallpaperManager.setResource(WallpaperManager.java:448)
02-26 21:23:09.990: E/AndroidRuntime(472):  at com.wallpaper.rahul.dravid.RahulDravid$1$1.onClick(RahulDravid.java:141)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.view.View.performClick(View.java:2485)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.view.View$PerformClick.run(View.java:9080)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.os.Handler.handleCallback(Handler.java:587)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.os.Looper.loop(Looper.java:123)
02-26 21:23:09.990: E/AndroidRuntime(472):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 21:23:09.990: E/AndroidRuntime(472):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 21:23:09.990: E/AndroidRuntime(472):  at java.lang.reflect.Method.invoke(Method.java:507)
02-26 21:23:09.990: E/AndroidRuntime(472):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 21:23:09.990: E/AndroidRuntime(472):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 21:23:09.990: E/AndroidRuntime(472):  at dalvik.system.NativeStart.main(Native Method)
02-26 21:28:10.137: I/Process(472): Sending signal. PID: 472 SIG: 9
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • The information in the stack trace tells you exactly what the problem is. Find this line in the stack trace `java.lang.SecurityException: Access denied to process: 472, must have permission android.permission.SET_WALLPAPER` This is telling you that you need to have the SET_WALLPAPER Security permission in order to be able to set the users wallpaper. add a Uses Permission tag to your manifest to declare `android.permission.SET_WALLPAPER` – FoamyGuy Feb 26 '13 at 16:37

1 Answers1

1

you should be able to reference from the array, just like you did when you set the image view.

ga.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
        Toast.makeText(v.getContext(), "You have selected picture " + (pos+1) +"of Rd", 
            Toast.LENGTH_SHORT).show();
        imageView.setImageResource(pics[pos]);
        imageView.setOnClickListner(new OnClickListener() {
          public void onClick(View v) {
            WallpaperManager wm = WallpaperManager.getInstance(v.getContext());
            try {
                wm.setResource(pics[pos]);
            } catch (IOException e) {
                e.printStackTrace();
            }
          }
        });
    }
});

I suggest you get in the habit of renaming auto generated variable names to something more descriptive. Your code is very confusing with arg0, arg1 etc...

Also when creating your Toast there is no need to use getBaseContext() You should use either YourActivity.this or v.getContext()

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Okay I will keep that in mind! Thank you! When i execute the code, it force closes every time i click on the image view! Any idea why it is happening? – varshini jagannath Feb 26 '13 at 15:42
  • edit your question and include the stacktrace from your logcat when it crashes. – FoamyGuy Feb 26 '13 at 15:43
  • that part is not relevant to your problem, Look for a big block of red text that says something about an Exception. and when you find it edit your question and post it there, the formatting in the comments is not made for that. – FoamyGuy Feb 26 '13 at 16:13