Android - how to set the wallpaper image
Asked Answered
T

4

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.

Tormentor answered 26/12, 2009 at 19:49 Comment(1)
#2205592 click this link I write there sample code.Anuria
V
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.

Verdure answered 26/12, 2009 at 19:54 Comment(2)
Note that API first appeared in 2.0; if you want to support older versions, use one of the original APIs on Context: developer.android.com/reference/android/content/…Baldachin
i tried same thing for videoistream...it's not working ...for imageurl it work fine...any idea/suggestion here.Nunatak
C
31

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>
Cockatoo answered 18/10, 2012 at 10:6 Comment(3)
I tried same things for video stream..but I am getting --- "SkImageDecoder::Factory returned null" error...any suggestion here.Nunatak
is it possible to set live wallpaper as an default wallpaper from application.Nunatak
It must be a new Questions.. anyways what do you mean by default wallpaper?Cockatoo
V
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.

Verdure answered 26/12, 2009 at 19:54 Comment(2)
Note that API first appeared in 2.0; if you want to support older versions, use one of the original APIs on Context: developer.android.com/reference/android/content/…Baldachin
i tried same thing for videoistream...it's not working ...for imageurl it work fine...any idea/suggestion here.Nunatak
M
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

Mariquilla answered 17/4, 2012 at 5:41 Comment(0)
L
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.

Lecher answered 20/4, 2010 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.