失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Android:获取当前的锁屏壁纸或桌面壁纸

Android:获取当前的锁屏壁纸或桌面壁纸

时间:2022-09-21 13:10:57

相关推荐

Android:获取当前的锁屏壁纸或桌面壁纸

使用WallpaperManager类

FLAG_LOCK为锁屏壁纸

FLAG_SYSTEM为桌面壁纸

//使用WallpaperManager类@TargetApi(Build.VERSION_CODES.N)private Bitmap getLockWallpaper(){WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);//获取WallpaperManager实例ParcelFileDescriptor mParcelFileDescriptor = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_LOCK);//获取锁屏壁纸if(mParcelFileDescriptor ==null){mParcelFileDescriptor = wallpaperManager.getWallpaperFile(WallpaperManager.FLAG_SYSTEM);//获取桌面壁纸//在未主动设置锁屏壁纸时,锁屏壁纸为null,默认取的是桌面壁纸,}FileDescriptor fileDescriptor = mParcelFileDescriptor.getFileDescriptor();Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);//获取Bitmap类型返回值try {mParcelFileDescriptor.close();} catch(Exception e) {android.util.Log.d(TAG,"mParcelFileDescriptor.close() error");}return image;}private void setBackground(){获取壁纸后设置为view的背景try {Drawable drawable =new BitmapDrawable(getLockWallpaper());//将Bitmap类型转换为Drawable类型mView.setBackgroundDrawable(drawable);//设置背景drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);//设置背景灰度} catch(Exception e) {android.util.Log.d(TAG,"set Background fail");}}

如果觉得《Android:获取当前的锁屏壁纸或桌面壁纸》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。