Android Study Notes (4) Change desktop wallpaper for Android-wallpapermanager

Source: Internet
Author: User

In Android, wallpaper management is primarily undertaken by wallpapermanager.

 

 

Provides access to the system wallpaper. With wallpapermanager, you can get
The current wallpaper, get the desired dimensions for the wallpaper, set
Wallpaper, and more. Get an instance of wallpapermanagergetInstance().

 

 

The initialization method is

Wallpapermanager = wallpapermanager. getinstance (this );

 

The method to obtain the current wallpaper is:

Drawable = wallpapermanager. getdrawable ();

 

To set an image as a wallpaper

 

 

Bitmapdrawable source = new bitmapdrawable (PATH); </P> <p> try {<br/> wallpapermanager. setbitmap (source. getbitmap (); <br/> IV. setimagedrawable (source); <br/>}catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} 

 

The following figure shows the implementation result:

 

 

After the wallpaper is changed:

 

 

The Code is as follows:

 

Package young. mywallpapermanager; <br/> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/> Import Java. util. arraylist; <br/> Import javax. security. auth. privatecredentialpermission; </P> <p> Import android. app. activity; <br/> Import android. app. wal Lpapermanager; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. graphics. drawable. bitmapdrawable; <br/> Import android. graphics. drawable. drawable; <br/> Import android. OS. bundle; <br/> Import android. view. contextmenu; <br/> Import android. view. menu; <br/> Import android. view. menuitem; <br/> Import android. view. view; <br/> Import android. view. contextmenu. Contextmenuinfo; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. edittext; <br/> Import android. widget. imageview; <br/> Import android. widget. textview; <br/> public class mywallpapermanager extends activity {</P> <p> private imageview IV; <br/> private button changeimagebutton; <br/> private textview currenttextview; </P> <p> private arraylist <F Ile> images; <br/> private wallpapermanager; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> // obtain the desktop wallpaper <br/> wallpapermanager = wallpapermanager. getinstance (this); <br/> drawable = wallpapermanager. getdrawabl E (); </P> <p> IV = (imageview) findviewbyid (R. id. imview); <br/> currenttextview = (textview) findviewbyid (R. id. currentwall); <br/> changeimagebutton = (button) findviewbyid (R. id. changewallpaperbutton); </P> <p> IV. setimagedrawable (drawable); </P> <p> // registerforcontextmenu (changeimagebutton); <br/> changeimagebutton. setoncreatecontextmenulistener (this); </P> <p> // retrieve all images <br/> images = new arraylist <File> (); <Br/> getallfiles (new file ("/sdcard ")); <br/>}</P> <p> @ override <br/> Public void oncreatecontextmenu (contextmenu menu, view, <br/> contextmenuinfo menuinfo) {</P> <p> menu. setheadertitle ("click to set as desktop Wallpaper"); <br/> for (INT I = 0; I <images. size (); I ++) {<br/> menu. add (0, I, I, images. get (I ). getname (); <br/>}</P> <p> @ override <br/> Public Boolean oncontextitemselected (menuitem item) {<br/> String name = (string) item. gettitle (); <br/> // obtain the image by name <br/> string Path = getelementbyname (name ). getpath (); <br/> // <br/> bitmapdrawable source = new bitmapdrawable (PATH ); </P> <p> try {<br/> // set as wallpaper <br/> wallpapermanager. setbitmap (source. getbitmap (); <br/> // display <br/> IV. setimagedrawable (source); <br/>}catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <B R/>}< br/> return true; <br/>}</P> <p> // obtain all PNG and jpg JPEG files. <br/> private void getallfiles (File root) {</P> <p> file files [] = root. listfiles (); </P> <p> If (files! = NULL) <br/> for (file F: Files) {</P> <p> If (F. isdirectory () {<br/> getallfiles (f ); <br/>}< br/> else {<br/> // check the file type <br/> string fpath = f. getpath (). substring (F. getpath (). lastindexof (". ") + 1, F. getpath (). length (); <br/> // if it is an image <br/> If (fpath. equals ("PNG") | fpath. equals ("jpg") {<br/> images. add (f); <br/>}</P> <p> private file getelementbyname (string name) {<br/> for (INT I = 0; I <images. size (); I ++) {<br/> If (images. get (I ). getname (). equals (name) <br/> return images. get (I); <br/>}< br/> return NULL; <br/>}</P> <p >}< br/> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.