How to Set Android Lock Screen Image

How to set android lock screen image

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.

how to set wallpaper as lockscreen or both(home and lock)?

Check my code if its working for you :)

WallpaperManager myWallpaperManager;
private static final int CHOOSE_IMAGE = 22;
private Button btnSetWallpaper;
String[] options = new String[]{
"Home Screen",
"Lock Screen",
"Both"
};

In OnCreate()

 btnSetWallpaper = findViewById(R.id.btnSetWallpaper);
btnSetWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "choose image"), CHOOSE_IMAGE);

}
});

In OnActivityResult()

if (requestCode == CHOOSE_IMAGE && data != null) {
mCropImageUri = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCropImageUri);
Log.e(TAG, "onActivityResult: BIT " + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose from below");
final Bitmap finalBitmap = bitmap;
builder.setItems(options, new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String selectedItem = Arrays.asList(options).get(i);
if (selectedItem.equals(options[0])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_SYSTEM);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
} else if (selectedItem.equals(options[1])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}

} else if (selectedItem.equals(options[2])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
}
}
});
dialog = builder.create();
dialog.show();

}

You wanna just set Flags for various Wallpaper Options.
Flags like...

WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM : for both Screen
WallpaperManager.FLAG_SYSTEM : for Home Screen
WallpaperManager.FLAG_LOCK :for Lock Screen

Thanks :)

Set lock screen wallpaper of Android device programmatically

WallpaperManager has a number of methods to help you get or set the specific wallpapers. As you want the lock screen wallpaper, use FLAG_LOCK. Relevant methods include:

getWallpaperFile(int)

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

How to set lock screen wallpaper on devices prior to API 24 in Android?

Use this Implicit intent to set the wallpaper or set lock screen wallpaper on devices prior to API 24.

Intent intent = new Intent("android.intent.action.ATTACH_DATA");
intent.addCategory("android.intent.category.DEFAULT");
String str = "image/*";
intent.setDataAndType(Uri.fromFile(new File(your_file_url)), str);
intent.putExtra("mimeType", str);
startActivity(Intent.createChooser(intent, "Set As:"));

How to change the lockscreen wallpaper in android?

This is now possible with the updated Android API in API Level 24.

You have to provide the target WallpaperManager.FLAG_LOCK flag via the setResource method.

int setResource (int resid, int which)

See the updated documentation for the WallpaperManager

How to get android lock screen wallpaper?

I find the answer myself, I hope it can help others with the same question.

The official docs for getWallpaperFile says: If no lock-specific wallpaper has been configured for the given user, then this method will return null when requesting FLAG_LOCK rather than returning the system wallpaper's image file.

The description is vague, at least not clear enough, what does it mean? If you set a photo as both lock screen and home screen wallpaper, the two share the same file, then by calling

ParcelFileDescriptor pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);

pfd will always be null, then you should get the lock screen wallpaper this way:

if (pfd == null)
pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);

you will get the non-null pfd. This is the case no lock-specific wallpaper has been configured.

On the contrary, lock-specific wallpaper has been configured if you set a photo as lock screen wallpaper directly, wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM) will return a non-null value.

So this is the code I use to retrieve the lock screen wallpaper:

/**
* please check permission outside
* @return Bitmap or Drawable
*/
public static Object getLockScreenWallpaper(Context context)
{
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
if (Build.VERSION.SDK_INT >= 24)
{
ParcelFileDescriptor pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (pfd == null)
pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);
if (pfd != null)
{
final Bitmap result = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());

try
{
pfd.close();
}
catch (Exception e)
{
e.printStackTrace();
}

return result;
}
}
return wallpaperManager.getDrawable();
}

Don't forget to add READ_EXTERNAL_STORAGE in the manifest file and grant it outside.



Related Topics



Leave a reply



Submit