【问题标题】:I cannot change Android wallpaper using WallpaperManager我无法使用 WallpaperManager 更改 Android 壁纸
【发布时间】:2015-03-17 00:43:17
【问题描述】:

我正在尝试使用代码更改 Android 壁纸。我正在使用 WallpaperManager 类,但没有占上风。我在 /drawable 目录中使用了 .png 图像。我收到一条错误消息,上面写着“预期的原始类型资源”。当我运行应用程序时(当该方法运行时),它崩溃了。我一定是一个非常愚蠢的错误的受害者。 changeWallpaper() 方法在用户点击按钮后运行。这是我的代码:

 public void changeWallpaper(View view) {

    try{
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        wallpaperManager.setResource(R.drawable.material_wallpaper);
        String successMessage = "Wallpaper Changes";
        Toast.makeText(this, successMessage, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
        String failedMessage = "Operation failed";
        Toast.makeText(this, failedMessage, Toast.LENGTH_SHORT).show();
    }

}

编辑:我的 /res/ 目录中没有“原始”文件夹。

【问题讨论】:

  • 您是否尝试将图像放入 res/raw 文件夹?它说它期待一个原始资源。

标签: android wallpaper android-wallpaper


【解决方案1】:

如果您想继续使用 Drawable,您可以将资源转换为位图,然后使用 setBitmap(Bitmap _bitmap) 将其设置为墙纸(请参阅 WallpaperManager)。

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity().getApplicationContext());
Bitmap bitmap = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.material_wallpaper);
wallpaperManager.setBitmap(bitmap);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多