HarmonyOS鸿蒙Next中如何设置桌面壁纸和锁屏壁纸呢

HarmonyOS鸿蒙Next中如何设置桌面壁纸和锁屏壁纸呢 如何设置桌面壁纸和锁屏壁纸呢

4 回复

省流:系统应用企业设备才能设置桌面壁纸和锁屏壁纸

方式一:使用系统接口 @ohos.wallpaper (系统接口)

此方式需要应用为系统应用,并申请相应的权限。

1. 设置静态壁纸(图片)

使用 setImage 方法,支持传入文件路径 (string) 或像素图 (image.PixelMap)。

所需权限: ohos.permission.SET_WALLPAPER

设置桌面壁纸示例 (Callback方式):

import { wallpaper } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg"; // 图片沙箱路径
try {
    wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
        if (error) {
            console.error(`failed to setImage because: ${JSON.stringify(error)}`);
            return;
        }
        console.info(`success to setImage.`);
    });
} catch (error) {
    console.error(`failed to setImage because: ${JSON.stringify(error)}`);
}

设置锁屏壁纸示例 (Promise方式):

import { wallpaper } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
try {
    wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_LOCKSCREEN).then(() => {
        console.info(`success to setImage.`);
    }).catch((error: BusinessError) => {
        console.error(`failed to setImage because: ${JSON.stringify(error)}`);
    });
} catch (error) {
    console.error(`failed to setImage because: ${JSON.stringify(error)}`);
}

2. 设置动态壁纸(视频)

使用 setVideo 方法,传入MP4文件的Uri路径。

所需权限: ohos.permission.SET_WALLPAPER

设置桌面动态壁纸示例 (Promise方式):

import { wallpaper } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4";
try {
    wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.info(`success to setVideo.`);
    }).catch((error: BusinessError) => {
        console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
    });
} catch (error) {
    console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
}

3. 设置自定义壁纸包(压缩包)

使用 setCustomWallpaper 方法,传入ZIP资源包的路径。(此功能需要系统存在 com.ohos.sceneboard

所需权限: ohos.permission.SET_WALLPAPER

设置自定义壁纸包示例 (Promise方式):

import { wallpaper } from '@kit.BasicServicesKit';
import { BusinessError } from '@kit.BasicServicesKit';

let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip";
try {
    wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.info(`success to setCustomWallpaper.`);
    }).catch((error: BusinessError) => {
        console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
    });
} catch (error) {
    console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
}

方式二:使用企业设备管理接口 deviceSettings (仅对已激活的MDM应用)

此方式适用于企业场景,需要应用是已激活的设备管理应用。

所需权限: ohos.permission.ENTERPRISE_SET_WALLPAPER

设置桌面壁纸

使用 setHomeWallpaper 方法,传入文件描述符 (fd)。

示例:

import { deviceSettings } from '@kit.MDMKit';
import { common, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { fileIo as fs } from '@kit.CoreFileKit';

// 定义管理组件信息
let wantTemp: Want = {
  bundleName: 'com.example.myapplication', // 替换为实际包名
  abilityName: 'EntryAbility' // 替换为实际Ability名
};

// 获取文件描述符
const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let filename: string = "homewallpaper.jpg";
let filePath: string = context.filesDir + '/' + filename;
let fd: number = fs.openSync(filePath, fs.OpenMode.READ_WRITE).fd;

// 设置壁纸
deviceSettings.setHomeWallpaper(wantTemp, fd).then(() => {
  console.info('Succeeded in setting home wallpaper');
}).catch((err: BusinessError) => {
  console.error(`Failed to set home wallpaper. Code: ${err.code}, message: ${err.message}`);
});

设置锁屏壁纸

使用 setUnlockWallpaper 方法,传入文件描述符 (fd)。

接口说明: 方法与 setHomeWallpaper 类似,只需将接口名替换为 setUnlockWallpaper

重要说明

  1. 系统API限制@ohos.wallpaper 模块下的接口从API version 9开始均为系统接口,仅限系统应用调用。非系统应用调用会收到错误码 202
  2. 权限要求:所有设置壁纸的操作都需要相应的权限,否则会收到错误码 201
  3. 资源路径:使用系统接口时,源文件路径必须是应用的沙箱目录路径(如示例中的 /data/storage/el2/base/haps/entry/files/)。
  4. 企业设备管理deviceSettings 接口仅对已激活的设备管理应用 (MDM) 开放,普通应用无法调用。

更多关于HarmonyOS鸿蒙Next中如何设置桌面壁纸和锁屏壁纸呢的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


怪不得我试了好久那个第一个方式没反应,还以为没拿到pixelmap 呢~~,

在HarmonyOS Next中,进入“设置”>“桌面和壁纸”>“壁纸”,可选择系统预设壁纸或从相册添加自定义图片。设置时可分别选择应用为桌面壁纸、锁屏壁纸或同时设置。

在HarmonyOS Next中,设置桌面壁纸和锁屏壁纸的步骤如下:

  1. 进入设置:打开“设置”应用,选择“桌面和壁纸”选项。
  2. 选择壁纸类型:点击“壁纸”进入壁纸设置界面,可选择“桌面壁纸”或“锁屏壁纸”。
  3. 应用壁纸:从系统默认壁纸或相册中选择图片,调整位置和尺寸后点击“设为壁纸”即可完成设置。

桌面和锁屏壁纸可以分别设置,操作简单直观。

回到顶部