1# @ohos.wallpaper (壁纸)
2
3壁纸管理服务为OpenHarmony系统服务,提供壁纸切换功能。从API 9开始壁纸管理的接口调整为系统API,壁纸的切换只能通过系统应用来完成。壁纸管理提供壁纸切换通道,使用壁纸的应用(如:桌面)需订阅壁纸变化通知并刷新壁纸显示。
4
5> **说明:**
6>
7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9
10## 导入模块
11
12
13```ts
14import { wallpaper } from '@kit.BasicServicesKit';
15```
16
17## WallpaperType<sup>7+</sup>
18
19定义壁纸的枚举类型。
20
21**系统能力**: SystemCapability.MiscServices.Wallpaper
22
23| 名称 | 值 |说明 |
24| -------- | -------- |-------- |
25| WALLPAPER_SYSTEM | 0 |主屏幕壁纸标识。 |
26| WALLPAPER_LOCKSCREEN | 1 |锁屏壁纸标识。 |
27
28
29## RgbaColor<sup>(deprecated)</sup>
30
31定义壁纸颜色信息结构。
32
33> **说明:**
34>
35> 从 API version 7开始支持,从API version 9开始废弃。
36
37**系统能力**: SystemCapability.MiscServices.Wallpaper
38
39| 名称 | 类型 | 可读 | 可写 | 说明 |
40| -------- | -------- | -------- | -------- | -------- |
41| red | number | 是 | 是 | 表示红色值,范围为 0 到 255。 |
42| green | number | 是 | 是 | 表示绿色值,范围为 0 到 255。 |
43| blue | number | 是 | 是 | 表示蓝色值,范围为 0 到 255。 |
44| alpha | number | 是 | 是 | 表示 alpha 值,范围为 0 到 255。 |
45
46
47## wallpaper.on('colorChange')<sup>(deprecated)</sup>
48
49on(type: 'colorChange', callback: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
50
51订阅壁纸颜色变化结果上报事件。不支持多线程并发调用。
52
53> **说明:**
54>
55> 从 API version 7开始支持,从API version 9开始废弃。
56
57**系统能力**: SystemCapability.MiscServices.Wallpaper
58
59**参数:**
60
61| 参数名 | 类型 | 必填 | 说明 |
62| -------- | -------- | -------- | -------- |
63| type | string | 是 | 取值为'colorChange',表示壁纸颜色变化结果上报事件。 |
64| callback | function | 是 | 壁纸颜色变化触发该回调方法,返回壁纸类型和壁纸的主要颜色信息。<br/>- colors<br/>  壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolordeprecated)。<br/>- wallpaperType<br/>  壁纸类型。 |
65
66**示例:**
67
68```ts
69try {
70    let listener = (colors: Array<wallpaper.RgbaColor>, wallpaperType: wallpaper.WallpaperType): void => {
71        console.log(`wallpaper color changed.`);
72    };
73    wallpaper.on('colorChange', listener);
74} catch (error) {
75    console.error(`failed to on because: ${JSON.stringify(error)}`);
76}
77```
78
79## wallpaper.off('colorChange')<sup>(deprecated)</sup>
80
81off(type: 'colorChange', callback?: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
82
83取消订阅壁纸颜色变化结果上报事件。不支持多线程并发调用。
84
85> **说明:**
86>
87> 从 API version 7开始支持,从API version 9开始废弃。
88
89**系统能力**: SystemCapability.MiscServices.Wallpaper
90
91**参数:**
92
93| 参数名 | 类型 | 必填 | 说明 |
94| -------- | -------- | -------- | -------- |
95| type | string | 是 | 取值为'colorChange',表示取消订阅壁纸颜色变化结果上报事件。 |
96| callback | function | 否 |   表示要取消的壁纸颜色变化的回调,不填写该参数则取消订阅该type对应的所有回调。<br/>- colors<br/>  壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolordeprecated)。<br/>- wallpaperType<br/>  壁纸类型。 |
97
98**示例:**
99
100```ts
101let listener = (colors: Array<wallpaper.RgbaColor>, wallpaperType: wallpaper.WallpaperType): void => {
102    console.log(`wallpaper color changed.`);
103};
104try {
105    wallpaper.on('colorChange', listener);
106} catch (error) {
107    console.error(`failed to on because: ${JSON.stringify(error)}`);
108}
109
110try {
111    // 取消订阅listener
112    wallpaper.off('colorChange', listener);
113} catch (error) {
114    console.error(`failed to off because: ${JSON.stringify(error)}`);
115}
116
117try {
118    // 取消所有'colorChange'类型的订阅
119    wallpaper.off('colorChange');
120} catch (error) {
121    console.error(`failed to off because: ${JSON.stringify(error)}`);
122}
123```
124
125## wallpaper.getColors<sup>(deprecated)</sup>
126
127getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;RgbaColor&gt;&gt;): void
128
129获取指定类型壁纸的主要颜色信息。
130
131> **说明:**
132>
133> 从 API version 7开始支持,从API version 9开始废弃。
134
135**系统能力**: SystemCapability.MiscServices.Wallpaper
136
137**参数:**
138
139| 参数名 | 类型 | 必填 | 说明 |
140| -------- | -------- | -------- | -------- |
141| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
142| callback | AsyncCallback&lt;Array&lt;[RgbaColor](#rgbacolordeprecated)&gt;&gt; | 是 | 回调函数,返回壁纸的主要颜色信息。 |
143
144**示例:**
145
146```ts
147import { BusinessError } from '@kit.BasicServicesKit';
148
149wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: Array<wallpaper.RgbaColor>) => {
150    if (error) {
151        console.error(`failed to getColors because: ${JSON.stringify(error)}`);
152        return;
153    }
154    console.log(`success to getColors: ${JSON.stringify(data)}`);
155});
156```
157
158## wallpaper.getColors<sup>(deprecated)</sup>
159
160getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt;
161
162获取指定类型壁纸的主要颜色信息。
163
164> **说明:**
165>
166> 从 API version 7开始支持,从API version 9开始废弃。
167
168**系统能力**: SystemCapability.MiscServices.Wallpaper
169
170**参数:**
171
172| 参数名 | 类型 | 必填 | 说明 |
173| -------- | -------- | -------- | -------- |
174| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
175
176**返回值:**
177
178| 类型 | 说明 |
179| -------- | -------- |
180| Promise&lt;Array&lt;[RgbaColor](#rgbacolordeprecated)&gt;&gt; | 返回壁纸的主要颜色信息。 |
181
182**示例:**
183
184```ts
185import { BusinessError } from '@kit.BasicServicesKit';
186
187wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: Array<wallpaper.RgbaColor>) => {
188    console.log(`success to getColors: ${JSON.stringify(data)}`);
189  }).catch((error: BusinessError) => {
190    console.error(`failed to getColors because: ${JSON.stringify(error)}`);
191});
192```
193
194## wallpaper.getId<sup>(deprecated)</sup>
195
196getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void
197
198获取指定类型壁纸的ID。
199
200> **说明:**
201>
202> 从 API version 7开始支持,从API version 9开始废弃。
203
204**系统能力**: SystemCapability.MiscServices.Wallpaper
205
206**参数:**
207
208| 参数名 | 类型 | 必填 | 说明 |
209| -------- | -------- | -------- | -------- |
210| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
211| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的ID。如果配置了指定类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。 |
212
213**示例:**
214
215```ts
216import { BusinessError } from '@kit.BasicServicesKit';
217
218wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: Number) => {
219    if (error) {
220        console.error(`failed to getId because: ${JSON.stringify(error)}`);
221        return;
222    }
223    console.log(`success to getId: ${JSON.stringify(data)}`);
224});
225```
226
227## wallpaper.getId<sup>(deprecated)</sup>
228
229getId(wallpaperType: WallpaperType): Promise&lt;number&gt;
230
231获取指定类型壁纸的ID。
232
233> **说明:**
234>
235> 从 API version 7开始支持,从API version 9开始废弃。
236
237**系统能力**: SystemCapability.MiscServices.Wallpaper
238
239**参数:**
240
241| 参数名 | 类型 | 必填 | 说明 |
242| -------- | -------- | -------- | -------- |
243| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
244
245**返回值:**
246
247| 类型 | 说明 |
248| -------- | -------- |
249| Promise&lt;number&gt; | 壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。 |
250
251**示例:**
252
253```ts
254import { BusinessError } from '@kit.BasicServicesKit';
255
256wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: Number) => {
257    console.log(`success to getId: ${JSON.stringify(data)}`);
258  }).catch((error: BusinessError) => {
259    console.error(`failed to getId because: ${JSON.stringify(error)}`);
260});
261```
262
263## wallpaper.getMinHeight<sup>(deprecated)</sup>
264
265getMinHeight(callback: AsyncCallback&lt;number&gt;): void
266
267获取壁纸的最小高度值。
268
269> **说明:**
270>
271> 从 API version 7开始支持,从API version 9开始废弃。
272
273**系统能力**: SystemCapability.MiscServices.Wallpaper
274
275**参数:**
276
277| 参数名 | 类型 | 必填 | 说明 |
278| -------- | -------- | -------- | -------- |
279| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
280
281**示例:**
282
283```ts
284import { BusinessError } from '@kit.BasicServicesKit';
285
286wallpaper.getMinHeight((error: BusinessError, data: Number) => {
287    if (error) {
288        console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
289        return;
290    }
291    console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
292});
293```
294
295## wallpaper.getMinHeight<sup>(deprecated)</sup>
296
297getMinHeight(): Promise&lt;number&gt;
298
299获取壁纸的最小高度值。
300
301> **说明:**
302>
303> 从 API version 7开始支持,从API version 9开始废弃。
304
305**系统能力**: SystemCapability.MiscServices.Wallpaper
306
307**返回值:**
308
309| 类型 | 说明 |
310| -------- | -------- |
311| Promise&lt;number&gt; | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
312
313**示例:**
314
315```ts
316import { BusinessError } from '@kit.BasicServicesKit';
317
318wallpaper.getMinHeight().then((data: Number) => {
319    console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
320}).catch((error: BusinessError) => {
321    console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
322});
323```
324
325## wallpaper.getMinWidth<sup>(deprecated)</sup>
326
327getMinWidth(callback: AsyncCallback&lt;number&gt;): void
328
329获取壁纸的最小宽度值。
330
331> **说明:**
332>
333> 从 API version 7开始支持,从API version 9开始废弃。
334
335**系统能力**: SystemCapability.MiscServices.Wallpaper
336
337**参数:**
338
339| 参数名 | 类型 | 必填 | 说明 |
340| -------- | -------- | -------- | -------- |
341| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
342
343**示例:**
344
345```ts
346import { BusinessError } from '@kit.BasicServicesKit';
347
348wallpaper.getMinWidth((error: BusinessError, data: Number) => {
349    if (error) {
350        console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
351        return;
352    }
353    console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
354});
355```
356
357## wallpaper.getMinWidth<sup>(deprecated)</sup>
358
359getMinWidth(): Promise&lt;number&gt;
360
361获取壁纸的最小宽度值。
362
363> **说明:**
364>
365> 从 API version 7开始支持,从API version 9开始废弃。
366
367**系统能力**: SystemCapability.MiscServices.Wallpaper
368
369**返回值:**
370
371| 类型 | 说明 |
372| -------- | -------- |
373| Promise&lt;number&gt; | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
374
375**示例:**
376
377```ts
378import { BusinessError } from '@kit.BasicServicesKit';
379
380wallpaper.getMinWidth().then((data: Number) => {
381    console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
382  }).catch((error: BusinessError) => {
383    console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
384});
385```
386
387## wallpaper.getFile<sup>(deprecated)</sup>
388
389getFile(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void
390
391获取指定类型的壁纸文件。
392
393> **说明:**
394>
395> 从 API version 8开始支持,从API version 9开始废弃。
396
397**需要权限**:ohos.permission.GET_WALLPAPER
398
399**系统能力**: SystemCapability.MiscServices.Wallpaper
400
401**参数:**
402
403| 参数名 | 类型 | 必填 | 说明 |
404| -------- | -------- | -------- | -------- |
405| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
406| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
407
408**示例:**
409
410```ts
411import { BusinessError } from '@kit.BasicServicesKit';
412
413wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: number) => {
414    if (error) {
415        console.error(`failed to getFile because: ${JSON.stringify(error)}`);
416        return;
417    }
418    console.log(`success to getFile: ${JSON.stringify(data)}`);
419});
420```
421
422## wallpaper.getFile<sup>(deprecated)</sup>
423
424getFile(wallpaperType: WallpaperType): Promise&lt;number&gt;
425
426获取指定类型的壁纸文件。
427
428> **说明:**
429>
430> 从 API version 8开始支持,从API version 9开始废弃。
431
432**需要权限**:ohos.permission.GET_WALLPAPER
433
434**系统能力**: SystemCapability.MiscServices.Wallpaper
435
436**参数:**
437
438| 参数名 | 类型 | 必填 | 说明 |
439| -------- | -------- | -------- | -------- |
440| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
441
442**返回值:**
443
444| 类型 | 说明 |
445| -------- | -------- |
446| Promise&lt;number&gt; | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
447
448**示例:**
449
450```ts
451import { BusinessError } from '@kit.BasicServicesKit';
452
453wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: number) => {
454    console.log(`success to getFile: ${JSON.stringify(data)}`);
455  }).catch((error: BusinessError) => {
456    console.error(`failed to getFile because: ${JSON.stringify(error)}`);
457});
458```
459
460## wallpaper.isChangePermitted<sup>(deprecated)</sup>
461
462isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void
463
464是否允许应用改变当前用户的壁纸。
465
466> **说明:**
467>
468> 从 API version 7开始支持,从API version 9开始废弃。
469
470**系统能力**: SystemCapability.MiscServices.Wallpaper
471
472**参数:**
473
474| 参数名 | 类型 | 必填 | 说明 |
475| -------- | -------- | -------- | -------- |
476| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
477
478**示例:**
479
480```ts
481import { BusinessError } from '@kit.BasicServicesKit';
482
483wallpaper.isChangePermitted((error: BusinessError, data: Boolean) => {
484    if (error) {
485        console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
486        return;
487    }
488    console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
489});
490```
491
492## wallpaper.isChangePermitted<sup>(deprecated)</sup>
493
494isChangePermitted(): Promise&lt;boolean&gt;
495
496是否允许应用改变当前用户的壁纸。
497
498> **说明:**
499>
500> 从 API version 7开始支持,从API version 9开始废弃。
501
502**系统能力**: SystemCapability.MiscServices.Wallpaper
503
504**返回值:**
505
506| 类型 | 说明 |
507| -------- | -------- |
508| Promise&lt;boolean&gt; | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
509
510**示例:**
511
512```ts
513import { BusinessError } from '@kit.BasicServicesKit';
514
515wallpaper.isChangePermitted().then((data: Boolean) => {
516    console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
517}).catch((error: BusinessError) => {
518    console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
519});
520```
521
522## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
523
524isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void
525
526是否允许用户设置壁纸。
527
528> **说明:**
529>
530> 从 API version 7开始支持,从API version 9开始废弃。
531
532**系统能力**: SystemCapability.MiscServices.Wallpaper
533
534**参数:**
535
536| 参数名 | 类型 | 必填 | 说明 |
537| -------- | -------- | -------- | -------- |
538| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
539
540**示例:**
541
542```ts
543import { BusinessError } from '@kit.BasicServicesKit';
544
545wallpaper.isOperationAllowed((error: BusinessError, data: Boolean) => {
546    if (error) {
547        console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
548        return;
549    }
550    console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
551});
552```
553
554## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
555
556isOperationAllowed(): Promise&lt;boolean&gt;
557
558是否允许用户设置壁纸。
559
560> **说明:**
561>
562> 从 API version 7开始支持,从API version 9开始废弃。
563
564**系统能力**: SystemCapability.MiscServices.Wallpaper
565
566**返回值:**
567
568| 类型 | 说明 |
569| -------- | -------- |
570| Promise&lt;boolean&gt; | 异步回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
571
572**示例:**
573
574```ts
575import { BusinessError } from '@kit.BasicServicesKit';
576
577wallpaper.isOperationAllowed().then((data: Boolean) => {
578    console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
579  }).catch((error: BusinessError) => {
580    console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
581});
582```
583
584## wallpaper.reset<sup>(deprecated)</sup>
585
586reset(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
587
588移除指定类型的壁纸,恢复为默认显示的壁纸。
589
590> **说明:**
591>
592> 从 API version 7开始支持,从API version 9开始废弃。
593
594**需要权限**:ohos.permission.SET_WALLPAPER
595
596**系统能力**: SystemCapability.MiscServices.Wallpaper
597
598**参数:**
599
600| 参数名 | 类型 | 必填 | 说明 |
601| -------- | -------- | -------- | -------- |
602| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
603| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,移除壁纸成功,error为undefined,否则返回error信息。 |
604
605**示例:**
606
607```ts
608import { BusinessError } from '@kit.BasicServicesKit';
609
610wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
611    if (error) {
612        console.error(`failed to reset because: ${JSON.stringify(error)}`);
613        return;
614    }
615    console.log(`success to reset.`);
616});
617```
618
619## wallpaper.reset<sup>(deprecated)</sup>
620
621reset(wallpaperType: WallpaperType): Promise&lt;void&gt;
622
623移除指定类型的壁纸,恢复为默认显示的壁纸。
624
625> **说明:**
626>
627> 从 API version 7开始支持,从API version 9开始废弃。
628
629**需要权限**:ohos.permission.SET_WALLPAPER
630
631**系统能力**: SystemCapability.MiscServices.Wallpaper
632
633**参数:**
634
635| 参数名 | 类型 | 必填 | 说明 |
636| -------- | -------- | -------- | -------- |
637| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
638
639**返回值:**
640
641| 类型 | 说明 |
642| -------- | -------- |
643| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
644
645**示例:**
646
647```ts
648import { BusinessError } from '@kit.BasicServicesKit';
649
650wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
651    console.log(`success to reset.`);
652}).catch((error: BusinessError) => {
653    console.error(`failed to reset because: ${JSON.stringify(error)}`);
654});
655```
656
657## wallpaper.setWallpaper<sup>(deprecated)</sup>
658
659setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
660
661将指定资源设置为指定类型的壁纸。
662
663> **说明:**
664>
665> 从 API version 7开始支持,从API version 9开始废弃。
666
667**需要权限**:ohos.permission.SET_WALLPAPER
668
669**系统能力**: SystemCapability.MiscServices.Wallpaper
670
671**参数:**
672
673| 参数名 | 类型 | 必填 | 说明 |
674| -------- | -------- | -------- | -------- |
675| source | string \| [image.PixelMap](../apis-image-kit/js-apis-image.md) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
676| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
677| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 |
678
679**示例:**
680
681```ts
682import { BusinessError } from '@kit.BasicServicesKit';
683import { image } from '@kit.ImageKit';
684
685// source类型为string
686let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
687wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
688    if (error) {
689        console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
690       return;
691       }
692    console.log(`success to setWallpaper.`);
693});
694
695// source类型为image.PixelMap
696let imageSource = image.createImageSource("file://" + wallpaperPath);
697let opts: image.DecodingOptions = {
698    desiredSize: {
699        height: 3648,
700        width: 2736
701    }
702};
703imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
704    wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
705        if (error) {
706            console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
707            return;
708        }
709        console.log(`success to setWallpaper.`);
710    });
711}).catch((error: BusinessError) => {
712    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
713});
714```
715
716## wallpaper.setWallpaper<sup>(deprecated)</sup>
717
718setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;
719
720将指定资源设置为指定类型的壁纸。
721
722> **说明:**
723>
724> 从 API version 7开始支持,从API version 9开始废弃。
725
726**需要权限**:ohos.permission.SET_WALLPAPER
727
728**系统能力**: SystemCapability.MiscServices.Wallpaper
729
730**参数:**
731
732| 参数名 | 类型 | 必填 | 说明 |
733| -------- | -------- | -------- | -------- |
734| source | string \| [image.PixelMap](../apis-image-kit/js-apis-image.md) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
735| wallpaperType | [WallpaperType](#wallpapertype7) | 是 | 壁纸类型。 |
736
737**返回值:**
738
739| 类型 | 说明 |
740| -------- | -------- |
741| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
742
743**示例:**
744
745```ts
746import { BusinessError } from '@kit.BasicServicesKit';
747import { image } from '@kit.ImageKit';
748
749// source类型为string
750let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
751wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
752    console.log(`success to setWallpaper.`);
753  }).catch((error: BusinessError) => {
754    console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
755});
756
757// source类型为image.PixelMap
758let imageSource = image.createImageSource("file://" + wallpaperPath);
759let opts: image.DecodingOptions = {
760    desiredSize: {
761        height: 3648,
762        width: 2736
763    }
764};
765imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
766    wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
767        console.log(`success to setWallpaper.`);
768    }).catch((error: BusinessError) => {
769        console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
770    });
771  }).catch((error: BusinessError) => {
772    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
773});
774```