Android Custom Desktop feature code implementation _android

Source: Internet
Author: User

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

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.