Android - how to set the wallpaper image

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>

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.


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


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.