Android 找不到当前选定的可绘制实时壁纸

Android 找不到当前选定的可绘制实时壁纸,android,live-wallpaper,Android,Live Wallpaper,我正在尝试查找当前选定的实时壁纸可绘制,但每次此代码都会返回默认的实时壁纸可绘制,而我的自定义实时壁纸设置为我的设备 WallpaperManager wallpaperManager wallpaperManager = WallpaperManager.getInstance( this); Drawable wallpaperDrawable1 = wallpaperManager.getDrawable(); getWindow().setBackgroundDrawable(wallp

我正在尝试查找当前选定的实时壁纸
可绘制
,但每次此代码都会返回默认的实时壁纸
可绘制
,而我的自定义实时壁纸设置为我的设备

WallpaperManager wallpaperManager wallpaperManager = WallpaperManager.getInstance( this);
Drawable wallpaperDrawable1 = wallpaperManager.getDrawable();
getWindow().setBackgroundDrawable(wallpaperDrawable1);

但是Docs说它将返回当前选择的实时壁纸,有什么问题吗?
清单

中的任何权限是否有任何要求?每个实时壁纸都将在设备中作为一个包找到。因此,每个生活壁纸包包含一个可绘制的图像,我们可以很容易地得到

方法
getwallperInfo()
用于获取有关实时墙纸的信息

因此,
getwallperinfo().loadThumbnail(PackageManager pm)
方法将为您解决这个问题

以下代码用于获取实时墙纸的可绘制图像:

    //Reference to the package manager instance
    PackageManager pm = getApplicationContext().getPackageManager();

    /*
     * Wallpaper info is not equal to null, that is if the live wallpaper
     * is set, then get the drawable image from the package for the 
     * live wallpaper
     */
    if (wallpaperManager.getWallpaperInfo() != null) {
         wallpaperDrawable = wallpaperManager
                .getWallpaperInfo().loadThumbnail(pm);
    } 

    /*
     * Else, if static wallpapers are set, then directly get the 
     * wallpaper image
     */
    else {
         wallpaperDrawable = wallpaperManager.getDrawable();
    }

这实际上不起作用。对于实时墙纸,它可以(通常也会)简单地返回表示任何图像的通用缩略图。使用google“Wallpapers”应用程序进行测试。loadThumbnail可能返回null,尽管getWallpaperInfo()不是null。因此,无论谁使用这段代码,请确保在使用它之前检查返回的drawable是否为null,以避免NullPointerException。