Using WallpaperManager in Android to set wallpaper

前端 未结 2 602
庸人自扰
庸人自扰 2021-01-01 04:32

Below are my codes, I want to use wallpaper manager to set as wallpaper. I\'m using Universal Image Loader, but i dont know how implement wallpaper manager. My setWall() is

2条回答
  •  轮回少年
    2021-01-01 04:41

    You should convert your Drawable to a Bitmap and then use the setBitmap() function of wallpaper manager to set your desired wallpaper

    Convert Drawable to Bitmap

    private Bitmap getBitmapFromDrawable(Drawable d){
             Bitmap bitmap = 
                     ((BitmapDrawable) d).getBitmap();
             return bitmap;
    }
    

    Set as wallpaper

    public void setWall() { 
        WallpaperManager myWallpaperManager = 
             WallpaperManager.getInstance(getApplicationContext()); 
     try { 
        myWallpaperManager.setBitmap(getBitmapFromDrawable(R.drawable.app_icon); 
     } catch (IOException e) { 
        // TODO Auto-generated catch block e.printStackTrace(); 
     } 
    } 
    

    Hope this helps

提交回复
热议问题