参考资料

​壁纸​

​Context模块​

api讲解

wallpaper.setWallpaper

setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise

将指定资源设置为指定类型的壁纸。

需要权限:ohos.permission.SET_WALLPAPER

系统能力: SystemCapability.MiscServices.Wallpaper

参数:

参数名

类型

必填

说明

source

string |PixelMap


JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。

wallpaperType

WallpaperType


壁纸类型。

返回值:

类型

说明

Promise

调用成功则返回是返回设置的结果,调用失败则返回error信息。

示例:

// source类型为string
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
console.log(`success to setWallpaper.`);
}).catch((error) => {
console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
});

【ARKUI】HarmonyOS 如何设置桌面壁纸_桌面壁纸

代码实现

准备工作

准备一张图片放在rawfile文件目录,如下图所示

申请权限

在config.json注册如下权限代码如下

"reqPermissions": [
{
"name": "ohos.permission.READ_USER_STORAGE"
},
{
"name": "ohos.permission.WRITE_USER_STORAGE"
},
{
"name": "ohos.permission.SET_WALLPAPER"
}
],
  • 【ARKUI】HarmonyOS 如何设置桌面壁纸_桌面壁纸_02

在mainAbility注册申请权限,并把权限写入指定文件夹目录下,代码如下

package com.newdemo.myapplication;

import ohos.aafwk.content.Operation;
import ohos.ace.ability.AceAbility;
import ohos.aafwk.content.Intent;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.security.SystemPermission;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainAbility extends AceAbility {
@Override
public void onStart(Intent intent) {
String[] permissions = {
"ohos.permission.WRITE_USER_STORAGE",
"ohos.permission.READ_USER_STORAGE",
" ohos.permission.SET_WALLPAPER"
};
requestPermissionsFromUser(permissions, 0);
super.onStart(intent);



writeToDisk( "entry/resources/rawfile/result.png","/result.png");
}

private void writeToDisk(String rawFilePath, String resultpath) {
String externalFilePath = getFilesDir() +resultpath ;
File file = new File(externalFilePath);
if (file.exists()) {
return;
}
RawFileEntry rawFileEntry = getResourceManager().getRawFileEntry(rawFilePath);
try (FileOutputStream outputStream = new FileOutputStream(new File(externalFilePath))) {
Resource resource = rawFileEntry.openRawFile();
// cache length
byte[] cache = new byte[1024];
int len = resource.read(cache);
while (len != -1) {
outputStream.write(cache, 0, len);
len = resource.read(cache);
}
} catch (IOException exception) {
}
}

}
  • 【ARKUI】HarmonyOS 如何设置桌面壁纸_ARKUI_03

Ets语言实现

在Ets绘画一个text文本内容为:设置壁纸,然后实现点击功能,代码如下

import image from '@ohos.multimedia.image'
import wallpaper from '@ohos.wallpaper';
import featureAbility from '@ohos.ability.featureAbility'
@Entry
@Component
struct Mypage {
public onclick() {

var context = featureAbility.getContext();
context.getFilesDir((err, data) => {
if (err) {
console.error('Operation failed. Cause: ' + JSON.stringify(err));
return;
}
let wallpaperPath =data+"/result.png";
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
if (error) {
console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
return;
}
console.log(`success to setWallpaper.`);
});
});
}

build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('设置壁纸')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(this.onclick.bind(this))
}
.width('100%')
.height('100%')
}
}
  • 【ARKUI】HarmonyOS 如何设置桌面壁纸_桌面壁纸_04

运行效果

【ARKUI】HarmonyOS 如何设置桌面壁纸_桌面壁纸_05

欲了解更多更全技术文章,欢迎访问​​https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh​