Hello Friends,
Here I am writing code to set wallpaper in android.
Its very applicable in many scenarios like after downloading an image we have to give an option to set it
as wallpaper or make functionality to create App wallpaper to phone wallpaper.
First of all we need to declare following permission in manifest file:
uses-permission android:name="android.permission.SET_WALLPAPER">
uses-permission android:name="android.permission.SET_WALLPAPER_HINTS">
We use Displaymatrics because we have some reasons like we need to set image to every devices or
every screen size.
As we know that In Android, We deal with so many screen size devices.
// Convert image to Bitmap
Bitmap imageBm = BitmapFactory.decodeStream(getResources().openRawResource(R.id.wall_img));
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenHight = dm.heightPixels;
int screenWidth = dm.widthPixels;
WallpaperManager myWallManager = WallpaperManager.getInstance(this);
try {
myWallManager.setBitmap(imageBm);
wallpaperManager.suggestDesiredDimensions(screenWidth, screenHight);
Toast.makeText(this, "Yesss. Got it Wallpaper Changed", Toast.LENGTH_LONG).show();
} catch (IOException ioe) {
ioe.printStackTrace();
}
0 Comment(s)