我正在构建一个启动器,需要访问用户当前的背景壁纸,但每次启动应用程序时,我都会在日志中收到警告W/WallpaperManager: No permission to access wallpaper, suppressing exception to avoid crashing legacy app.
。
这是我正在使用的代码:
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
ImageView imageView = findViewById(R.id.background);
imageView.setImageDrawable(wallpaperDrawable);
此代码返回默认的库存背景图像,而不是我的实际背景图像。我已经注意到其他一些主流发射器(Evie,Launchair等)的这个问题,但它似乎只发生在我最近更新的Andorid 8.1设备上。
进一步深入到实际的Android源代码,我发现了这个:
if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.O) {
Log.w(TAG, "No permission to access wallpaper, suppressing"
+ " exception to avoid crashing legacy app.");
} else {
// Post-O apps really most sincerely need the permission.
throw e;
}
但我似乎无法找到所需的实际许可的参考。
任何帮助表示赞赏!
对于Api 27(Android 8.1)设备,将targetSdkVersion
更改为27并将READ_EXTERNAL_STORAGE
权限添加到清单中可以解决此问题。