Run the effect chart first
First of all, we want to put a picture of their favorite into the SDcard, in short, as long as we can get the picture on it.
I am here in the sdcard, you can use mouse click to import in Eclipse, more convenient, can also run on the command line:
C:\Documents and settings\administrator>adb push "C:\Documents and Settings\Administrator\My Documents\My Pictures\ Mm-320x480.png "/sdcard/mm-320x480.png
After the import is successful, you need to get this picture, the code is as follows:
Copy Code code as follows:
Package com.android.test;
Import Java.io.File;
Import java.io.IOException;
Import android.app.Activity;
Import Android.app.WallpaperManager;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.widget.Toast;
public class Wallpaperactivity extends activity {
public static final String file_name = "Mm-320x480.png";
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Bitmap wall = Getwallpaper ();
SetWallpaper (wall);
}
Get pictures from SDcard
Private Bitmap Getwallpaper () {
Get the path to the root directory of SDcard
File root = Environment.getexternalstoragedirectory ();
Get the picture specified in SDcard
File wall = new file (root, file_name);
Get the absolute path to the picture
String path = Wall.getabsolutepath ();
SYSTEM.OUT.PRINTLN ("path =" + path);
Get a picture based on the absolute path of the picture
return Bitmapfactory.decodefile (path);
}
Set the acquired picture as a wallpaper
private void SetWallpaper (Bitmap wall) {
Get an object for a wallpaper manager
Wallpapermanager Wmanager = Wallpapermanager.getinstance (this);
try {
Set the corresponding picture as wallpaper
Wmanager.setbitmap (wall);
}
catch (IOException e) {
String msg = "Set Desktop background exception:" + E.getlocalizedmessage ();
System.out.println ("Set Desktop background exception:" + msg);
Toast.maketext (This, MSG, Toast.length_long). Show ();
E.printstacktrace ();
}
}
}
Code comments are more detailed.
Get the SDcard Path First, then find the file from the SDcard, get the path to the file, and build the picture from Bitmapfactory.
After you get the picture, you need to set the picture as a wallpaper.
Just get an object from the wallpaper manager and call Wmanager.setbitmap (wall) ok
Actually setting up the wallpaper is very simple, there is wood there
Oh, yes, don't forget to add the permission to set the wallpaper in the Manif.xml file:
<uses-permission android:name= "Android.permission.SET_WALLPAPER" ></uses-permission>
You can set your favorite wallpaper, the effect is very bright bar, haha