【发布时间】: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