How To Get Android Lock Screen Wallpaper?
Solution 1:
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
ParcelFileDescriptorpfd= 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
*/publicstatic Object getLockScreenWallpaper(Context context)
{
WallpaperManagerwallpaperManager= WallpaperManager.getInstance(context);
if (Build.VERSION.SDK_INT >= 24)
{
ParcelFileDescriptorpfd= wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (pfd == null)
pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);
if (pfd != null)
{
finalBitmapresult= 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.
Solution 2:
The code I was testing is similar to yours. I have tested it on Samsung A5 and LG Nexus 5X.
MainActivity.java
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
publicclassMainActivityextendsAppCompatActivity {
publicstaticfinalintREQUEST_CODE_EXTERNAL_STORAGE=5;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Stringpermission= Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
permission = Manifest.permission.READ_EXTERNAL_STORAGE;
}
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, newString[]{permission}, REQUEST_CODE_EXTERNAL_STORAGE);
} else {
retrieveLockScreenWallpaper();
}
}
@OverridepublicvoidonRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
retrieveLockScreenWallpaper();
}
}
}
}
@SuppressLint("MissingPermission")privatevoidretrieveLockScreenWallpaper() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
WallpaperManagermanager= WallpaperManager.getInstance(getApplicationContext());
ParcelFileDescriptordescriptor= manager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (descriptor != null) {
Bitmapbitmap= BitmapFactory.decodeFileDescriptor(descriptor.getFileDescriptor());
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
}
}
}
}
manifest.xml
<manifestxmlns:android="http://schemas.android.com/apk/res/android"package="aminography.com.lockscreenapplication"><uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" /><application...
</application></manifest>
.
Result on LGE Nexus 5X (Android 8.1.0, API 27):
Solution 3:
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.
Are you sure you have a valid lock screen wallpaper set for the current user?
/**
* Get an open, readable file descriptor to the given wallpaper image file.
* The caller is responsible for closing the file descriptor when done ingesting the file.
*
* <p>If no lock-specific wallpaper has been configured for the given user, then
* this method will return {@code null} when requesting {@link #FLAG_LOCK} rather than
* returning the system wallpaper's image file.
*
* @param which The wallpaper whose image file is to be retrieved. Must be a single
* defined kind of wallpaper, either {@link #FLAG_SYSTEM} or
* {@link #FLAG_LOCK}.
*
* @see #FLAG_LOCK
* @see #FLAG_SYSTEM
*/public ParcelFileDescriptor getWallpaperFile(@SetWallpaperFlagsint which) {
return getWallpaperFile(which, mContext.getUserId());
}
There is a similar function in WallpaperManager openDefaultWallpaper in that if you want LOCK-SCREEN wallpaper you get null, always, as According to the code Factory-default lock wallpapers are not yet supported.
So in your case maybe you haven't set the Lock-Screen wallpaper. Just to test, Download any wallpaper app, and try to set a wallpaper, then use your earlier code, to get it.
/**
* Open stream representing the default static image wallpaper.
*
* If the device defines no default wallpaper of the requested kind,
* {@code null} is returned.
*
* @hide
*/publicstatic InputStream openDefaultWallpaper(Context context, @SetWallpaperFlagsint which) {
final String whichProp;
finalint defaultResId;
if (which == FLAG_LOCK) {
/* Factory-default lock wallpapers are not yet supported
whichProp = PROP_LOCK_WALLPAPER;
defaultResId = com.android.internal.R.drawable.default_lock_wallpaper;
*/returnnull;
}
You can check the code at : http://androidxref.com/8.1.0_r33/xref/frameworks/base/core/java/android/app/WallpaperManager.java#openDefaultWallpaper
Post a Comment for "How To Get Android Lock Screen Wallpaper?"