问题
I'm building a launcher and need to access the user's current background wallpaper but every time I launch the app I get the warning W/WallpaperManager: No permission to access wallpaper, suppressing exception to avoid crashing legacy app.
in the logs.
Here is the code I'm using:
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
ImageView imageView = findViewById(R.id.background);
imageView.setImageDrawable(wallpaperDrawable);
This code returns the default stock background image rather than my actual background image. I've noticed this problem on a few other mainstream launchers (Evie, Launchair, etc) but it only seems to happen on my recently-updated Andorid 8.1 device.
Digging further into the actual Android Source code, I've found this:
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;
}
But I can't seem to find a reference to the actual permission required.
Any help is appreciated!
回答1:
For Api 27 (Android 8.1) devices, changing the targetSdkVersion
to 27 and adding the READ_EXTERNAL_STORAGE
permission to the manifest fixes this.
来源:https://stackoverflow.com/questions/47995463/accessing-wallpapermanager-in-android-8-1