Android 设置系统桌面壁纸

本篇主要讲下如何设置系统桌面壁纸.

代码如下:

java 复制代码
    public static void setLauncherWallPaper(Context context, Bitmap bitmap) throws IOException {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            final int result;
            try {
                result = WallpaperManager.getInstance(context).setBitmap(bitmap, null, false, WallpaperManager.FLAG_SYSTEM);
            } catch (Throwable e) {
                throw new IOException(e);
            }
            if (result == 0) {
                throw new IOException("setLauncherWallPaper failed");
            }
        } else {
            try {
                WallpaperManager.getInstance(context).setBitmap(bitmap);
            } catch (Throwable e) {
                throw new IOException(e);
            }
        }
    }

本地调用如下:

java 复制代码
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.wallpaper);
        try {
            setLauncherWallPaper(this,bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
相关推荐
GISer_Jing3 分钟前
2025年Flutter与React Native对比
android·flutter·react native
MasterLi802329 分钟前
我的读书清单
android·linux·学习
怪兽20141 小时前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
彭同学学习日志1 小时前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
Gracker2 小时前
Android Perfetto 系列 9 - CPU 信息解读
android
Gracker2 小时前
Android Perfetto 系列 8:深入理解 Vsync 机制与性能分析
android
Gracker2 小时前
Android Perfetto 系列 07 - MainThread 和 RenderThread 解读
android
Gracker2 小时前
Android Perfetto 系列 5:Android App 基于 Choreographer 的渲染流程
android
Gracker3 小时前
Android Perfetto 系列 6:为什么是 120Hz?高刷新率的优势与挑战
android
apigfly3 小时前
Flutter,Compose,Web 在Android平台上从布局到屏幕的机制探究
android·flutter·webview