如何获取android锁屏壁纸?
How to get android lock screen wallpaper?
我使用下面的代码在 android 8.1 phone 上检索 android 锁屏壁纸:
WallpaperManager manager = WallpaperManager.getInstance(getActivity());
ParcelFileDescriptor pfd = manager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (pfd == null) // pfd is always null for FLAG_LOCK, why?
return;
Bitmap lockScreenWallpaper = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
// ...
我已授予READ_EXTERNAL_STORAGE
权限,并事先设置了锁屏壁纸
我运行在真实的phone上演示,发现pfd
对于FLAG_LOCK
总是空的,所以我无法获取锁屏壁纸。请帮忙解决问题,谢谢
我测试的代码与您的相似。我已经在 Samsung A5 和 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;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE_EXTERNAL_STORAGE = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String permission = 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, new String[]{permission}, REQUEST_CODE_EXTERNAL_STORAGE);
} else {
retrieveLockScreenWallpaper();
}
}
@Override
public void onRequestPermissionsResult(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")
private void retrieveLockScreenWallpaper() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
ParcelFileDescriptor descriptor = manager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (descriptor != null) {
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(descriptor.getFileDescriptor());
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
}
}
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="aminography.com.lockscreenapplication">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
...
</application>
</manifest>
.
LGE Nexus 5X(Android 8.1.0,API 27)的结果:
如果没有为给定用户配置特定于锁的墙纸,那么在请求 FLAG_LOCK 而不是 return 系统墙纸图像文件时,此方法将 return 无效。
您确定为当前用户设置了有效的锁屏壁纸吗?
/**
* 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(@SetWallpaperFlags int which) {
return getWallpaperFile(which, mContext.getUserId());
}
WallpaperManager openDefaultWallpaper 中有一个类似的功能,如果您想要锁屏壁纸,您总是会得到 null,因为根据代码,尚不支持出厂默认锁定壁纸。
所以您的情况可能是您没有设置锁屏壁纸。
只是为了测试,下载任何壁纸应用程序,并尝试设置壁纸,然后使用您之前的代码来获取它。
/**
* Open stream representing the default static image wallpaper.
*
* If the device defines no default wallpaper of the requested kind,
* {@code null} is returned.
*
* @hide
*/
public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
final String whichProp;
final int 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;
*/
return null;
}
我自己找到了答案,希望能帮助到有同样问题的人。
getWallpaperFile 的官方文档说: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.
描述的很模糊,至少不够清楚,什么意思?如果你将一张照片设置为锁屏和桌面壁纸,两者共享同一个文件,那么通过调用
ParcelFileDescriptor pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
pfd
永远为null,那么你应该这样获取锁屏壁纸:
if (pfd == null)
pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);
您将获得非空 pfd
。是这样的no lock-specific wallpaper has been configured.
相反,lock-specific wallpaper has been configured
如果直接将照片设置为锁屏壁纸,wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM)
将return一个非空值。
所以这是我用来检索锁屏壁纸的代码:
/**
* 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();
}
不要忘记在清单文件中添加READ_EXTERNAL_STORAGE
并在外部授予它。
我使用下面的代码在 android 8.1 phone 上检索 android 锁屏壁纸:
WallpaperManager manager = WallpaperManager.getInstance(getActivity());
ParcelFileDescriptor pfd = manager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (pfd == null) // pfd is always null for FLAG_LOCK, why?
return;
Bitmap lockScreenWallpaper = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
// ...
我已授予READ_EXTERNAL_STORAGE
权限,并事先设置了锁屏壁纸
我运行在真实的phone上演示,发现pfd
对于FLAG_LOCK
总是空的,所以我无法获取锁屏壁纸。请帮忙解决问题,谢谢
我测试的代码与您的相似。我已经在 Samsung A5 和 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;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE_EXTERNAL_STORAGE = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String permission = 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, new String[]{permission}, REQUEST_CODE_EXTERNAL_STORAGE);
} else {
retrieveLockScreenWallpaper();
}
}
@Override
public void onRequestPermissionsResult(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")
private void retrieveLockScreenWallpaper() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
ParcelFileDescriptor descriptor = manager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
if (descriptor != null) {
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(descriptor.getFileDescriptor());
((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
}
}
}
}
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="aminography.com.lockscreenapplication">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
...
</application>
</manifest>
.
LGE Nexus 5X(Android 8.1.0,API 27)的结果:
如果没有为给定用户配置特定于锁的墙纸,那么在请求 FLAG_LOCK 而不是 return 系统墙纸图像文件时,此方法将 return 无效。
您确定为当前用户设置了有效的锁屏壁纸吗?
/**
* 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(@SetWallpaperFlags int which) {
return getWallpaperFile(which, mContext.getUserId());
}
WallpaperManager openDefaultWallpaper 中有一个类似的功能,如果您想要锁屏壁纸,您总是会得到 null,因为根据代码,尚不支持出厂默认锁定壁纸。
所以您的情况可能是您没有设置锁屏壁纸。 只是为了测试,下载任何壁纸应用程序,并尝试设置壁纸,然后使用您之前的代码来获取它。
/**
* Open stream representing the default static image wallpaper.
*
* If the device defines no default wallpaper of the requested kind,
* {@code null} is returned.
*
* @hide
*/
public static InputStream openDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
final String whichProp;
final int 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;
*/
return null;
}
我自己找到了答案,希望能帮助到有同样问题的人。
getWallpaperFile 的官方文档说: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.
描述的很模糊,至少不够清楚,什么意思?如果你将一张照片设置为锁屏和桌面壁纸,两者共享同一个文件,那么通过调用
ParcelFileDescriptor pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);
pfd
永远为null,那么你应该这样获取锁屏壁纸:
if (pfd == null)
pfd = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);
您将获得非空 pfd
。是这样的no lock-specific wallpaper has been configured.
相反,lock-specific wallpaper has been configured
如果直接将照片设置为锁屏壁纸,wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM)
将return一个非空值。
所以这是我用来检索锁屏壁纸的代码:
/**
* 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();
}
不要忘记在清单文件中添加READ_EXTERNAL_STORAGE
并在外部授予它。