1# @ohos.wallpaper (壁纸) 2 3壁纸管理服务是OpenHarmony中的系统服务,主要为系统提供壁纸管理服务能力,支持系统显示、设置、切换壁纸等功能。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10## 导入模块 11 12 13```ts 14import wallpaper from '@ohos.wallpaper'; 15``` 16## WallpaperResourceType<sup>10+</sup> 17 18定义壁纸资源的枚举类型。 19 20**系统能力**: SystemCapability.MiscServices.Wallpaper 21 22**系统接口**:此接口为系统接口。 23 24| 名称 | 值 |说明 | 25| -------- | -------- |-------- | 26| DEFAULT | 0 |默认为图片资源。 | 27| PICTURE | 1 |图片资源。 | 28| VIDEO | 2 |视频资源。 | 29| PACKAGE | 3 |包资源。 | 30 31 32## WallpaperType<sup>7+</sup> 33 34定义壁纸的枚举类型。 35 36**系统能力**: SystemCapability.MiscServices.Wallpaper 37 38| 名称 | 值 |说明 | 39| -------- | -------- |-------- | 40| WALLPAPER_SYSTEM | 0 |主屏幕壁纸标识。 | 41| WALLPAPER_LOCKSCREEN | 1 |锁屏壁纸标识。 | 42 43 44## RgbaColor<sup>(deprecated)</sup> 45 46定义壁纸颜色信息结构。 47 48> **说明:** 49> 50> 从 API version 7开始支持,从API version 9开始废弃。 51 52**系统能力**: SystemCapability.MiscServices.Wallpaper 53 54| 名称 | 类型 | 可读 | 可写 | 说明 | 55| -------- | -------- | -------- | -------- | -------- | 56| red | number | 是 | 是 | 表示红色值,范围为 0 到 255。 | 57| green | number | 是 | 是 | 表示绿色值,范围为 0 到 255。 | 58| blue | number | 是 | 是 | 表示蓝色值,范围为 0 到 255。 | 59| alpha | number | 是 | 是 | 表示 alpha 值,范围为 0 到 255。 | 60 61 62## wallpaper.setVideo<sup>10+</sup> 63 64setVideo(source: string, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 65 66将视频资源设置为桌面或锁屏的动态壁纸。使用callback异步回调。 67 68**需要权限**:ohos.permission.SET_WALLPAPER 69 70**系统能力**: SystemCapability.MiscServices.Wallpaper 71 72**系统接口**:此接口为系统接口。 73 74**参数:** 75 76| 参数名 | 类型 | 必填 | 说明 | 77| -------- | -------- | -------- | -------- | 78| source | string | 是 | mp4文件的Uri路径。 | 79| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 80| callback | AsyncCallback<void> | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 | 81 82**示例:** 83 84```ts 85import { BusinessError } from '@ohos.base'; 86 87let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4"; 88try { 89 wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 90 if (error) { 91 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 92 return; 93 } 94 console.log(`success to setVideo.`); 95 }); 96} catch (error) { 97 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 98} 99``` 100 101## wallpaper.setVideo<sup>10+</sup> 102 103setVideo(source: string, wallpaperType: WallpaperType): Promise<void> 104 105将视频资源设置为桌面或锁屏的动态壁纸。使用promise异步回调。 106 107**需要权限**:ohos.permission.SET_WALLPAPER 108 109**系统能力**: SystemCapability.MiscServices.Wallpaper 110 111**系统接口**:此接口为系统接口。 112 113**参数:** 114 115| 参数名 | 类型 | 必填 | 说明 | 116| -------- | -------- | -------- | -------- | 117| source | string | 是 | mp4文件的Uri路径。 | 118| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 119 120**返回值:** 121 122| 类型 | 说明 | 123| -------- | -------- | 124| Promise<void> | 无返回结果的Promise对象。 | 125 126**示例:** 127 128```ts 129import { BusinessError } from '@ohos.base'; 130 131let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4"; 132try { 133 wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 134 console.log(`success to setVideo.`); 135 }).catch((error: BusinessError) => { 136 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 137 }); 138} catch (error) { 139 console.error(`failed to setVideo because: ${JSON.stringify(error)}`); 140} 141``` 142 143## wallpaper.setCustomWallpaper<sup>10+</sup> 144 145setCustomWallpaper(source: string, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 146 147将指定uri的内容设置为壁纸资源,仅当com.ohos.sceneboard存在时,支持使用该接口。使用callback异步回调。 148 149**需要权限**:ohos.permission.SET_WALLPAPER 150 151**系统能力**: SystemCapability.MiscServices.Wallpaper 152 153**系统接口**:此接口为系统接口。 154 155**参数:** 156 157| 参数名 | 类型 | 必填 | 说明 | 158| -------- | -------- | -------- | -------- | 159| source | string | 是 | 自定义壁纸的Uri路径。 | 160| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 161| callback | AsyncCallback<void> | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 | 162 163**示例:** 164 165```ts 166import { BusinessError } from '@ohos.base'; 167 168let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip"; 169try { 170 wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 171 if (error) { 172 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 173 return; 174 } 175 console.log(`success to setCustomWallpaper.`); 176 }); 177} catch (error) { 178 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 179} 180 181``` 182 183## wallpaper.setCustomWallpaper<sup>10+</sup> 184 185setCustomWallpaper(source: string, wallpaperType: WallpaperType): Promise<void> 186 187将指定uri的内容设置为壁纸资源,仅当com.ohos.sceneboard存在时,支持使用该接口。使用Promise异步回调。 188 189**需要权限**:ohos.permission.SET_WALLPAPER 190 191**系统能力**: SystemCapability.MiscServices.Wallpaper 192 193**系统接口**:此接口为系统接口。 194 195**参数:** 196 197| 参数名 | 类型 | 必填 | 说明 | 198| -------- | -------- | -------- | -------- | 199| source | string | 是 | 自定义壁纸的Uri路径。 | 200| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 201 202**返回值:** 203 204| 类型 | 说明 | 205| -------- | -------- | 206| Promise<void> | 无返回结果的Promise对象。 | 207 208**示例:** 209 210```ts 211import { BusinessError } from '@ohos.base'; 212 213let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip"; 214try { 215 wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 216 console.log(`success to setCustomWallpaper.`); 217 }).catch((error: BusinessError) => { 218 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 219 }); 220} catch (error) { 221 console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`); 222} 223``` 224 225## wallpaper.on('wallpaperChange')<sup>10+</sup> 226 227on(type: 'wallpaperChange', callback: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) => void): void 228 229订阅壁纸变化通知事件。 230 231**系统能力**: SystemCapability.MiscServices.Wallpaper 232 233**系统接口**:此接口为系统接口。 234 235**参数:** 236 237| 参数名 | 类型 | 必填 | 说明 | 238| -------- | -------- | -------- | -------- | 239| type | string | 是 | 事件回调类型。支持的事件为'wallpaperChange',完成壁纸切换后触发该事件。 | 240| callback | function | 是 | 壁纸变化触发该回调方法,返回壁纸类型和壁纸资源类型。<br/>- wallpaperType:壁纸类型。<br/>- resourceType:壁纸资源类型。<br/>- uri:壁纸资源地址。 | 241 242**示例:** 243 244```ts 245try { 246 let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => { 247 console.log(`wallpaper color changed.`); 248 }; 249 wallpaper.on('wallpaperChange', listener); 250} catch (error) { 251 console.error(`failed to on because: ${JSON.stringify(error)}`); 252} 253``` 254 255## wallpaper.off('wallpaperChange')<sup>10+</sup> 256 257off(type: 'wallpaperChange', callback?: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) => void): void 258 259取消订阅壁纸变化通知事件。 260 261**系统能力**: SystemCapability.MiscServices.Wallpaper 262 263**系统接口**:此接口为系统接口。 264 265**参数:** 266 267| 参数名 | 类型 | 必填 | 说明 | 268| -------- | -------- | -------- | -------- | 269| type | string | 是 | 事件回调类型。支持的事件为'wallpaperChange',完成壁纸切换后触发该事件。 | 270| callback | function | 否 | 表示要取消的壁纸变化回调,不填写该参数则取消订阅该type对应的所有回调。<br/>- wallpaperType:壁纸类型。<br/>- resourceType:壁纸资源类型。<br/>- uri:壁纸资源地址。 | 271 272**示例:** 273 274```ts 275let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => { 276 console.log(`wallpaper color changed.`); 277}; 278try { 279 wallpaper.on('wallpaperChange', listener); 280} catch (error) { 281 console.error(`failed to on because: ${JSON.stringify(error)}`); 282} 283 284try { 285 // 取消订阅listener 286 wallpaper.off('wallpaperChange', listener); 287} catch (error) { 288 console.error(`failed to off because: ${JSON.stringify(error)}`); 289} 290 291try { 292 // 取消所有'wallpaperChange'类型的订阅 293 wallpaper.off('wallpaperChange'); 294} catch (error) { 295 console.error(`failed to off because: ${JSON.stringify(error)}`); 296} 297``` 298 299## wallpaper.getColorsSync<sup>9+</sup> 300 301getColorsSync(wallpaperType: WallpaperType): Array<RgbaColor> 302 303获取指定类型壁纸的主要颜色信息。 304 305**系统能力**: SystemCapability.MiscServices.Wallpaper 306 307**系统接口**:此接口为系统接口。 308 309**参数:** 310 311| 参数名 | 类型 | 必填 | 说明 | 312| -------- | -------- | -------- | -------- | 313| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 314 315**返回值**: 316 317| 类型 | 说明 | 318| -------- | -------- | 319| Array<[RgbaColor](#rgbacolor)> | 返回壁纸的主要颜色信息。 | 320 321**示例**: 322 323```ts 324try { 325 let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM); 326 console.log(`success to getColorsSync: ${JSON.stringify(colors)}`); 327} catch (error) { 328 console.error(`failed to getColorsSync because: ${JSON.stringify(error)}`); 329} 330``` 331 332## wallpaper.getMinHeightSync<sup>9+</sup> 333 334getMinHeightSync(): number 335 336获取壁纸的最小高度值。 337 338**系统能力**: SystemCapability.MiscServices.Wallpaper 339 340**系统接口**:此接口为系统接口。 341 342**返回值:** 343 344| 类型 | 说明 | 345| -------- | -------- | 346| number | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 | 347 348**示例:** 349 350```ts 351let minHeight = wallpaper.getMinHeightSync(); 352``` 353 354## wallpaper.getMinWidthSync<sup>9+</sup> 355 356getMinWidthSync(): number 357 358获取壁纸的最小宽度值。 359 360**系统能力**: SystemCapability.MiscServices.Wallpaper 361 362**系统接口**:此接口为系统接口。 363 364**返回值:** 365 366| 类型 | 说明 | 367| -------- | -------- | 368| number | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 | 369 370**示例:** 371 372```ts 373let minWidth = wallpaper.getMinWidthSync(); 374``` 375 376## wallpaper.restore<sup>9+</sup> 377 378restore(wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 379 380移除指定类型的壁纸,恢复为默认显示的壁纸。使用callback异步回调。 381 382**需要权限**:ohos.permission.SET_WALLPAPER 383 384**系统能力**: SystemCapability.MiscServices.Wallpaper 385 386**系统接口**:此接口为系统接口。 387 388**参数:** 389 390| 参数名 | 类型 | 必填 | 说明 | 391| -------- | -------- | -------- | -------- | 392| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 393| callback | AsyncCallback<void> | 是 | 回调函数,移除壁纸成功,error为undefined,否则返回error信息。 | 394 395**示例:** 396 397```ts 398import { BusinessError } from '@ohos.base'; 399 400wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 401 if (error) { 402 console.error(`failed to restore because: ${JSON.stringify(error)}`); 403 return; 404 } 405 console.log(`success to restore.`); 406}); 407``` 408 409## wallpaper.restore<sup>9+</sup> 410 411restore(wallpaperType: WallpaperType): Promise<void> 412 413移除指定类型的壁纸,恢复为默认显示的壁纸。使用promise异步回调。 414 415**需要权限**:ohos.permission.SET_WALLPAPER 416 417**系统能力**: SystemCapability.MiscServices.Wallpaper 418 419**系统接口**:此接口为系统接口。 420 421**参数:** 422 423| 参数名 | 类型 | 必填 | 说明 | 424| -------- | -------- | -------- | -------- | 425| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 426 427**返回值:** 428 429| 类型 | 说明 | 430| -------- | -------- | 431| Promise<void> | 无返回结果的Promise对象。 | 432 433**示例:** 434 435```ts 436import { BusinessError } from '@ohos.base'; 437 438wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 439 console.log(`success to restore.`); 440 }).catch((error: BusinessError) => { 441 console.error(`failed to restore because: ${JSON.stringify(error)}`); 442}); 443``` 444 445## wallpaper.setImage<sup>9+</sup> 446 447setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 448 449将指定资源设置为指定类型的壁纸。使用callback异步回调。 450 451**需要权限**:ohos.permission.SET_WALLPAPER 452 453**系统能力**: SystemCapability.MiscServices.Wallpaper 454 455**系统接口**:此接口为系统接口。 456 457**参数:** 458 459| 参数名 | 类型 | 必填 | 说明 | 460| -------- | -------- | -------- | -------- | 461| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 | 462| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 463| callback | AsyncCallback<void> | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 | 464 465**示例:** 466 467```ts 468import { BusinessError } from '@ohos.base'; 469import image from '@ohos.multimedia.image'; 470 471// source类型为string 472let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg"; 473wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 474 if (error) { 475 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 476 return; 477 } 478 console.log(`success to setImage.`); 479}); 480 481// source类型为image.PixelMap 482let imageSource = image.createImageSource("file://" + wallpaperPath); 483let opts: image.DecodingOptions = { 484 desiredSize: { 485 height: 3648, 486 width: 2736 487 } 488}; 489imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => { 490 wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 491 if (error) { 492 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 493 return; 494 } 495 console.log(`success to setImage.`); 496 }); 497}).catch((error: BusinessError) => { 498 console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`); 499}); 500``` 501 502## wallpaper.setImage<sup>9+</sup> 503 504setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise<void> 505 506将指定资源设置为指定类型的壁纸。使用promise异步回调。 507 508**需要权限**:ohos.permission.SET_WALLPAPER 509 510**系统能力**: SystemCapability.MiscServices.Wallpaper 511 512**系统接口**:此接口为系统接口。 513 514**参数:** 515 516| 参数名 | 类型 | 必填 | 说明 | 517| -------- | -------- | -------- | -------- | 518| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 | 519| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 520 521**返回值:** 522 523| 类型 | 说明 | 524| -------- | -------- | 525| Promise<void> | 无返回结果的Promise对象。 | 526 527**示例:** 528 529```ts 530import { BusinessError } from '@ohos.base'; 531import image from '@ohos.multimedia.image'; 532 533// source类型为string 534let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg"; 535wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 536 console.log(`success to setImage.`); 537}).catch((error: BusinessError) => { 538 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 539}); 540 541// source类型为image.PixelMap 542let imageSource = image.createImageSource("file://" + wallpaperPath); 543let opts: image.DecodingOptions = { 544 desiredSize: { 545 height: 3648, 546 width: 2736 547 } 548}; 549imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => { 550 wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 551 console.log(`success to setImage.`); 552 }).catch((error: BusinessError) => { 553 console.error(`failed to setImage because: ${JSON.stringify(error)}`); 554 }); 555}).catch((error: BusinessError) => { 556 console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`); 557}); 558``` 559 560## wallpaper.getImage<sup>9+</sup> 561 562getImage(wallpaperType: WallpaperType, callback: AsyncCallback<image.PixelMap>): void; 563 564获取壁纸图片的像素图。使用callback异步回调。 565 566**需要权限**:ohos.permission.GET_WALLPAPER 567 568**系统能力**: SystemCapability.MiscServices.Wallpaper 569 570**系统接口**:此接口为系统接口。 571 572**参数:** 573 574| 参数名 | 类型 | 必填 | 说明 | 575| -------- | -------- | -------- | -------- | 576| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 577| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | 是 | 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 | 578 579**示例:** 580 581```ts 582import { BusinessError } from '@ohos.base'; 583import image from '@ohos.multimedia.image'; 584 585wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => { 586 if (error) { 587 console.error(`failed to getImage because: ${JSON.stringify(error)}`); 588 return; 589 } 590 console.log(`success to getImage: ${JSON.stringify(data)}`); 591}); 592``` 593 594 595## wallpaper.getImage<sup>9+</sup> 596 597getImage(wallpaperType: WallpaperType): Promise<image.PixelMap> 598 599获取壁纸图片的像素图。使用promise异步回调。 600 601**需要权限**:ohos.permission.GET_WALLPAPER 602 603**系统能力**: SystemCapability.MiscServices.Wallpaper 604 605**系统接口**:此接口为系统接口。 606 607**参数:** 608 609| 参数名 | 类型 | 必填 | 说明 | 610| -------- | -------- | -------- | -------- | 611| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 612 613**返回值:** 614 615| 类型 | 说明 | 616| -------- | -------- | 617| Promise<[image.PixelMap](js-apis-image.md#pixelmap7)> | 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 | 618 619**示例:** 620 621```ts 622import { BusinessError } from '@ohos.base'; 623import image from '@ohos.multimedia.image'; 624 625wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => { 626 console.log(`success to getImage: ${JSON.stringify(data)}`); 627 }).catch((error: BusinessError) => { 628 console.error(`failed to getImage because: ${JSON.stringify(error)}`); 629}); 630``` 631 632## wallpaper.on('colorChange')<sup>(deprecated)</sup> 633 634on(type: 'colorChange', callback: (colors: Array<RgbaColor>, wallpaperType: WallpaperType) => void): void 635 636订阅壁纸颜色变化结果上报事件。 637 638> **说明:** 639> 640> 从 API version 7开始支持,从API version 9开始废弃。 641 642**系统能力**: SystemCapability.MiscServices.Wallpaper 643 644**参数:** 645 646| 参数名 | 类型 | 必填 | 说明 | 647| -------- | -------- | -------- | -------- | 648| type | string | 是 | 取值为'colorChange',表示壁纸颜色变化结果上报事件。 | 649| callback | function | 是 | 壁纸颜色变化触发该回调方法,返回壁纸类型和壁纸的主要颜色信息。<br/>- colors<br/> 壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)。<br/>- wallpaperType<br/> 壁纸类型。 | 650 651**示例:** 652 653```ts 654try { 655 let listener = (colors: Array<wallpaper.RgbaColor>, wallpaperType: wallpaper.WallpaperType): void => { 656 console.log(`wallpaper color changed.`); 657 }; 658 wallpaper.on('colorChange', listener); 659} catch (error) { 660 console.error(`failed to on because: ${JSON.stringify(error)}`); 661} 662``` 663 664## wallpaper.off('colorChange')<sup>(deprecated)</sup> 665 666off(type: 'colorChange', callback?: (colors: Array<RgbaColor>, wallpaperType: WallpaperType) => void): void 667 668取消订阅壁纸颜色变化结果上报事件。 669 670> **说明:** 671> 672> 从 API version 7开始支持,从API version 9开始废弃。 673 674**系统能力**: SystemCapability.MiscServices.Wallpaper 675 676**参数:** 677 678| 参数名 | 类型 | 必填 | 说明 | 679| -------- | -------- | -------- | -------- | 680| type | string | 是 | 取值为'colorChange',表示取消订阅壁纸颜色变化结果上报事件。 | 681| callback | function | 否 | 表示要取消的壁纸颜色变化的回调,不填写该参数则取消订阅该type对应的所有回调。<br/>- colors<br/> 壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)。<br/>- wallpaperType<br/> 壁纸类型。 | 682 683**示例:** 684 685```ts 686let listener = (colors: Array<wallpaper.RgbaColor>, wallpaperType: wallpaper.WallpaperType): void => { 687 console.log(`wallpaper color changed.`); 688}; 689try { 690 wallpaper.on('colorChange', listener); 691} catch (error) { 692 console.error(`failed to on because: ${JSON.stringify(error)}`); 693} 694 695try { 696 // 取消订阅listener 697 wallpaper.off('colorChange', listener); 698} catch (error) { 699 console.error(`failed to off because: ${JSON.stringify(error)}`); 700} 701 702try { 703 // 取消所有'colorChange'类型的订阅 704 wallpaper.off('colorChange'); 705} catch (error) { 706 console.error(`failed to off because: ${JSON.stringify(error)}`); 707} 708``` 709 710## wallpaper.getColors<sup>(deprecated)</sup> 711 712getColors(wallpaperType: WallpaperType, callback: AsyncCallback<Array<RgbaColor>>): void 713 714获取指定类型壁纸的主要颜色信息。 715 716> **说明:** 717> 718> 从 API version 7开始支持,从API version 9开始废弃。 719 720**系统能力**: SystemCapability.MiscServices.Wallpaper 721 722**参数:** 723 724| 参数名 | 类型 | 必填 | 说明 | 725| -------- | -------- | -------- | -------- | 726| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 727| callback | AsyncCallback<Array<[RgbaColor](#rgbacolor)>> | 是 | 回调函数,返回壁纸的主要颜色信息。 | 728 729**示例:** 730 731```ts 732import { BusinessError } from '@ohos.base'; 733 734wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: Array<wallpaper.RgbaColor>) => { 735 if (error) { 736 console.error(`failed to getColors because: ${JSON.stringify(error)}`); 737 return; 738 } 739 console.log(`success to getColors: ${JSON.stringify(data)}`); 740}); 741``` 742 743## wallpaper.getColors<sup>(deprecated)</sup> 744 745getColors(wallpaperType: WallpaperType): Promise<Array<RgbaColor>> 746 747获取指定类型壁纸的主要颜色信息。 748 749> **说明:** 750> 751> 从 API version 7开始支持,从API version 9开始废弃。 752 753**系统能力**: SystemCapability.MiscServices.Wallpaper 754 755**参数:** 756 757| 参数名 | 类型 | 必填 | 说明 | 758| -------- | -------- | -------- | -------- | 759| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 760 761**返回值:** 762 763| 类型 | 说明 | 764| -------- | -------- | 765| Promise<Array<[RgbaColor](#rgbacolor)>> | 返回壁纸的主要颜色信息。 | 766 767**示例:** 768 769```ts 770import { BusinessError } from '@ohos.base'; 771 772wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: Array<wallpaper.RgbaColor>) => { 773 console.log(`success to getColors: ${JSON.stringify(data)}`); 774 }).catch((error: BusinessError) => { 775 console.error(`failed to getColors because: ${JSON.stringify(error)}`); 776}); 777``` 778 779## wallpaper.getId<sup>(deprecated)</sup> 780 781getId(wallpaperType: WallpaperType, callback: AsyncCallback<number>): void 782 783获取指定类型壁纸的ID。 784 785> **说明:** 786> 787> 从 API version 7开始支持,从API version 9开始废弃。 788 789**系统能力**: SystemCapability.MiscServices.Wallpaper 790 791**参数:** 792 793| 参数名 | 类型 | 必填 | 说明 | 794| -------- | -------- | -------- | -------- | 795| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 796| callback | AsyncCallback<number> | 是 | 回调函数,返回壁纸的ID。如果配置了指定类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。 | 797 798**示例:** 799 800```ts 801import { BusinessError } from '@ohos.base'; 802 803wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: Number) => { 804 if (error) { 805 console.error(`failed to getId because: ${JSON.stringify(error)}`); 806 return; 807 } 808 console.log(`success to getId: ${JSON.stringify(data)}`); 809}); 810``` 811 812## wallpaper.getId<sup>(deprecated)</sup> 813 814getId(wallpaperType: WallpaperType): Promise<number> 815 816获取指定类型壁纸的ID。 817 818> **说明:** 819> 820> 从 API version 7开始支持,从API version 9开始废弃。 821 822**系统能力**: SystemCapability.MiscServices.Wallpaper 823 824**参数:** 825 826| 参数名 | 类型 | 必填 | 说明 | 827| -------- | -------- | -------- | -------- | 828| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 829 830**返回值:** 831 832| 类型 | 说明 | 833| -------- | -------- | 834| Promise<number> | 壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。 | 835 836**示例:** 837 838```ts 839import { BusinessError } from '@ohos.base'; 840 841wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: Number) => { 842 console.log(`success to getId: ${JSON.stringify(data)}`); 843 }).catch((error: BusinessError) => { 844 console.error(`failed to getId because: ${JSON.stringify(error)}`); 845}); 846``` 847 848## wallpaper.getMinHeight<sup>(deprecated)</sup> 849 850getMinHeight(callback: AsyncCallback<number>): void 851 852获取壁纸的最小高度值。 853 854> **说明:** 855> 856> 从 API version 7开始支持,从API version 9开始废弃。 857 858**系统能力**: SystemCapability.MiscServices.Wallpaper 859 860**参数:** 861 862| 参数名 | 类型 | 必填 | 说明 | 863| -------- | -------- | -------- | -------- | 864| callback | AsyncCallback<number> | 是 | 回调函数,返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 | 865 866**示例:** 867 868```ts 869import { BusinessError } from '@ohos.base'; 870 871wallpaper.getMinHeight((error: BusinessError, data: Number) => { 872 if (error) { 873 console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`); 874 return; 875 } 876 console.log(`success to getMinHeight: ${JSON.stringify(data)}`); 877}); 878``` 879 880## wallpaper.getMinHeight<sup>(deprecated)</sup> 881 882getMinHeight(): Promise<number> 883 884获取壁纸的最小高度值。 885 886> **说明:** 887> 888> 从 API version 7开始支持,从API version 9开始废弃。 889 890**系统能力**: SystemCapability.MiscServices.Wallpaper 891 892**返回值:** 893 894| 类型 | 说明 | 895| -------- | -------- | 896| Promise<number> | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 | 897 898**示例:** 899 900```ts 901import { BusinessError } from '@ohos.base'; 902 903wallpaper.getMinHeight().then((data: Number) => { 904 console.log(`success to getMinHeight: ${JSON.stringify(data)}`); 905}).catch((error: BusinessError) => { 906 console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`); 907}); 908``` 909 910## wallpaper.getMinWidth<sup>(deprecated)</sup> 911 912getMinWidth(callback: AsyncCallback<number>): void 913 914获取壁纸的最小宽度值。 915 916> **说明:** 917> 918> 从 API version 7开始支持,从API version 9开始废弃。 919 920**系统能力**: SystemCapability.MiscServices.Wallpaper 921 922**参数:** 923 924| 参数名 | 类型 | 必填 | 说明 | 925| -------- | -------- | -------- | -------- | 926| callback | AsyncCallback<number> | 是 | 回调函数,壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 | 927 928**示例:** 929 930```ts 931import { BusinessError } from '@ohos.base'; 932 933wallpaper.getMinWidth((error: BusinessError, data: Number) => { 934 if (error) { 935 console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`); 936 return; 937 } 938 console.log(`success to getMinWidth: ${JSON.stringify(data)}`); 939}); 940``` 941 942## wallpaper.getMinWidth<sup>(deprecated)</sup> 943 944getMinWidth(): Promise<number> 945 946获取壁纸的最小宽度值。 947 948> **说明:** 949> 950> 从 API version 7开始支持,从API version 9开始废弃。 951 952**系统能力**: SystemCapability.MiscServices.Wallpaper 953 954**返回值:** 955 956| 类型 | 说明 | 957| -------- | -------- | 958| Promise<number> | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 | 959 960**示例:** 961 962```ts 963import { BusinessError } from '@ohos.base'; 964 965wallpaper.getMinWidth().then((data: Number) => { 966 console.log(`success to getMinWidth: ${JSON.stringify(data)}`); 967 }).catch((error: BusinessError) => { 968 console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`); 969}); 970``` 971 972## wallpaper.isChangePermitted<sup>(deprecated)</sup> 973 974isChangePermitted(callback: AsyncCallback<boolean>): void 975 976是否允许应用改变当前用户的壁纸。 977 978> **说明:** 979> 980> 从 API version 7开始支持,从API version 9开始废弃。 981 982**系统能力**: SystemCapability.MiscServices.Wallpaper 983 984**参数:** 985 986| 参数名 | 类型 | 必填 | 说明 | 987| -------- | -------- | -------- | -------- | 988| callback | AsyncCallback<boolean> | 是 | 回调函数,返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 | 989 990**示例:** 991 992```ts 993import { BusinessError } from '@ohos.base'; 994 995wallpaper.isChangePermitted((error: BusinessError, data: Boolean) => { 996 if (error) { 997 console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`); 998 return; 999 } 1000 console.log(`success to isChangePermitted: ${JSON.stringify(data)}`); 1001}); 1002``` 1003 1004## wallpaper.isChangePermitted<sup>(deprecated)</sup> 1005 1006isChangePermitted(): Promise<boolean> 1007 1008是否允许应用改变当前用户的壁纸。 1009 1010> **说明:** 1011> 1012> 从 API version 7开始支持,从API version 9开始废弃。 1013 1014**系统能力**: SystemCapability.MiscServices.Wallpaper 1015 1016**返回值:** 1017 1018| 类型 | 说明 | 1019| -------- | -------- | 1020| Promise<boolean> | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 | 1021 1022**示例:** 1023 1024```ts 1025import { BusinessError } from '@ohos.base'; 1026 1027wallpaper.isChangePermitted().then((data: Boolean) => { 1028 console.log(`success to isChangePermitted: ${JSON.stringify(data)}`); 1029}).catch((error: BusinessError) => { 1030 console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`); 1031}); 1032``` 1033 1034## wallpaper.isOperationAllowed<sup>(deprecated)</sup> 1035 1036isOperationAllowed(callback: AsyncCallback<boolean>): void 1037 1038是否允许用户设置壁纸。 1039 1040> **说明:** 1041> 1042> 从 API version 7开始支持,从API version 9开始废弃。 1043 1044**系统能力**: SystemCapability.MiscServices.Wallpaper 1045 1046**参数:** 1047 1048| 参数名 | 类型 | 必填 | 说明 | 1049| -------- | -------- | -------- | -------- | 1050| callback | AsyncCallback<boolean> | 是 | 回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 | 1051 1052**示例:** 1053 1054```ts 1055import { BusinessError } from '@ohos.base'; 1056 1057wallpaper.isChangePermitted().then((data: Boolean) => { 1058 console.log(`success to isChangePermitted: ${JSON.stringify(data)}`); 1059}).catch((error: BusinessError) => { 1060 console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`); 1061}); 1062``` 1063 1064## wallpaper.isOperationAllowed<sup>(deprecated)</sup> 1065 1066isOperationAllowed(): Promise<boolean> 1067 1068是否允许用户设置壁纸。 1069 1070> **说明:** 1071> 1072> 从 API version 7开始支持,从API version 9开始废弃。 1073 1074**系统能力**: SystemCapability.MiscServices.Wallpaper 1075 1076**返回值:** 1077 1078| 类型 | 说明 | 1079| -------- | -------- | 1080| Promise<boolean> | 异步回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 | 1081 1082**示例:** 1083 1084```ts 1085import { BusinessError } from '@ohos.base'; 1086 1087wallpaper.isOperationAllowed().then((data: Boolean) => { 1088 console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`); 1089 }).catch((error: BusinessError) => { 1090 console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`); 1091}); 1092``` 1093 1094## wallpaper.reset<sup>(deprecated)</sup> 1095 1096reset(wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 1097 1098移除指定类型的壁纸,恢复为默认显示的壁纸。 1099 1100> **说明:** 1101> 1102> 从 API version 7开始支持,从API version 9开始废弃。 1103 1104**需要权限**:ohos.permission.SET_WALLPAPER 1105 1106**系统能力**: SystemCapability.MiscServices.Wallpaper 1107 1108**参数:** 1109 1110| 参数名 | 类型 | 必填 | 说明 | 1111| -------- | -------- | -------- | -------- | 1112| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1113| callback | AsyncCallback<void> | 是 | 回调函数,移除壁纸成功,error为undefined,否则返回error信息。 | 1114 1115**示例:** 1116 1117```ts 1118import { BusinessError } from '@ohos.base'; 1119 1120wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 1121 if (error) { 1122 console.error(`failed to reset because: ${JSON.stringify(error)}`); 1123 return; 1124 } 1125 console.log(`success to reset.`); 1126}); 1127``` 1128 1129## wallpaper.reset<sup>(deprecated)</sup> 1130 1131reset(wallpaperType: WallpaperType): Promise<void> 1132 1133移除指定类型的壁纸,恢复为默认显示的壁纸。 1134 1135> **说明:** 1136> 1137> 从 API version 7开始支持,从API version 9开始废弃。 1138 1139**需要权限**:ohos.permission.SET_WALLPAPER 1140 1141**系统能力**: SystemCapability.MiscServices.Wallpaper 1142 1143**参数:** 1144 1145| 参数名 | 类型 | 必填 | 说明 | 1146| -------- | -------- | -------- | -------- | 1147| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1148 1149**返回值:** 1150 1151| 类型 | 说明 | 1152| -------- | -------- | 1153| Promise<void> | 无返回结果的Promise对象。 | 1154 1155**示例:** 1156 1157```ts 1158import { BusinessError } from '@ohos.base'; 1159 1160wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 1161 console.log(`success to reset.`); 1162}).catch((error: BusinessError) => { 1163 console.error(`failed to reset because: ${JSON.stringify(error)}`); 1164}); 1165``` 1166 1167## wallpaper.setWallpaper<sup>(deprecated)</sup> 1168 1169setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void 1170 1171将指定资源设置为指定类型的壁纸。 1172 1173> **说明:** 1174> 1175> 从 API version 7开始支持,从API version 9开始废弃。 1176 1177**需要权限**:ohos.permission.SET_WALLPAPER 1178 1179**系统能力**: SystemCapability.MiscServices.Wallpaper 1180 1181**参数:** 1182 1183| 参数名 | 类型 | 必填 | 说明 | 1184| -------- | -------- | -------- | -------- | 1185| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 | 1186| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1187| callback | AsyncCallback<void> | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 | 1188 1189**示例:** 1190 1191```ts 1192import { BusinessError } from '@ohos.base'; 1193import image from '@ohos.multimedia.image'; 1194 1195// source类型为string 1196let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg"; 1197wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 1198 if (error) { 1199 console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`); 1200 return; 1201 } 1202 console.log(`success to setWallpaper.`); 1203}); 1204 1205// source类型为image.PixelMap 1206let imageSource = image.createImageSource("file://" + wallpaperPath); 1207let opts: image.DecodingOptions = { 1208 desiredSize: { 1209 height: 3648, 1210 width: 2736 1211 } 1212}; 1213imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => { 1214 wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => { 1215 if (error) { 1216 console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`); 1217 return; 1218 } 1219 console.log(`success to setWallpaper.`); 1220 }); 1221}).catch((error: BusinessError) => { 1222 console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`); 1223}); 1224``` 1225 1226## wallpaper.setWallpaper<sup>(deprecated)</sup> 1227 1228setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise<void> 1229 1230将指定资源设置为指定类型的壁纸。 1231 1232> **说明:** 1233> 1234> 从 API version 7开始支持,从API version 9开始废弃。 1235 1236**需要权限**:ohos.permission.SET_WALLPAPER 1237 1238**系统能力**: SystemCapability.MiscServices.Wallpaper 1239 1240**参数:** 1241 1242| 参数名 | 类型 | 必填 | 说明 | 1243| -------- | -------- | -------- | -------- | 1244| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 | 1245| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1246 1247**返回值:** 1248 1249| 类型 | 说明 | 1250| -------- | -------- | 1251| Promise<void> | 无返回结果的Promise对象。 | 1252 1253**示例:** 1254 1255```ts 1256import { BusinessError } from '@ohos.base'; 1257import image from '@ohos.multimedia.image'; 1258 1259// source类型为string 1260let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg"; 1261wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 1262 console.log(`success to setWallpaper.`); 1263 }).catch((error: BusinessError) => { 1264 console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`); 1265}); 1266 1267// source类型为image.PixelMap 1268let imageSource = image.createImageSource("file://" + wallpaperPath); 1269let opts: image.DecodingOptions = { 1270 desiredSize: { 1271 height: 3648, 1272 width: 2736 1273 } 1274}; 1275imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => { 1276 wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => { 1277 console.log(`success to setWallpaper.`); 1278 }).catch((error: BusinessError) => { 1279 console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`); 1280 }); 1281 }).catch((error: BusinessError) => { 1282 console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`); 1283}); 1284``` 1285 1286 1287## wallpaper.getFile<sup>(deprecated)</sup> 1288 1289getFile(wallpaperType: WallpaperType, callback: AsyncCallback<number>): void 1290 1291获取指定类型的壁纸文件。 1292 1293> **说明:** 1294> 1295> 从 API version 8开始支持,从API version 9开始废弃。 1296 1297**需要权限**:ohos.permission.GET_WALLPAPER 1298 1299**系统能力**: SystemCapability.MiscServices.Wallpaper 1300 1301**参数:** 1302 1303| 参数名 | 类型 | 必填 | 说明 | 1304| -------- | -------- | -------- | -------- | 1305| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1306| callback | AsyncCallback<number> | 是 | 回调函数,调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 | 1307 1308**示例:** 1309 1310```ts 1311import { BusinessError } from '@ohos.base'; 1312 1313wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: number) => { 1314 if (error) { 1315 console.error(`failed to getFile because: ${JSON.stringify(error)}`); 1316 return; 1317 } 1318 console.log(`success to getFile: ${JSON.stringify(data)}`); 1319}); 1320``` 1321 1322## wallpaper.getFile<sup>(deprecated)</sup> 1323 1324getFile(wallpaperType: WallpaperType): Promise<number> 1325 1326获取指定类型的壁纸文件。 1327 1328> **说明:** 1329> 1330> 从 API version 8开始支持,从API version 9开始废弃。 1331 1332**需要权限**:ohos.permission.GET_WALLPAPER 1333 1334**系统能力**: SystemCapability.MiscServices.Wallpaper 1335 1336**参数:** 1337 1338| 参数名 | 类型 | 必填 | 说明 | 1339| -------- | -------- | -------- | -------- | 1340| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1341 1342**返回值:** 1343 1344| 类型 | 说明 | 1345| -------- | -------- | 1346| Promise<number> | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 | 1347 1348**示例:** 1349 1350```ts 1351import { BusinessError } from '@ohos.base'; 1352 1353wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: number) => { 1354 console.log(`success to getFile: ${JSON.stringify(data)}`); 1355 }).catch((error: BusinessError) => { 1356 console.error(`failed to getFile because: ${JSON.stringify(error)}`); 1357}); 1358 1359``` 1360 1361## wallpaper.getPixelMap<sup>(deprecated)</sup> 1362 1363getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback<image.PixelMap>): void; 1364 1365获取壁纸图片的像素图。 1366 1367> **说明:** 1368> 1369> 从 API version 7开始支持,从API version 9开始废弃。 1370 1371**需要权限**:ohos.permission.GET_WALLPAPER 1372 1373**系统能力**: SystemCapability.MiscServices.Wallpaper 1374 1375**系统接口**:此接口为系统接口。 1376 1377**参数:** 1378 1379| 参数名 | 类型 | 必填 | 说明 | 1380| -------- | -------- | -------- | -------- | 1381| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1382| callback | AsyncCallback<image.PixelMap> | 是 | 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 | 1383 1384**示例:** 1385 1386```ts 1387import { BusinessError } from '@ohos.base'; 1388import image from '@ohos.multimedia.image'; 1389 1390wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => { 1391 if (error) { 1392 console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`); 1393 return; 1394 } 1395 console.log(`success to getPixelMap : ${JSON.stringify(data)}`); 1396 }); 1397``` 1398 1399## wallpaper.getPixelMap<sup>(deprecated)</sup> 1400 1401getPixelMap(wallpaperType: WallpaperType): Promise<image.PixelMap> 1402 1403获取壁纸图片的像素图。 1404 1405> **说明:** 1406> 1407> 从 API version 7开始支持,从API version 9开始废弃。 1408 1409**需要权限**:ohos.permission.GET_WALLPAPER 1410 1411**系统能力**: SystemCapability.MiscServices.Wallpaper 1412 1413**系统接口**:此接口为系统接口。 1414 1415**参数:** 1416 1417| 参数名 | 类型 | 必填 | 说明 | 1418| -------- | -------- | -------- | -------- | 1419| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 | 1420 1421**返回值:** 1422 1423| 类型 | 说明 | 1424| -------- | -------- | 1425| Promise<image.PixelMap> | 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 | 1426 1427**示例:** 1428 1429```ts 1430import { BusinessError } from '@ohos.base'; 1431import image from '@ohos.multimedia.image'; 1432 1433wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => { 1434 console.log(`success to getPixelMap : ${JSON.stringify(data)}`); 1435 }).catch((error: BusinessError) => { 1436 console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`); 1437}); 1438``` 1439