How to set android lock screen image

Solution 1:

As of API Level 24 they have added new methods (and updated the documentation) and flags to the WallpaperManager which allow you to set a Wallpaper not only to the home screen but also to the Lockscreen

To set a Wallpaper to the Lockscreen use the new flag WallpaperManager.FLAG_LOCK, and one of the methods which take int which

WallpaperManager.getInstance(this).setStream(inputStream, null, true, WallpaperManager.FLAG_LOCK);

You can also use one of the following methods

int setStream (InputStream bitmapData,  Rect visibleCropHint,  boolean allowBackup, int which)

int setResource (int resid, int which)

int setBitmap (Bitmap fullImage, Rect visibleCropHint,  boolean allowBackup,  int which)

A nice addition is that you can now also check if you are allowed to set the wallpaper via isSetWallpaperAllowed, and get the current set wallpaper via getWallpaperFile

Check out the updated documentation for the WallpaperManager.

Solution 2:

There is no "lock screen image" in Android. There most certainly is no "lock screen image" concept that is the same between stock Android, HTC Sense, MOTOBLUR, etc. This simply is not part of the Android SDK.

The project that Mr. Rijk points to is a security violation that pretends to be a lock screen replacement.

Solution 3:

There is a way to do it on Samsung devices. In the intent you can put an extra.

intent.putExtra("SET_LOCKSCREEN_WALLPAPER", true);
startActivity(intent);

I've only tested this on some Samsung phones and there's no guarantee that this won't break some time in the future. Use with caution.

Solution 4:

You can use these three methods of WalpaperManager class but it will only work for nought version devices or above it:-

public int setBitmap (Bitmap fullImage, 
            Rect visibleCropHint, 
            boolean allowBackup, 
            int which)

public int setResource (int resid, 
            int which)

public int setStream (InputStream inputStreamData, 
            Rect visibleCropHint, 
            boolean allowBackup, 
            int which)

Parameter of these three methods:-

Bitmap/resid/inputStreamData :-this parameter accept data

visibleCropHint:-this parameter accept Rect object which is mainly used for Cropping functionality, for more information refer to Android developer reference website, you can also pass null if u don't want cropping functionality

allowBackup:-boolean: true if the OS is permitted to back up this wallpaper image for restore to a future device; false otherwise.

which:-It is one of the most important parameter which helps you to configure wallpaper for lock screen and home wallpaper. for lock screen use WalpaperManager.FLAG_LOCK and for home wallpaper use FLAG_SYSTEM

I am giving one example to make you understand how to use it:-

WalaperManager wm = WalaperManager.getInstance();
try {
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
           wm.setBitmap(bitmap,null,true,WalpaperManager.FLAG_LOCK);//For Lock screen
           Toast.makeText(context.context, "done", Toast.LENGTH_SHORT).show();
       }
       else{
            Toast.makeText(context.context, "Lock screen walpaper not supported", 
            Toast.LENGTH_SHORT).show();
       }
    } catch (e: Exception) {
        Toast.makeText(context.context, e.message, Toast.LENGTH_SHORT).show();
    }

for more information visit Android developer wallpaper manager reference