27

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.

James Cadd
  • 12,136
  • 30
  • 85
  • 134
  • http://stackoverflow.com/questions/2205092/android-how-to-set-the-wallpaper-image/5813607#5813607 click this link I write there sample code. – Maidul Apr 28 '11 at 04:30

4 Answers4

32

If you have image URL then use

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

If you have image URI then use

WallpaperManager wpm = WallpaperManager.getInstance(context);
wpm.setResource(Uri.of.image);

In your manifest file:

<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>
Kishore
  • 952
  • 2
  • 11
  • 31
22

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.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 8
    Note that API first appeared in 2.0; if you want to support older versions, use one of the original APIs on Context: http://developer.android.com/reference/android/content/Context.html#setWallpaper(java.io.InputStream) – hackbod Dec 26 '09 at 20:51
  • i tried same thing for videoistream...it's not working ...for imageurl it work fine...any idea/suggestion here. – CoDe Oct 31 '12 at 12:42
5

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();
        context.setWallpaper(bitmap);            
        Toast.makeText(getApplicationContext(), "Wallpaper has been set",             Toast.LENGTH_SHORT).show();            
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }         
}

you should add permission for this

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

hope it will work

djk
  • 3,671
  • 9
  • 31
  • 40
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.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
Jonah
  • 139
  • 1
  • 2