Android retrieves images from SDCard and sets them as the desktop background.

Source: Internet
Author: User
1. push the background image to the SDCard adb push MM-320x480.png/sdcard/to confirm whether it already exists, you can go to SDCard to see: adb shellcd sdcardls can see the uploaded picture: 2. Compile the Activity program
Public static final String TAG = "WallpaperActivity ";

/**
* Background image name
*/
Private static final String FILE_NAME = "MM-320x480.png ";

/**
* @ See android. app. Activity # onCreate (android. OS. Bundle)
*/
Public void onCreate (Bundle cycle ){
Super. onCreate (cycle );
Super. setContentView (R. layout. wallpaper );

// Obtain the background image
Bitmap wallpaper = this. getWallpager ();

// Set the desktop background
This. putWallpaper (wallpaper );
}

/**
* Obtain the background image in the SDCard.
*/
Private Bitmap getWallpager (){
// SDCard path, that is, "/sdcard /"
File root = Environment. getExternalStorageDirectory ();
// Mmimage
File wall = new File (root, FILE_NAME );
// Mmimage path
String path = wall. getAbsolutePath ();

Log. d (TAG, "MM file path:" + path );

Return BitmapFactory. decodeFile (path );
}

/**
* Set the desktop background
*/
Private void putWallpaper (Bitmap bitmap ){
Try {
WallpaperManager wallpaperManager = WallpaperManager. getInstance (this );
WallpaperManager. setBitmap (bitmap );
} Catch (IOException e ){
String msg = "An exception occurred when setting the desktop background:" + e. getLocalizedMessage ();
Log. e (TAG, "an exception occurred when setting the desktop background! ", E );
Toast. makeText (this, msg, Toast. LENGTH_LONG). show ();
}
}
3. Add the permission to set the desktop background in the AndroidManifest. xml file:
<! -- Set the desktop background permission -->
<Uses-permission android: name = "android. permission. SET_WALLPAPER"/>
4. Run the program and return to the desktop. The background has changed.
Related Article

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.