1# @ohos.wallpaper (Wallpaper) 2 3The **wallpaper** module provides APIs for switching between wallpapers. Since API version 9, the APIs of this module function as system APIs, and only system applications are allowed to switch between wallpapers. Applications that use the wallpaper, for example, the home screen, need to subscribe to wallpaper changes and update the wallpaper accordingly. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10## Modules to Import 11 12 13```ts 14import { wallpaper } from '@kit.BasicServicesKit'; 15``` 16 17## WallpaperType<sup>7+</sup> 18 19Enumerates the wallpaper types. 20 21**System capability**: SystemCapability.MiscServices.Wallpaper 22 23| Name| Value|Description| 24| -------- | -------- |-------- | 25| WALLPAPER_SYSTEM | 0 |Home screen wallpaper.| 26| WALLPAPER_LOCKSCREEN | 1 |Lock screen wallpaper.| 27 28 29## RgbaColor<sup>(deprecated)</sup> 30 31Defines the RGBA color space for the wallpaper. 32 33> **NOTE** 34> 35> This API is supported since API version 7 and deprecated since API version 9. 36 37**System capability**: SystemCapability.MiscServices.Wallpaper 38 39| Name| Type| Readable| Writable| Description| 40| -------- | -------- | -------- | -------- | -------- | 41| red | number | Yes| Yes| Red color. The value ranges from 0 to 255.| 42| green | number | Yes| Yes| Green color. The value ranges from 0 to 255.| 43| blue | number | Yes| Yes| Blue color. The value ranges from 0 to 255.| 44| alpha | number | Yes| Yes| Alpha value. The value ranges from 0 to 255.| 45 46 47## wallpaper.on('colorChange')<sup>(deprecated)</sup> 48 49on(type: 'colorChange', callback: (colors: Array<RgbaColor>, wallpaperType: WallpaperType) => void): void 50 51Subscribes to the wallpaper color change event. Multi-thread concurrent calls are not supported. 52 53> **NOTE** 54> 55> This API is supported since API version 7 and deprecated since API version 9. 56 57**System capability**: SystemCapability.MiscServices.Wallpaper 58 59**Parameters** 60 61| Name| Type| Mandatory| Description| 62| -------- | -------- | -------- | -------- | 63| type | string | Yes| Type of the event to subscribe to. The value **'colorChange'** indicates subscribing to the wallpaper color change event.| 64| callback | function | Yes| Callback triggered when the wallpaper color changes. The wallpaper type and main colors are returned.<br>- colors<br> Main color of the wallpaper. For details, see [RgbaColor](#rgbacolordeprecated).<br>- wallpaperType<br> Wallpaper type.| 65 66**Example** 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<RgbaColor>, wallpaperType: WallpaperType) => void): void 82 83Unsubscribes from the wallpaper color change event. Multi-thread concurrent calls are not supported. 84 85> **NOTE** 86> 87> This API is supported since API version 7 and deprecated since API version 9. 88 89**System capability**: SystemCapability.MiscServices.Wallpaper 90 91**Parameters** 92 93| Name| Type| Mandatory| Description| 94| -------- | -------- | -------- | -------- | 95| type | string | Yes| Type of the event to unsubscribe from. The value **'colorChange'** indicates unsubscribing from the wallpaper color change event.| 96| callback | function | No| Callback used for unsubscription. If this parameter is not set, this API unsubscribes from all callbacks of the specified event type.<br>- colors<br> Main color of the wallpaper. For details, see [RgbaColor](#rgbacolordeprecated).<br>- wallpaperType<br> Wallpaper type.| 97 98**Example** 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 // Unsubscribe from the listener. 112 wallpaper.off('colorChange', listener); 113} catch (error) { 114 console.error(`failed to off because: ${JSON.stringify(error)}`); 115} 116 117try { 118 // Unsubscribe from all subscriptions of the colorChange type. 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<Array<RgbaColor>>): void 128 129Obtains the main color information of the wallpaper of the specified type. 130 131> **NOTE** 132> 133> This API is supported since API version 7 and deprecated since API version 9. 134 135**System capability**: SystemCapability.MiscServices.Wallpaper 136 137**Parameters** 138 139| Name| Type| Mandatory| Description| 140| -------- | -------- | -------- | -------- | 141| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 142| callback | AsyncCallback<Array<[RgbaColor](#rgbacolordeprecated)>> | Yes| Callback used to return the main color information of the wallpaper.| 143 144**Example** 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<Array<RgbaColor>> 161 162Obtains the main color information of the wallpaper of the specified type. 163 164> **NOTE** 165> 166> This API is supported since API version 7 and deprecated since API version 9. 167 168**System capability**: SystemCapability.MiscServices.Wallpaper 169 170**Parameters** 171 172| Name| Type| Mandatory| Description| 173| -------- | -------- | -------- | -------- | 174| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 175 176**Return value** 177 178| Type| Description| 179| -------- | -------- | 180| Promise<Array<[RgbaColor](#rgbacolordeprecated)>> | Promise used to return the main color information of the wallpaper.| 181 182**Example** 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<number>): void 197 198Obtains the ID of the wallpaper of the specified type. 199 200> **NOTE** 201> 202> This API is supported since API version 7 and deprecated since API version 9. 203 204**System capability**: SystemCapability.MiscServices.Wallpaper 205 206**Parameters** 207 208| Name| Type| Mandatory| Description| 209| -------- | -------- | -------- | -------- | 210| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 211| callback | AsyncCallback<number> | Yes| Callback used to return the wallpaper ID. If the wallpaper of the specified type is configured, a number greater than or equal to **0** is returned. Otherwise, **-1** is returned. The value ranges from -1 to (2^31-1).| 212 213**Example** 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<number> 230 231Obtains the ID of the wallpaper of the specified type. 232 233> **NOTE** 234> 235> This API is supported since API version 7 and deprecated since API version 9. 236 237**System capability**: SystemCapability.MiscServices.Wallpaper 238 239**Parameters** 240 241| Name| Type| Mandatory| Description| 242| -------- | -------- | -------- | -------- | 243| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 244 245**Return value** 246 247| Type| Description| 248| -------- | -------- | 249| Promise<number> | Promise used to return the wallpaper ID. If this type of wallpaper is configured, a number greater than or equal to **0** is returned. Otherwise, **-1** is returned. The value ranges from -1 to (2^31-1).| 250 251**Example** 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<number>): void 266 267Obtains the minimum height of this wallpaper. 268 269> **NOTE** 270> 271> This API is supported since API version 7 and deprecated since API version 9. 272 273**System capability**: SystemCapability.MiscServices.Wallpaper 274 275**Parameters** 276 277| Name| Type| Mandatory| Description| 278| -------- | -------- | -------- | -------- | 279| callback | AsyncCallback<number> | Yes| Callback used to return the minimum wallpaper height, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default height should be used instead.| 280 281**Example** 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<number> 298 299Obtains the minimum height of this wallpaper. 300 301> **NOTE** 302> 303> This API is supported since API version 7 and deprecated since API version 9. 304 305**System capability**: SystemCapability.MiscServices.Wallpaper 306 307**Return value** 308 309| Type| Description| 310| -------- | -------- | 311| Promise<number> | Promise used to return the minimum wallpaper height, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default height should be used instead.| 312 313**Example** 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<number>): void 328 329Obtains the minimum width of this wallpaper. 330 331> **NOTE** 332> 333> This API is supported since API version 7 and deprecated since API version 9. 334 335**System capability**: SystemCapability.MiscServices.Wallpaper 336 337**Parameters** 338 339| Name| Type| Mandatory| Description| 340| -------- | -------- | -------- | -------- | 341| callback | AsyncCallback<number> | Yes| Callback used to return the minimum wallpaper width, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default width should be used instead.| 342 343**Example** 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<number> 360 361Obtains the minimum width of this wallpaper. 362 363> **NOTE** 364> 365> This API is supported since API version 7 and deprecated since API version 9. 366 367**System capability**: SystemCapability.MiscServices.Wallpaper 368 369**Return value** 370 371| Type| Description| 372| -------- | -------- | 373| Promise<number> | Promise used to return the minimum wallpaper width, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default width should be used instead.| 374 375**Example** 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<number>): void 390 391Obtains the wallpaper of the specified type. 392 393> **NOTE** 394> 395> This API is supported since API version 8 and deprecated since API version 9. 396 397**Required permissions**: ohos.permission.GET_WALLPAPER 398 399**System capability**: SystemCapability.MiscServices.Wallpaper 400 401**Parameters** 402 403| Name| Type| Mandatory| Description| 404| -------- | -------- | -------- | -------- | 405| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 406| callback | AsyncCallback<number> | Yes| Callback used to return the result. If the operation is successful, the file descriptor ID to the wallpaper is returned. Otherwise, error information is returned.| 407 408**Example** 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<number> 425 426Obtains the wallpaper of the specified type. 427 428> **NOTE** 429> 430> This API is supported since API version 8 and deprecated since API version 9. 431 432**Required permissions**: ohos.permission.GET_WALLPAPER 433 434**System capability**: SystemCapability.MiscServices.Wallpaper 435 436**Parameters** 437 438| Name| Type| Mandatory| Description| 439| -------- | -------- | -------- | -------- | 440| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 441 442**Return value** 443 444| Type| Description| 445| -------- | -------- | 446| Promise<number> | Promise used to return the result. If the operation is successful, the file descriptor ID to the wallpaper is returned. Otherwise, error information is returned.| 447 448**Example** 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<boolean>): void 463 464Checks whether to allow the application to change the wallpaper for the current user. 465 466> **NOTE** 467> 468> This API is supported since API version 7 and deprecated since API version 9. 469 470**System capability**: SystemCapability.MiscServices.Wallpaper 471 472**Parameters** 473 474| Name| Type| Mandatory| Description| 475| -------- | -------- | -------- | -------- | 476| callback | AsyncCallback<boolean> | Yes| Callback used to return whether to allow the application to change the wallpaper for the current user. The value **true** means that the operation is allowed, and **false** means the opposite.| 477 478**Example** 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<boolean> 495 496Checks whether to allow the application to change the wallpaper for the current user. 497 498> **NOTE** 499> 500> This API is supported since API version 7 and deprecated since API version 9. 501 502**System capability**: SystemCapability.MiscServices.Wallpaper 503 504**Return value** 505 506| Type| Description| 507| -------- | -------- | 508| Promise<boolean> | Promise used to return whether to allow the application to change the wallpaper for the current user. The value **true** means that the operation is allowed, and **false** means the opposite.| 509 510**Example** 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<boolean>): void 525 526Checks whether the user is allowed to set wallpapers. 527 528> **NOTE** 529> 530> This API is supported since API version 7 and deprecated since API version 9. 531 532**System capability**: SystemCapability.MiscServices.Wallpaper 533 534**Parameters** 535 536| Name| Type| Mandatory| Description| 537| -------- | -------- | -------- | -------- | 538| callback | AsyncCallback<boolean> | Yes| Callback used to return whether the user is allowed to set wallpapers. The value **true** means that the operation is allowed, and **false** means the opposite.| 539 540**Example** 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<boolean> 557 558Checks whether the user is allowed to set wallpapers. 559 560> **NOTE** 561> 562> This API is supported since API version 7 and deprecated since API version 9. 563 564**System capability**: SystemCapability.MiscServices.Wallpaper 565 566**Return value** 567 568| Type| Description| 569| -------- | -------- | 570| Promise<boolean> | Promise used to return whether the user is allowed to set wallpapers. The value **true** means that the operation is allowed, and **false** means the opposite.| 571 572**Example** 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<void>): void 587 588Resets the wallpaper of the specified type to the default wallpaper. 589 590> **NOTE** 591> 592> This API is supported since API version 7 and deprecated since API version 9. 593 594**Required permissions**: ohos.permission.SET_WALLPAPER 595 596**System capability**: SystemCapability.MiscServices.Wallpaper 597 598**Parameters** 599 600| Name| Type| Mandatory| Description| 601| -------- | -------- | -------- | -------- | 602| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 603| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the wallpaper is reset, **err** is **undefined**. Otherwise, **err** is an error object.| 604 605**Example** 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<void> 622 623Resets the wallpaper of the specified type to the default wallpaper. 624 625> **NOTE** 626> 627> This API is supported since API version 7 and deprecated since API version 9. 628 629**Required permissions**: ohos.permission.SET_WALLPAPER 630 631**System capability**: SystemCapability.MiscServices.Wallpaper 632 633**Parameters** 634 635| Name| Type| Mandatory| Description| 636| -------- | -------- | -------- | -------- | 637| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 638 639**Return value** 640 641| Type| Description| 642| -------- | -------- | 643| Promise<void> | Promise that returns no value.| 644 645**Example** 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<void>): void 660 661Sets a specified source as the wallpaper of a specified type. 662 663> **NOTE** 664> 665> This API is supported since API version 7 and deprecated since API version 9. 666 667**Required permissions**: ohos.permission.SET_WALLPAPER 668 669**System capability**: SystemCapability.MiscServices.Wallpaper 670 671**Parameters** 672 673| Name| Type| Mandatory| Description| 674| -------- | -------- | -------- | -------- | 675| source | string \| [image.PixelMap](../apis-image-kit/js-apis-image.md) | Yes| URI of a JPEG or PNG file, or pixel map of a PNG file.| 676| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 677| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the wallpaper is set, **err** is **undefined**. Otherwise, **err** is an error object.| 678 679**Example** 680 681```ts 682import { BusinessError } from '@kit.BasicServicesKit'; 683import { image } from '@kit.ImageKit'; 684 685// The source type is 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// The source type is 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<void> 719 720Sets a specified source as the wallpaper of a specified type. 721 722> **NOTE** 723> 724> This API is supported since API version 7 and deprecated since API version 9. 725 726**Required permissions**: ohos.permission.SET_WALLPAPER 727 728**System capability**: SystemCapability.MiscServices.Wallpaper 729 730**Parameters** 731 732| Name| Type| Mandatory| Description| 733| -------- | -------- | -------- | -------- | 734| source | string \| [image.PixelMap](../apis-image-kit/js-apis-image.md) | Yes| URI of a JPEG or PNG file, or pixel map of a PNG file.| 735| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.| 736 737**Return value** 738 739| Type| Description| 740| -------- | -------- | 741| Promise<void> | Promise that returns no value.| 742 743**Example** 744 745```ts 746import { BusinessError } from '@kit.BasicServicesKit'; 747import { image } from '@kit.ImageKit'; 748 749// The source type is 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// The source type is 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``` 775