获取手机壁纸

//使用WallpaperManager类
    @TargetApi(Build.VERSION_CODES.N)
    private Bitmap getLockWallpaper(){
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);//获取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类型
            getWindow().getDecorView().setBackgroundDrawable(drawable);//设置背景
            drawable.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);//设置背景灰度
        } catch(Exception e) {
            android.util.Log.d("TAG","set Background fail");
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容