There are many ways to set up wallpaper on Android, there are two main ideas:
1: Set through Wallpapermanager
2: Through the System program settings
The following sections explain separately:
<1> throughWallpapermanagerSet up
This method can be used to set the image as wallpaper directly for all platforms, but it is not possible to crop/adjust the image.
try { Wallpapermanager wpm = (Wallpapermanager) getactivity (). Getsystemservice ( context.wallpaper_service); if (wallpaper! = null) { wpm.setbitmap (bitmap); LOG.I ("Xzy", "Wallpaper NOT NULL");} } catch (IOException e) { log.e (TAG, "Failed to set Wallpaper:" + e);}
You need to declare permissions in Androidmanifest.xml:
<uses-permission android:name = "Android.permission.SET_WALLPAPER"/>
<2> through System program settings
The advantage of the system application setting is that you can crop the picture, set the best wallpaper effect, and the operating experience is consistent with the platform. and this method is not allowed to declare permissions
There are two possible ways to do this.
1: Call system crop activity to set wallpaper by cropping activity:
Intent Intent = new Intent ("Com.android.camera.CropImage"); int width = getactivity (). Getwallpaperdesiredminimumwidth (); int height = getactivity (). Getwallpaperdesiredminimumheight (); Intent.putextra ("Outputx", width); Intent.putextra ("Outputy", height); Intent.putextra ("Aspectx", width); Intent.putextra ("Aspecty", height); Intent.putextra ("scale", true); Intent.putextra ("Nofacedetection", true); Intent.putextra ("SetWallpaper", true); Intent.putextra ("Data", ((bitmapdrawable) wallpaper). Getbitmap ()); StartActivity (Intent);
There is a drawback to this approach, Com.android.camera.CropImage is an optional action, not a standard action, so that all Android devices support the API, Many devices will appear activitynotfoundexception.
2. Call the system Intent.action_attach_data, the intent will arouse all the settings wallpaper program and set the contact picture program, the user can choose through chooseactivity:
The intent is a standard intent, so all settings will be supported.
Intent Intent = new Intent (intent.action_attach_data); Intent.addflags (intent.flag_grant_read_uri_permission); Intent.putextra ("MimeType", "image/*"); Uri uri = uri.parse (MediaStore.Images.Media . Insertimage (Getactivity (). Getcontentresolver (), ( ( bitmapdrawable) wallpaper). Getbitmap (), NULL, NULL)); Intent.setdata (URI); Startactivityforresult (Intent, set_wallpaper);
These methods are recommended only for the last one, for simple reasons: good experience and low adaptation cost.
xzy2046, reprint need to indicate. Blog home:http://blog.csdn.net/xzy2046
Several scenarios for Android settings wallpaper