Java Android从gridview图像设置壁纸

Java Android从gridview图像设置壁纸,java,android,gridview,wallpaper,Java,Android,Gridview,Wallpaper,像往常一样,恐怕我对这件事有点不知所措 我已经成功地修改了一个教程,从SD卡上的文件夹中获取一组图像以显示在GridView中 我试图做的是,允许用户选择一个设置为壁纸 我的问题是,尽管我知道图像的文件夹位置必须已被接收才能显示,但我无法确定如何获取它,然后将其设置为壁纸 我已经包括了我的许多尝试中的一小部分,但它抛出了所有类型的错误,主要是空指针,因为我正在尝试从galleryitem.xml文件访问ImageView public class MyWallpapers extends Ac

像往常一样,恐怕我对这件事有点不知所措

我已经成功地修改了一个教程,从SD卡上的文件夹中获取一组图像以显示在GridView中

我试图做的是,允许用户选择一个设置为壁纸

我的问题是,尽管我知道图像的文件夹位置必须已被接收才能显示,但我无法确定如何获取它,然后将其设置为壁纸

我已经包括了我的许多尝试中的一小部分,但它抛出了所有类型的错误,主要是空指针,因为我正在尝试从galleryitem.xml文件访问ImageView

 public class MyWallpapers extends Activity {


private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;

ImageView Selected;


ArrayList<String> f = new ArrayList<String>();// list of file paths
File[] listFile;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wallpaper_view_activity);


    getFromSdcard();
    GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);


    final ImageView Selected = (ImageView) findViewById(R.id.thumbImage);
      Selected.setOnClickListener(new OnClickListener() {

          public void onClick(View v) {


              //need to get the position of the file and set it as wallpaper when the user clicks the image

              Context context = this.getAbsolutePath();
              Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
                      mImageIds[pos]);
              context.setWallpaper(context);

          }
      });
  }



public void getFromSdcard()
{
    File file= new File(android.os.Environment.getExternalStorageDirectory(),"Wallpaper");

        if (file.isDirectory())
        {
            listFile = file.listFiles();


            for (int i = 0; i < listFile.length; i++)
            {

                f.add(listFile[i].getAbsolutePath());

            }
        }
}

public class ImageAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return f.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }


        Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
        holder.imageview.setImageBitmap(myBitmap);
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;


}
    }
这是我在wallpaper\u view\u activity.xml中的GridView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<GridView
    android:id="@+id/PhoneImageGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/button1"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />
最后是galleryitem.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
    android:id="@+id/thumbImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />
我们将一如既往地非常感谢您在这方面提供的任何帮助

ImageLocation = listFile[position].getAbsolutePath();

                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                            Bitmap bitmap = BitmapFactory.decodeFile(ImageLocation, options);


                            WallpaperManager myWallpaperManager 
                            = WallpaperManager.getInstance(getApplicationContext());
                            try {
                                myWallpaperManager.setBitmap(bitmap);
                                Toast.makeText(MyWallpapers.this, "Wallpaper Set", Toast.LENGTH_SHORT).show();
                            } catch (IOException e) {
                                Toast.makeText(MyWallpapers.this, "Error", Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                            }