如果图像存储在阵列中,如何设置壁纸?

时间:2013-02-26 15:00:15

标签: android wallpaper android-drawable

我在一个名为pics的数组中有许多图像。我在哪里调用wallpapermanager? 请告诉我如何在单击图像视图时创建壁纸。我知道我必须在imageview上创建一个OnCickLisener,然后使用wallpapermanager来调用图像。但由于图像是在数组中,我如何调用被点击的特定图像?因为它不在R.drawable中。 这是我的代码:

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;
    }

   }
}

这是来自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

1 个答案:

答案 0 :(得分:1)

您应该可以从数组中引用,就像您在设置图像视图时一样。

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();
            }
          }
        });
    }
});

我建议您养成将自动生成的变量名称重命名为更具描述性的习惯。您的代码与arg0arg1等等相混淆......

同时在创建Toast时,无需使用getBaseContext()您应该使用YourActivity.thisv.getContext()