如何添加一组作为壁纸按钮

时间:2017-02-15 18:59:37

标签: wallpaper

大家好我正在创建一个在线壁纸应用程序。用户可以在线访问壁纸。我想添加两个按钮,第一个设置为壁纸,第二个是下载按钮。所以,如果有人帮助我这个我会感谢他们谢谢。

公共类GalleryDetailActivity扩展了ActionBarActivity {

public static final String EXTRA_IMAGE = "extra_image";


private ImageView mImageView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery_detail);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    mImageView = (ImageView) findViewById(R.id.image);

    if (getIntent() != null && getIntent().getExtras() != null) {
        if (getIntent().getExtras().containsKey(EXTRA_IMAGE)) {
            Picasso.with(this).load(getIntent().getExtras().getString(EXTRA_IMAGE)).into(mImageView);
        }

    }

1 个答案:

答案 0 :(得分:0)

首先,您应该确保您有权采取此类行动。添加您的清单:

 <uses-permission android:name="android.permission.SET_WALLPAPER"/>

要下载某些网站中托管的图片,您可以使用“doInBackground Thread”,如下所示:

How to download and save an image in Android

设置壁纸的按钮由以下代码创建:

Button setWallpaper = (Button)findViewById(R.id.YOUR_BUTTON);
ImageView imagePreview = (ImageView)findViewById(R.id.YOUR_PREVIEW);
imagePreview.setImageResource(YOUR_IMAGE_RESOURCE);

setWallpaper.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View arg0) {
            WallpaperManager myWallpaperManager 
            = WallpaperManager.getInstance(getApplicationContext());
            try {
                myWallpaperManager.setResource(YOUR_IMAGE_RESOURCE);
            } catch (IOException e) {
                e.printStackTrace();
            }
}});

YOUR_IMAGE可以是本地资源,例如:

R.drawable.myImageFile

答案中的链接有几种方法可以下载您的在线图像。请检查并尝试首先使用本地图像设置壁纸。

File f = new File(Environment.getExternalStorageDirectory(), "yourfile.jpg");
String path = f.getAbsolutePath();
File jpg = new File(path);

if(jpg.exists()) {
    Bitmap bmp = BitmapFactory.decodeFile(path);
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
    WallpaperManager m=WallpaperManager.getInstance(this);

    try {
        m.setBitmap(bmp);
    } catch (IOException e) {
        e.printStackTrace();
    }
}