我有一张图库和一张imageView,如下图所示:http://i50.tinypic.com/2e6h2so.png 我需要button1将当前图像设置为壁纸 我在setResource
中遇到了问题 myWallpaperManager.setResource(mImageIds[position]);
这是我的代码:
public class batman extends Activity implements OnClickListener{
Button set;
private int[] mImageIds = {
R.drawable.b,
R.drawable.b1,
R.drawable.b2,
R.drawable.b3,
R.drawable.b4,
R.drawable.b5,
R.drawable.b6,
R.drawable.b7,
R.drawable.b8,
R.drawable.b9,
R.drawable.b10,
R.drawable.b11,
R.drawable.b12,
R.drawable.b13,
R.drawable.b14,
R.drawable.b15,
R.drawable.b16,
R.drawable.b17,
R.drawable.b18,
R.drawable.b19,
R.drawable.b20,
R.drawable.b21,
R.drawable.b22,
R.drawable.b23,
R.drawable.b24,
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.batman);
set=(Button)findViewById(R.id.button1);
set.setOnClickListener(this);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick
(AdapterView<?> parent, View v, int position, long id) {
ImageView imageView =(ImageView)findViewById(R.id.imageView1);
imageView.setImageResource(mImageIds[position]);
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.GalleryA);
mGalleryItemBackground = a.getResourceId(
R.styleable.GalleryA_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id=arg0.getId();
if(id==R.id.button1)
{
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(mImageIds[position]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
答案 0 :(得分:2)
首先,您需要在清单中获得此权限:
“android.permission.SET_WALLPAPER”
ImageAdapter i = (ImageAdapter)parent.getAdapter();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
}
catch (IOException e)
{
Toast.makeText(MainActivity.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
}
}
});
}
答案 1 :(得分:0)
首先您必须向SET_WALLPAPER
AndroidManifest.xml
权限
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.SET_WALLPAPER" />
</manifest>
然后更改您的Button ClickListener
,如下所示:
ImageAdapter image = (ImageAdapter)parent.getAdapter(); Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),mImageIds[pos]); <----Try by changing this line. WallpaperManager m_manager = WallpaperManager.getInstance(getApplicationContext()); try { m_manager.setBitmap(mBitmap); } catch (IOException e) {} } }); }