1# @ohos.wallpaper (Wallpaper) (System API) 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> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.wallpaper (Wallpaper)](js-apis-wallpaper.md). 9 10 11## Modules to Import 12 13 14```ts 15import { wallpaper } from '@kit.BasicServicesKit'; 16``` 17## WallpaperResourceType<sup>10+</sup> 18 19Enumerates the types of wallpaper resources. 20 21**System capability**: SystemCapability.MiscServices.Wallpaper 22 23**System API**: This is a system API. 24 25| Name| Value|Description| 26| -------- | -------- |-------- | 27| DEFAULT | 0 |Default type (image resource).| 28| PICTURE | 1 |Image resource.| 29| VIDEO | 2 |Video resource.| 30| PACKAGE | 3 |Package resource.| 31 32## FoldState<sup>14+</sup> 33 34Enumerates the types of the folding state of a device. 35 36**System capability**: SystemCapability.MiscServices.Wallpaper 37 38**System API**: This is a system API. 39 40| Name| Value|Description| 41| -------- | -------- |-------- | 42| NORMAL | 0 |Default state.| 43| UNFOLD_ONCE_STATE | 1 |Initial unfolded state.| 44| UNFOLD_TWICE_STATE | 2 |Secondary unfolded state.| 45 46## RotateState<sup>14+</sup> 47 48Enumerates the landscape or portrait mode of a device. 49 50**System capability**: SystemCapability.MiscServices.Wallpaper 51 52**System API**: This is a system API. 53 54| Name| Value|Description| 55| -------- | -------- |-------- | 56| PORTRAIT | 0 |Portrait mode (default).| 57| LANDSCAPE | 1 |Landscape mode.| 58 59## WallpaperInfo<sup>14+</sup> 60 61Defines the wallpaper information structure. 62 63**System capability**: SystemCapability.MiscServices.Wallpaper 64 65**System API**: This is a system API. 66 67| Name| Type| Description| 68| -------- | -------- | -------- | 69| [FoldState](#foldstate14) | enum | Folding state of a device.| 70| [RotateState](#rotatestate14) | enum | Landscape/portrait mode of a device.| 71| source | string | Wallpaper resource URI. Only the application sandbox directory is supported.| 72 73## wallpaper.setVideo<sup>10+</sup> 74 75setVideo(source: string, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 76 77Sets a video resource as the home screen wallpaper or lock screen wallpaper. This API uses an asynchronous callback to return the result. 78 79**Required permissions**: ohos.permission.SET_WALLPAPER 80 81**System capability**: SystemCapability.MiscServices.Wallpaper 82 83**System API**: This is a system API. 84 85**Parameters** 86 87| Name| Type| Mandatory| Description| 88| -------- | -------- | -------- | -------- | 89| source | string | Yes| URI of an MP4 file.| 90| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 91| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the wallpaper is set, **err** is **undefined**. Otherwise, **err** is an error object.| 92 93**Error codes** 94 95For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 96 97| **ID**| **Error Message** | 98| ------------ | ------------------------------------------- | 99| 201 | permission denied. | 100| 202 | permission verification failed, application which is not a system application uses system API. | 101| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 102 103**Example** 104 105```ts 106import { BusinessError } from '@kit.BasicServicesKit'; 107 108let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4"; 109try { 110 wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 111 if (error) { 112 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 113 return; 114 } 115 console.info(`success to setVideo.`); 116 }); 117} catch (error) { 118 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 119} 120 121``` 122 123## wallpaper.setVideo<sup>10+</sup> 124 125setVideo(source: string, wallpaperType: WallpaperType): Promise<void> 126 127Sets a video resource as the home screen wallpaper or lock screen wallpaper. This API uses a promise to return the result. 128 129**Required permissions**: ohos.permission.SET_WALLPAPER 130 131**System capability**: SystemCapability.MiscServices.Wallpaper 132 133**System API**: This is a system API. 134 135**Parameters** 136 137| Name| Type| Mandatory| Description| 138| -------- | -------- | -------- | -------- | 139| source | string | Yes| URI of an MP4 file.| 140| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 141 142**Error codes** 143 144For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 145 146| **ID**| **Error Message** | 147| ------------ | ------------------------------------------- | 148| 201 | permission denied. | 149| 202 | permission verification failed, application which is not a system application uses system API. | 150| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 151 152**Return value** 153 154| Type| Description| 155| -------- | -------- | 156| Promise<void> | Promise that returns no value.| 157 158**Example** 159 160```ts 161import { BusinessError } from '@kit.BasicServicesKit'; 162 163let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4"; 164try { 165 wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 166 console.info(`success to setVideo.`); 167 }).catch((error: BusinessError) => { 168 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 169 }); 170} catch (error) { 171 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 172} 173``` 174 175## wallpaper.setCustomWallpaper<sup>10+</sup> 176 177setCustomWallpaper(source: string, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 178 179Sets a specific ZIP file as the wallpaper. This API works only when **com.ohos.sceneboard** is set. Applications with the **ohos.permission.GET_WALLPAPER** permission have access to the **/data/wallpaper/** directory. This API uses an asynchronous callback to return the result. 180 181**Required permissions**: ohos.permission.SET_WALLPAPER 182 183**System capability**: SystemCapability.MiscServices.Wallpaper 184 185**System API**: This is a system API. 186 187**Parameters** 188 189| Name| Type| Mandatory| Description| 190| -------- | -------- | -------- | -------- | 191| source | string | Yes| ZIP file to set as the wallpaper.| 192| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 193| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the wallpaper is set, **err** is **undefined**. Otherwise, **err** is an error object.| 194 195**Error codes** 196 197For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 198 199| **ID**| **Error Message** | 200| ------------ | ------------------------------------------- | 201| 201 | permission denied. | 202| 202 | permission verification failed, application which is not a system application uses system API. | 203| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 204 205**Example** 206 207```ts 208import { BusinessError } from '@kit.BasicServicesKit'; 209 210let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip"; 211try { 212 wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 213 if (error) { 214 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 215 return; 216 } 217 console.info(`success to setCustomWallpaper.`); 218 }); 219} catch (error) { 220 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 221} 222 223``` 224 225## wallpaper.setCustomWallpaper<sup>10+</sup> 226 227setCustomWallpaper(source: string, wallpaperType: WallpaperType): Promise<void> 228 229Sets a specific ZIP file as the wallpaper. This API works only when **com.ohos.sceneboard** is set. Applications with the **ohos.permission.GET_WALLPAPER** permission have access to the **/data/wallpaper/** directory. This API uses a promise to return the result. 230 231**Required permissions**: ohos.permission.SET_WALLPAPER 232 233**System capability**: SystemCapability.MiscServices.Wallpaper 234 235**System API**: This is a system API. 236 237**Parameters** 238 239| Name| Type| Mandatory| Description| 240| -------- | -------- | -------- | -------- | 241| source | string | Yes| ZIP file to set as the wallpaper.| 242| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 243 244**Return value** 245 246| Type| Description| 247| -------- | -------- | 248| Promise<void> | Promise that returns no value.| 249 250**Error codes** 251 252For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 253 254| **ID**| **Error Message** | 255| ------------ | ------------------------------------------- | 256| 201 | permission denied. | 257| 202 | permission verification failed, application which is not a system application uses system API. | 258| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 259 260**Example** 261 262```ts 263import { BusinessError } from '@kit.BasicServicesKit'; 264 265let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip"; 266try { 267 wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 268 console.info(`success to setCustomWallpaper.`); 269 }).catch((error: BusinessError) => { 270 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 271 }); 272} catch (error) { 273 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 274} 275``` 276 277## wallpaper.on('wallpaperChange')<sup>10+</sup> 278 279on(type: 'wallpaperChange', callback: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) => void): void 280 281Subscribes to wallpaper change events. Multi-thread concurrent calls are not supported. 282 283**System capability**: SystemCapability.MiscServices.Wallpaper 284 285**System API**: This is a system API. 286 287**Parameters** 288 289| Name| Type| Mandatory| Description| 290| -------- | -------- | -------- | -------- | 291| type | string | Yes| Event type. The value is fixed at **'wallpaperChange'**.| 292| callback | function | Yes| Callback used to return the wallpaper type and wallpaper resource type.<br>- **wallpaperType**: wallpaper type.<br>- **resourceType**: wallpaper resource type.<br>- **uri**: URI of the wallpaper resource.| 293 294**Error codes** 295 296For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 297 298| **ID**| **Error Message** | 299| ------------ | ------------------------------------------- | 300| 202 | permission verification failed, application which is not a system application uses system API. | 301| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 302 303**Example** 304 305```ts 306try { 307 let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => { 308 console.info(`wallpaper color changed.`); 309 }; 310 wallpaper.on('wallpaperChange', listener); 311} catch (error) { 312 console.error(`failed to on because: ${JSON.stringify(error)}`); 313} 314``` 315 316## wallpaper.off('wallpaperChange')<sup>10+</sup> 317 318off(type: 'wallpaperChange', callback?: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) => void): void 319 320Unsubscribes from wallpaper change events. Multi-thread concurrent calls are not supported. 321 322**System capability**: SystemCapability.MiscServices.Wallpaper 323 324**System API**: This is a system API. 325 326**Parameters** 327 328| Name| Type| Mandatory| Description| 329| -------- | -------- | -------- | -------- | 330| type | string | Yes| Event type. The value is fixed at **'wallpaperChange'**.| 331| 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>- **wallpaperType**: wallpaper type.<br>- **resourceType**: wallpaper resource type.<br>- **uri**: URI of the wallpaper resource.| 332 333**Error codes** 334 335For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 336 337| **ID**| **Error Message** | 338| ------------ | ------------------------------------------- | 339| 202 | permission verification failed, application which is not a system application uses system API. | 340| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 341 342**Example** 343 344```ts 345let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => { 346 console.info(`wallpaper color changed.`); 347}; 348try { 349 wallpaper.on('wallpaperChange', listener); 350} catch (error) { 351 console.error(`failed to on because: ${JSON.stringify(error)}`); 352} 353 354try { 355 // Unsubscribe from the listener. 356 wallpaper.off('wallpaperChange', listener); 357} catch (error) { 358 console.error(`failed to off because: ${JSON.stringify(error)}`); 359} 360 361try { 362 // Unsubscribe from all callbacks of the 'wallpaperChange' event type. 363 wallpaper.off('wallpaperChange'); 364} catch (error) { 365 console.error(`failed to off because: ${JSON.stringify(error)}`); 366} 367``` 368 369## wallpaper.getColorsSync<sup>9+</sup> 370 371getColorsSync(wallpaperType: WallpaperType): Array<RgbaColor> 372 373Obtains the main color information of the wallpaper of the specified type. 374 375**System capability**: SystemCapability.MiscServices.Wallpaper 376 377**System API**: This is a system API. 378 379**Parameters** 380 381| Name| Type| Mandatory| Description| 382| -------- | -------- | -------- | -------- | 383| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 384 385**Return value** 386 387| Type| Description| 388| -------- | -------- | 389| Array<[RgbaColor](js-apis-wallpaper.md#rgbacolordeprecated)> | Promise used to return the main color information of the wallpaper.| 390 391**Error codes** 392 393For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 394 395| **ID**| **Error Message** | 396| ------------ | ------------------------------------------- | 397| 202 | permission verification failed, application which is not a system application uses system API. | 398| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 399 400**Example** 401 402```ts 403try { 404 let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM); 405 console.info(`success to getColorsSync: ${JSON.stringify(colors)}`); 406} catch (error) { 407 console.error(`failed to getColorsSync because: ${JSON.stringify(error)}`); 408} 409``` 410 411## wallpaper.getMinHeightSync<sup>9+</sup> 412 413getMinHeightSync(): number 414 415Obtains the minimum height of this wallpaper. 416 417**System capability**: SystemCapability.MiscServices.Wallpaper 418 419**System API**: This is a system API. 420 421**Return value** 422 423| Type| Description| 424| -------- | -------- | 425| 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.| 426 427**Error codes** 428 429For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 430 431| **ID**| **Error Message** | 432| ------------ | ------------------------------------------- | 433| 202 | permission verification failed, application which is not a system application uses system API. | 434 435**Example** 436 437```ts 438let minHeight = wallpaper.getMinHeightSync(); 439``` 440 441## wallpaper.getMinWidthSync<sup>9+</sup> 442 443getMinWidthSync(): number 444 445Obtains the minimum width of this wallpaper. 446 447**System capability**: SystemCapability.MiscServices.Wallpaper 448 449**System API**: This is a system API. 450 451**Return value** 452 453| Type| Description| 454| -------- | -------- | 455| 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.| 456 457**Error codes** 458 459For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 460 461| **ID**| **Error Message** | 462| ------------ | ------------------------------------------- | 463| 202 | permission verification failed, application which is not a system application uses system API. | 464 465**Example** 466 467```ts 468let minWidth = wallpaper.getMinWidthSync(); 469``` 470 471## wallpaper.restore<sup>9+</sup> 472 473restore(wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 474 475Resets the wallpaper of the specified type to the default wallpaper. This API uses an asynchronous callback to return the result. 476 477**Required permissions**: ohos.permission.SET_WALLPAPER 478 479**System capability**: SystemCapability.MiscServices.Wallpaper 480 481**System API**: This is a system API. 482 483**Parameters** 484 485| Name| Type| Mandatory| Description| 486| -------- | -------- | -------- | -------- | 487| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 488| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the wallpaper is reset, **err** is **undefined**. Otherwise, **err** is an error object.| 489 490**Error codes** 491 492For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 493 494| **ID**| **Error Message** | 495| ------------ | ------------------------------------------- | 496| 201 | permission denied. | 497| 202 | permission verification failed, application which is not a system application uses system API. | 498| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 499 500**Example** 501 502```ts 503import { BusinessError } from '@kit.BasicServicesKit'; 504 505wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 506 if (error) { 507 console.error(`failed to restore because: ${JSON.stringify(error)}`); 508 return; 509 } 510 console.info(`success to restore.`); 511}); 512``` 513 514## wallpaper.restore<sup>9+</sup> 515 516restore(wallpaperType: WallpaperType): Promise<void> 517 518Resets the wallpaper of the specified type to the default wallpaper. This API uses a promise to return the result. 519 520**Required permissions**: ohos.permission.SET_WALLPAPER 521 522**System capability**: SystemCapability.MiscServices.Wallpaper 523 524**System API**: This is a system API. 525 526**Parameters** 527 528| Name| Type| Mandatory| Description| 529| -------- | -------- | -------- | -------- | 530| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 531 532**Return value** 533 534| Type| Description| 535| -------- | -------- | 536| Promise<void> | Promise that returns no value.| 537 538**Error codes** 539 540For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 541 542| **ID**| **Error Message** | 543| ------------ | ------------------------------------------- | 544| 201 | permission denied. | 545| 202 | permission verification failed, application which is not a system application uses system API. | 546| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 547 548**Example** 549 550```ts 551import { BusinessError } from '@kit.BasicServicesKit'; 552 553wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 554 console.info(`success to restore.`); 555 }).catch((error: BusinessError) => { 556 console.error(`failed to restore because: ${JSON.stringify(error)}`); 557}); 558``` 559 560## wallpaper.setImage<sup>9+</sup> 561 562setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 563 564Sets a specified source as the wallpaper of a specified type. This API uses an asynchronous callback to return the result. 565 566**Required permissions**: ohos.permission.SET_WALLPAPER 567 568**System capability**: SystemCapability.MiscServices.Wallpaper 569 570**System API**: This is a system API. 571 572**Parameters** 573 574| Name| Type| Mandatory| Description| 575| -------- | -------- | -------- | -------- | 576| 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.| 577| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 578| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the wallpaper is set, **err** is **undefined**. Otherwise, **err** is an error object.| 579 580**Error codes** 581 582For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 583 584| **ID**| **Error Message** | 585| ------------ | ------------------------------------------- | 586| 201 | permission denied. | 587| 202 | permission verification failed, application which is not a system application uses system API. | 588| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 589 590**Example** 591 592```ts 593import { BusinessError } from '@kit.BasicServicesKit'; 594import { image } from '@kit.ImageKit'; 595 596// The source type is string. 597let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg"; 598wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 599 if (error) { 600 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 601 return; 602 } 603 console.info(`success to setImage.`); 604}); 605 606// The source type is image.PixelMap. 607let imageSource = image.createImageSource("file://" + wallpaperPath); 608let opts: image.DecodingOptions = { 609 desiredSize: { 610 height: 3648, 611 width: 2736 612 } 613}; 614imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => { 615 wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 616 if (error) { 617 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 618 return; 619 } 620 console.info(`success to setImage.`); 621 }); 622}).catch((error: BusinessError) => { 623 console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`); 624}); 625``` 626 627## wallpaper.setImage<sup>9+</sup> 628 629setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise<void> 630 631Sets a specified source as the wallpaper of a specified type. This API uses a promise to return the result. 632 633**Required permissions**: ohos.permission.SET_WALLPAPER 634 635**System capability**: SystemCapability.MiscServices.Wallpaper 636 637**System API**: This is a system API. 638 639**Parameters** 640 641| Name| Type| Mandatory| Description| 642| -------- | -------- | -------- | -------- | 643| 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.| 644| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 645 646**Return value** 647 648| Type| Description| 649| -------- | -------- | 650| Promise<void> | Promise that returns no value.| 651 652**Error codes** 653 654For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 655 656| **ID**| **Error Message** | 657| ------------ | ------------------------------------------- | 658| 201 | permission denied. | 659| 202 | permission verification failed, application which is not a system application uses system API. | 660| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 661 662**Example** 663 664```ts 665import { BusinessError } from '@kit.BasicServicesKit'; 666import { image } from '@kit.ImageKit'; 667 668// The source type is string. 669let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg"; 670wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 671 console.info(`success to setImage.`); 672}).catch((error: BusinessError) => { 673 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 674}); 675 676// The source type is image.PixelMap. 677let imageSource = image.createImageSource("file://" + wallpaperPath); 678let opts: image.DecodingOptions = { 679 desiredSize: { 680 height: 3648, 681 width: 2736 682 } 683}; 684imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => { 685 wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 686 console.info(`success to setImage.`); 687 }).catch((error: BusinessError) => { 688 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 689 }); 690}).catch((error: BusinessError) => { 691 console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`); 692}); 693``` 694 695## wallpaper.getImage<sup>9+</sup> 696 697getImage(wallpaperType: WallpaperType, callback: AsyncCallback<image.PixelMap>): void; 698 699Obtains the pixel map for the wallpaper of the specified type. This API only works for the static wallpaper set using **setImage**. This API uses an asynchronous callback to return the result. 700 701**Required permissions**: ohos.permission.GET_WALLPAPER 702 703**System capability**: SystemCapability.MiscServices.Wallpaper 704 705**System API**: This is a system API. 706 707**Parameters** 708 709| Name| Type| Mandatory| Description| 710| -------- | -------- | -------- | -------- | 711| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 712| callback | AsyncCallback<[image.PixelMap](../apis-image-kit/js-apis-image.md)> | Yes| Callback used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.| 713 714**Error codes** 715 716For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 717 718| **ID**| **Error Message** | 719| ------------ | ------------------------------------------- | 720| 201 | permission denied. | 721| 202 | permission verification failed, application which is not a system application uses system API. | 722| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 723 724**Example** 725 726```ts 727import { BusinessError } from '@kit.BasicServicesKit'; 728import { image } from '@kit.ImageKit'; 729 730wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => { 731 if (error) { 732 console.error(`failed to getImage because: ${JSON.stringify(error)}`); 733 return; 734 } 735 console.info(`success to getImage: ${JSON.stringify(data)}`); 736}); 737``` 738 739## wallpaper.getImage<sup>9+</sup> 740 741getImage(wallpaperType: WallpaperType): Promise<image.PixelMap> 742 743Obtains the pixel map for the wallpaper of the specified type. This API only works for the static wallpaper set using **setImage**. This API uses a promise to return the result. 744 745**Required permissions**: ohos.permission.GET_WALLPAPER 746 747**System capability**: SystemCapability.MiscServices.Wallpaper 748 749**System API**: This is a system API. 750 751**Parameters** 752 753| Name| Type| Mandatory| Description| 754| -------- | -------- | -------- | -------- | 755| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 756 757**Return value** 758 759| Type| Description| 760| -------- | -------- | 761| Promise<[image.PixelMap](../apis-image-kit/js-apis-image.md)> | Promise used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.| 762 763**Error codes** 764 765For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 766 767| **ID**| **Error Message** | 768| ------------ | ------------------------------------------- | 769| 201 | permission denied. | 770| 202 | permission verification failed, application which is not a system application uses system API. | 771| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 772 773**Example** 774 775```ts 776import { BusinessError } from '@kit.BasicServicesKit'; 777import { image } from '@kit.ImageKit'; 778 779wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => { 780 console.info(`success to getImage: ${JSON.stringify(data)}`); 781 }).catch((error: BusinessError) => { 782 console.error(`failed to getImage because: ${JSON.stringify(error)}`); 783}); 784``` 785## wallpaper.getWallpaperByState<sup>14+</sup> 786 787getWallpaperByState(wallpaperType:WallpaperType, foldState:FoldState, rotateState:RotateState): Promise<image.PixelMap> 788 789Obtains the pixel map of the wallpaper of a specific type, folding state, or landscape/portrait mode. If the specified wallpaper does not exist, the matching follows a degrading order: unfolded-land > unfolded-port > normal-port. This API uses a promise to return the result. 790 791**Required permissions**: ohos.permission.GET_WALLPAPER 792 793**System capability**: SystemCapability.MiscServices.Wallpaper 794 795**System API**: This is a system API. 796 797**Parameters** 798 799| Name| Type| Mandatory| Description| 800| -------- | -------- | -------- | -------- | 801| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 802| foldState | [FoldState](#foldstate14) | Yes| Folding state type.| 803| rotateState | [RotateState](#rotatestate14) | Yes| Landscape/portrait mode.| 804 805**Return value** 806 807| Type| Description| 808| -------- | -------- | 809| Promise<[image.PixelMap](../apis-image-kit/js-apis-image.md)> | Promise used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.| 810 811**Error codes** 812 813For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 814 815| **ID**| **Error Message** | 816| ------------ | ------------------------------------------- | 817| 201 | permission denied. | 818| 202 | permission verification failed, application which is not a system application uses system API. | 819| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed. | 820 821**Example** 822 823```ts 824import { BusinessError } from '@kit.BasicServicesKit'; 825import { wallpaper } from '@kit.BasicServicesKit'; 826import { image } from '@kit.ImageKit'; 827 828wallpaper.getWallpaperByState(wallpaper.WallpaperType.WALLPAPER_SYSTEM,wallpaper.FoldState.NORMAL,wallpaper.RotateState.PORTRAIT).then((data:image.PixelMap) => { 829 console.info(`success to getWallpaperByState: ${JSON.stringify(data)}`); 830}).catch((error: BusinessError) => { 831 console.error(`failed to getWallpaperByState because: ${JSON.stringify(error)}`); 832}); 833``` 834 835## wallpaper.setAllWallpapers<sup>14+</sup> 836 837setAllWallpapers(wallpaperInfos: Array\<WallpaperInfo>\, wallpaperType: WallpaperType): Promise<void> 838 839Sets all wallpaper to a specific folding state, landscape/portrait mode, and resource path, where **wallpaper.FoldState.NORMAL** and **wallpaper.RotateState.PORTRAIT** are mandatory. This API uses a promise to return the result. 840 841**Required permissions**: ohos.permission.SET_WALLPAPER 842 843**System capability**: SystemCapability.MiscServices.Wallpaper 844 845**System API**: This is a system API. 846 847**Parameters** 848 849| Name| Type| Mandatory| Description| 850| -------- | -------- | -------- | -------- | 851| wallpaperInfos | Array<[WallpaperInfo](#wallpaperinfo14)> | Yes| Information structure of all wallpapers.| 852| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 853 854**Return value** 855 856| Type| Description| 857| -------- | -------- | 858| Promise<void> | Promise that returns no value.| 859 860**Error codes** 861 862For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 863 864| **ID**| **Error Message** | 865| ------------ | ------------------------------------------- | 866| 201 | permission denied. | 867| 202 | permission verification failed, application which is not a system application uses system API. | 868| 401 | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.| 869 870**Example** 871 872```ts 873import { BusinessError } from '@kit.BasicServicesKit'; 874import { wallpaper } from '@kit.BasicServicesKit'; 875 876let wallpaperInfos: Array<wallpaper.WallpaperInfo> 877wallpaperInfos = [ 878 { 879 foldState: wallpaper.FoldState.NORMAL, 880 rotateState: wallpaper.RotateState.PORTRAIT, 881 source: '/data/storage/el2/base/haps/entry/files/normal.jpeg' 882 }, 883 { 884 foldState: wallpaper.FoldState.UNFOLD_ONCE_STATE, 885 rotateState: wallpaper.RotateState.LANDSCAPE, 886 source: '/data/storage/el2/base/haps/entry/files/unfold_once_state.jpeg' 887 }, 888 { 889 foldState: wallpaper.FoldState.UNFOLD_TWICE_STATE, 890 rotateState: wallpaper.RotateState.PORTRAIT, 891 source: '/data/storage/el2/base/haps/entry/files/unfold_twice_state.jpeg' 892 } 893]; 894wallpaper.setAllWallpapers(wallpaperInfos, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 895 console.info(`success to setAllWallpapers.`); 896}).catch((error: BusinessError) => { 897 console.error(`failed to setAllWallpapers because: ${JSON.stringify(error)}`); 898}); 899``` 900 901## wallpaper.getPixelMap<sup>(deprecated)</sup> 902 903getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback<image.PixelMap>): void; 904 905Obtains the pixel map for the wallpaper of the specified type. 906 907> **NOTE** 908> 909> This API is supported since API version 7 and deprecated since API version 9. 910 911**Required permissions**: ohos.permission.GET_WALLPAPER 912 913**System capability**: SystemCapability.MiscServices.Wallpaper 914 915**System API**: This is a system API. 916 917**Parameters** 918 919| Name| Type| Mandatory| Description| 920| -------- | -------- | -------- | -------- | 921| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 922| callback | AsyncCallback<image.PixelMap> | Yes| Callback used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.| 923 924**Example** 925 926```ts 927import { BusinessError } from '@kit.BasicServicesKit'; 928import { image } from '@kit.ImageKit'; 929 930wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => { 931 if (error) { 932 console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`); 933 return; 934 } 935 console.info(`success to getPixelMap : ${JSON.stringify(data)}`); 936 }); 937``` 938 939## wallpaper.getPixelMap<sup>(deprecated)</sup> 940 941getPixelMap(wallpaperType: WallpaperType): Promise<image.PixelMap> 942 943Obtains the pixel map for the wallpaper of the specified type. 944 945> **NOTE** 946> 947> This API is supported since API version 7 and deprecated since API version 9. 948 949**Required permissions**: ohos.permission.GET_WALLPAPER 950 951**System capability**: SystemCapability.MiscServices.Wallpaper 952 953**System API**: This is a system API. 954 955**Parameters** 956 957| Name| Type| Mandatory| Description| 958| -------- | -------- | -------- | -------- | 959| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | Yes| Wallpaper type.| 960 961**Return value** 962 963| Type| Description| 964| -------- | -------- | 965| Promise<image.PixelMap> | Promise used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.| 966 967**Example** 968 969```ts 970import { BusinessError } from '@kit.BasicServicesKit'; 971import { image } from '@kit.ImageKit'; 972 973wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => { 974 console.info(`success to getPixelMap : ${JSON.stringify(data)}`); 975 }).catch((error: BusinessError) => { 976 console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`); 977}); 978``` 979