Is it possible to set the android wallpaper image programatically? I'd like to create a service that downloads an image from the web and updates the home screen wallpaper periodically.

Solution 1

If you have image URL then use

WallpaperManager wpm = WallpaperManager.getInstance(context);
InputStream ins = new URL("absolute/path/of/image").openStream();

If you have image URI then use

WallpaperManager wpm = WallpaperManager.getInstance(context);

In your manifest file:

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

Solution 2

From this page on the developer site:

public void setStream (InputStream data)

Change the current system wallpaper to a specific byte stream. The give InputStream is copied into persistent storage and will now be used as the wallpaper. Currently it must be either a JPEG or PNG image.

Solution 3

If you have bitmap of image than you will add this function to set as wallpaper:

  public void SetBackground(int Url) {

    try {
        File file = new File("/sdcard/sampleimage");
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Url);
        bitmap.compress(CompressFormat.JPEG, 80, new FileOutputStream(file));
        Context context = this.getBaseContext();
        Toast.makeText(getApplicationContext(), "Wallpaper has been set",             Toast.LENGTH_SHORT).show();            
    } catch (MalformedURLException e) {
    } catch (IOException e) {

you should add permission for this

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

hope it will work

Solution 4

OK Here's how to do it before api 2.0:

You need to call getApplicationContext.setWallpaper() and pass it the bitmap.

This method is now deprecated. See ChrisF's answer for details on the new method.