7

I am developing a simple app that sets wallpapers based on user input. I am missing code for setting wallpapers. I have been looking for it in lots of websites in vain. Can anybody post a sample code that sets as a wallpaper as a drawable that is saved in the res folder?

Nick Stauner
  • 395
  • 3
  • 13
Amine
  • 195
  • 2
  • 8

3 Answers3

5

Works on Android 1.5 and above

public void setWallpaper() {
  Context context = this.getBaseContext(); 
  Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), mImageIds[pos]);

  context.setWallpaper(mBitmap);
}
Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
success_anil
  • 3,659
  • 3
  • 27
  • 31
  • Thank you so much !!! do u know by any chance how I can solve the pixelation problems. I heard that the app: wallpaper set and save produces super crisp wallpapers , does it just use crop method to adjust the wallpaper to the screen ? Thanks a lot once more – Amine Aug 05 '10 at 02:57
  • Can't say about that ... I will have to search before making a comment to it – success_anil Aug 05 '10 at 06:17
2

u can try

InputStream inputStream = getResources().openRawResource(wallpaperResource);
Bitmap setWallToDevice = BitmapFactory.decodeStream(inputStream);

try {
getApplicationContext().setWallpaper(setWallToDevice);
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
}

however this method is deprecated so u can use

try {
WallpaperManager.getInstance(getApplicationContext()).setResource(wallpaperResource);
} catch (IOException e){
e.printStackTrace();
}
0

Here how we can set Wallpaper from our android application

MainActivity.Java

public class AlarmActivity extends Activity{



       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);



            WallpaperManager myWallpaperManager
                    = WallpaperManager.getInstance(getApplicationContext());


                    try {
                        myWallpaperManager.setImageResource(R.raw.sample);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

    }
}

Manifest.xml

you should provide this permission

<uses-permission android:name="android.permission.SET_WALLPAPER" />
Krishh
  • 101
  • 6