js-apis-wallpaper.md 23.4 KB
Newer Older
Z
zengyawen 已提交
1 2 3
# Wallpaper


E
ester.zhou 已提交
4
> **NOTE**<br>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.
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22


## Modules to Import


```
import wallpaper from '@ohos.wallpaper';
```


## WallpaperType

Defines the wallpaper type.

**System capability**: SystemCapability.MiscServices.Wallpaper

| Name | Description |
| -------- | -------- |
E
ester.zhou 已提交
23 24
| WALLPAPER_LOCKSCREEN | Lock screen wallpaper. |
| WALLPAPER_SYSTEM | Home screen wallpaper. |
Z
zengyawen 已提交
25 26 27 28 29 30


## wallpaper.getColors

getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;RgbaColor&gt;&gt;): void

E
ester.zhou 已提交
31
Obtains the main color information of the wallpaper of the specified type. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
32 33 34

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
35 36 37 38 39 40 41
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
| callback | AsyncCallback&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | Yes | Callback used to return the main color information of the wallpaper. |

**Example**
Z
zengyawen 已提交
42

E
ester.zhou 已提交
43
  ```js
Z
zengyawen 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57
  wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
      if (error) {
          console.error(`failed to getColors because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to getColors.`);
  });
  ```


## wallpaper.getColors

getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt;

E
ester.zhou 已提交
58
Obtains the main color information of the wallpaper of the specified type. This API uses a promise to return the result.
Z
zengyawen 已提交
59 60 61

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
62
**Parameters**
Z
zengyawen 已提交
63

E
ester.zhou 已提交
64 65 66
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
Z
zengyawen 已提交
67

E
ester.zhou 已提交
68 69 70 71 72 73 74 75
**Return value**

| Type | Description |
| -------- | -------- |
| Promise&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | Promise used to return the main color information of the wallpaper. |

**Example**

E
ester.zhou 已提交
76 77 78 79 80 81 82
  ```js
  wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
      console.log(`success to getColors.`);
  }).catch((error) => {
      console.error(`failed to getColors because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
83 84 85 86 87 88


## wallpaper.getId

getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void

E
ester.zhou 已提交
89
Obtains the ID of the wallpaper of the specified type. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
90 91 92

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
93
**Parameters**
Z
zengyawen 已提交
94

E
ester.zhou 已提交
95 96 97 98 99 100 101
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
| callback | AsyncCallback&lt;number&gt; | 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. |

**Example**

E
ester.zhou 已提交
102 103 104 105 106 107 108 109 110
  ```js
  wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
      if (error) {
          console.error(`failed to getId because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to getId: ` + JSON.stringify(data));
  });
  ```
Z
zengyawen 已提交
111 112 113 114 115 116


## wallpaper.getId

getId(wallpaperType: WallpaperType): Promise&lt;number&gt;

E
ester.zhou 已提交
117
Obtains the ID of the wallpaper of the specified type. This API uses a promise to return the result.
Z
zengyawen 已提交
118 119 120

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
121

E
ester.zhou 已提交
122
**Parameters**
Z
zengyawen 已提交
123

E
ester.zhou 已提交
124 125 126
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
Z
zengyawen 已提交
127

E
ester.zhou 已提交
128
**Return value**
Z
zengyawen 已提交
129

E
ester.zhou 已提交
130 131 132 133 134 135
| Type | Description |
| -------- | -------- |
| Promise&lt;number&gt; | 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. |

**Example**

E
ester.zhou 已提交
136 137 138 139 140 141 142
  ```js
  wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
      console.log(`success to getId: ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to getId because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
143 144 145 146 147 148


## wallpaper.getMinHeight

getMinHeight(callback: AsyncCallback&lt;number&gt;): void

E
ester.zhou 已提交
149
Obtains the minimum height of this wallpaper. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
150 151 152

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
153
**Parameters**
Z
zengyawen 已提交
154

E
ester.zhou 已提交
155 156 157 158 159 160
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;number&gt; | 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. |

**Example**

E
ester.zhou 已提交
161 162 163 164 165 166 167 168 169
  ```js
  wallpaper.getMinHeight((error, data) => {
      if (error) {
          console.error(`failed to getMinHeight because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to getMinHeight: ` + JSON.stringify(data));
  });
  ```
Z
zengyawen 已提交
170 171 172 173 174 175


## wallpaper.getMinHeight

getMinHeight(): Promise&lt;number&gt;

E
ester.zhou 已提交
176
Obtains the minimum height of this wallpaper. This API uses a promise to return the result.
Z
zengyawen 已提交
177 178 179

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
180

E
ester.zhou 已提交
181
**Return value**
Z
zengyawen 已提交
182

E
ester.zhou 已提交
183 184 185
| Type | Description |
| -------- | -------- |
| Promise&lt;number&gt; | 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. |
Z
zengyawen 已提交
186

E
ester.zhou 已提交
187 188
**Example**

E
ester.zhou 已提交
189 190 191 192 193 194 195
  ```js
  wallpaper.getMinHeight().then((data) => {
      console.log(`success to getMinHeight: ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to getMinHeight because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
196 197 198 199 200 201


## wallpaper.getMinWidth

getMinWidth(callback: AsyncCallback&lt;number&gt;): void

E
ester.zhou 已提交
202
Obtains the minimum width of this wallpaper. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
203 204 205

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
206
**Parameters**
Z
zengyawen 已提交
207

E
ester.zhou 已提交
208 209 210
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;number&gt; | 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. |
Z
zengyawen 已提交
211

E
ester.zhou 已提交
212 213
**Example**

E
ester.zhou 已提交
214 215 216 217 218 219 220 221 222
  ```js
  wallpaper.getMinWidth((error, data) => {
      if (error) {
          console.error(`failed to getMinWidth because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to getMinWidth: ` + JSON.stringify(data));
  });
  ```
Z
zengyawen 已提交
223 224 225 226 227 228


## wallpaper.getMinWidth

getMinWidth(): Promise&lt;number&gt;

E
ester.zhou 已提交
229
Obtains the minimum width of this wallpaper. This API uses a promise to return the result.
Z
zengyawen 已提交
230 231 232

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
233
**Return value**
Z
zengyawen 已提交
234

E
ester.zhou 已提交
235 236 237 238 239 240
| Type | Description |
| -------- | -------- |
| Promise&lt;number&gt; | Promised 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. |

**Example**

E
ester.zhou 已提交
241 242 243 244 245 246 247
  ```js
  wallpaper.getMinWidth().then((data) => {
      console.log(`success to getMinWidth: ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to getMinWidth because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
248 249 250 251 252 253


## wallpaper.isChangePermitted

isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
254
Checks whether to allow the application to change the wallpaper for the current user. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
255 256 257

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
258
**Parameters**
Z
zengyawen 已提交
259

E
ester.zhou 已提交
260 261
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
262
| callback | AsyncCallback&lt;boolean&gt; | Yes | Returns **true** if the application is allowed to change the wallpaper for the current user; returns **false** otherwise. |
E
ester.zhou 已提交
263 264 265

**Example**

E
ester.zhou 已提交
266 267 268 269 270 271 272 273 274
  ```js
  wallpaper.isChangePermitted((error, data) => {
      if (error) {
          console.error(`failed to isChangePermitted because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to isChangePermitted: ` + JSON.stringify(data));
  });
  ```
Z
zengyawen 已提交
275 276 277 278 279 280


## wallpaper.isChangePermitted

isChangePermitted(): Promise&lt;boolean&gt;

E
ester.zhou 已提交
281
Checks whether to allow the application to change the wallpaper for the current user. This API uses a promise to return the result.
Z
zengyawen 已提交
282 283 284

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
285
**Return value**
Z
zengyawen 已提交
286

E
ester.zhou 已提交
287 288
| Type | Description |
| -------- | -------- |
E
ester.zhou 已提交
289
| Promise&lt;boolean&gt; | Returns **true** if the application is allowed to change the wallpaper for the current user; returns **false** otherwise. |
E
ester.zhou 已提交
290 291 292

**Example**

E
ester.zhou 已提交
293 294 295 296 297 298 299
  ```js
  wallpaper.isChangePermitted().then((data) => {
      console.log(`success to isChangePermitted: ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to isChangePermitted because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
300 301 302 303 304 305


## wallpaper.isOperationAllowed

isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void

E
ester.zhou 已提交
306
Checks whether the user is allowed to set wallpapers. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
307 308 309

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
310
**Parameters**
Z
zengyawen 已提交
311

E
ester.zhou 已提交
312 313
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
314
| callback | AsyncCallback&lt;boolean&gt; | Yes | Returns **true** if the user is allowed to set wallpapers; returns **false** otherwise. |
E
ester.zhou 已提交
315 316 317

**Example**

E
ester.zhou 已提交
318 319 320 321 322 323 324 325 326
  ```js
  wallpaper.isOperationAllowed((error, data) => {
      if (error) {
          console.error(`failed to isOperationAllowed because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to isOperationAllowed: ` + JSON.stringify(data));
  });
  ```
Z
zengyawen 已提交
327 328 329 330 331 332


## wallpaper.isOperationAllowed

isOperationAllowed(): Promise&lt;boolean&gt;

E
ester.zhou 已提交
333
Checks whether the user is allowed to set wallpapers. This API uses a promise to return the result.
Z
zengyawen 已提交
334 335 336

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
337
**Return value**
Z
zengyawen 已提交
338

E
ester.zhou 已提交
339 340
| Type | Description |
| -------- | -------- |
E
ester.zhou 已提交
341
| Promise&lt;boolean&gt; | Returns **true** if the user is allowed to set wallpapers; returns **false** otherwise. |
E
ester.zhou 已提交
342 343 344

**Example**

E
ester.zhou 已提交
345 346 347 348 349 350 351
  ```js
  wallpaper.isOperationAllowed().then((data) => {
      console.log(`success to isOperationAllowed: ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to isOperationAllowed because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
352 353 354 355 356 357


## wallpaper.reset

reset(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void

E
ester.zhou 已提交
358
Resets the wallpaper of the specified type to the default wallpaper. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
359

E
ester.zhou 已提交
360
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
361 362 363

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
364
**Parameters**
Z
zengyawen 已提交
365

E
ester.zhou 已提交
366 367 368
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
E
ester.zhou 已提交
369
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. If the operation is successful, the result is returned. Otherwise, error information is returned. |
E
ester.zhou 已提交
370 371 372

**Example**

E
ester.zhou 已提交
373 374 375 376 377 378 379 380 381
  ```js
  wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
      if (error) {
          console.error(`failed to reset because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to reset.`);
  });
  ```
Z
zengyawen 已提交
382 383 384 385 386 387


## wallpaper.reset

reset(wallpaperType: WallpaperType): Promise&lt;void&gt;

E
ester.zhou 已提交
388
Resets the wallpaper of the specified type to the default wallpaper. This API uses a promise to return the result.
Z
zengyawen 已提交
389

E
ester.zhou 已提交
390
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
391 392 393

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
394
**Parameters**
Z
zengyawen 已提交
395

E
ester.zhou 已提交
396 397 398
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
Z
zengyawen 已提交
399

E
ester.zhou 已提交
400 401 402 403
**Return value**

| Type | Description |
| -------- | -------- |
E
ester.zhou 已提交
404
| Promise&lt;void&gt; | Promise used to return the result. If the operation is successful, the result is returned. Otherwise, error information is returned. |
E
ester.zhou 已提交
405 406 407

**Example**

E
ester.zhou 已提交
408 409 410 411 412 413 414
  ```js
  wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
      console.log(`success to reset.`);
  }).catch((error) => {
      console.error(`failed to reset because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
415 416 417 418 419 420


## wallpaper.setWallpaper

setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void

E
ester.zhou 已提交
421
Sets a specified source as the wallpaper of a specified type. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
422

E
ester.zhou 已提交
423
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
424 425 426

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
427 428 429 430
**Parameters**

| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
431
| source | string \| [PixelMap](js-apis-image.md#pixelmap7) | Yes | URI of a JPEG or PNG file, or bitmap of a PNG file. |
E
ester.zhou 已提交
432 433 434 435 436 437 438
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result. If the operation is successful, the setting result is returned. Otherwise, error information is returned. |

**Example**

```js
// The source type is string.
E
ester.zhou 已提交
439 440 441 442 443 444 445 446 447
  let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
  wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {    
      if (error) {        
          console.error(`failed to setWallpaper because: ` + JSON.stringify(error));       
          return;   
      }    
      console.log(`success to setWallpaper.`);
  });
  
E
ester.zhou 已提交
448
// The source type is image.PixelMap.
E
ester.zhou 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  import image from '@ohos.multimedia.image';
  let imageSource = image.createImageSource("file://" + wallpaperPath);
  let opts = {
      "desiredSize": {
          "height": 3648,
          "width": 2736
      }
  };
  imageSource.createPixelMap(opts).then((pixelMap) => {      
      wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {    
          if (error) {       
              console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
              return;
          }    
          console.log(`success to setWallpaper.`);
      });
  }).catch((error) => {       
      console.error(`failed to createPixelMap because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
469 470 471 472 473 474


## wallpaper.setWallpaper

setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;

E
ester.zhou 已提交
475
Sets a specified source as the wallpaper of a specified type. This API uses a promise to return the result.
Z
zengyawen 已提交
476

E
ester.zhou 已提交
477
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
478 479 480

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
481
**Parameters**
Z
zengyawen 已提交
482

E
ester.zhou 已提交
483 484
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
485
| source | string \| [PixelMap](js-apis-image.md#pixelmap7) | Yes | URI path of the JPEG or PNG file, or bitmap of the PNG file. |
E
ester.zhou 已提交
486
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
Z
zengyawen 已提交
487

E
ester.zhou 已提交
488 489 490 491 492 493 494 495 496 497
**Return value**

| Type | Description |
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result. If the operation is successful, the setting result is returned. Otherwise, error information is returned. |

**Example**

```js
// The source type is string.
E
ester.zhou 已提交
498 499 500 501 502 503 504
  let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
  wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
      console.log(`success to setWallpaper.`);
  }).catch((error) => {
      console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
  });
  
E
ester.zhou 已提交
505
// The source type is image.PixelMap.
E
ester.zhou 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  import image from '@ohos.multimedia.image';
  let imageSource = image.createImageSource("file://" + wallpaperPath);
  let opts = {
      "desiredSize": {
          "height": 3648,
          "width": 2736
      }
  };
  imageSource.createPixelMap(opts).then((pixelMap) => {      
      wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
          console.log(`success to setWallpaper.`);
      }).catch((error) => {
          console.error(`failed to setWallpaper because: ` + JSON.stringify(error));
      });
  }).catch((error) => {       
      console.error(`failed to createPixelMap because: ` + JSON.stringify(error));
  });
  ```
Z
zengyawen 已提交
524 525 526 527 528

## wallpaper.getFile<sup>8+</sup>

getFile(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void

E
ester.zhou 已提交
529
Obtains the wallpaper of the specified type. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
530

E
ester.zhou 已提交
531
**Required permissions**: ohos.permission.SET_WALLPAPER and ohos.permission.READ_USER_STORAGE
Z
zengyawen 已提交
532 533 534

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
535
**Parameters**
Z
zengyawen 已提交
536

E
ester.zhou 已提交
537 538 539 540 541 542 543
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
| callback | AsyncCallback&lt;number&gt; | 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. |

**Example**

E
ester.zhou 已提交
544 545 546 547 548 549 550 551 552
  ```js
  wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
      if (error) {
          console.error(`failed to getFile because: ` + JSON.stringify(error));
          return;
      }
      console.log(`success to getFile: ` + JSON.stringify(data));
  });
  ```
Z
zengyawen 已提交
553 554 555 556 557

## wallpaper.getFile<sup>8+</sup>

getFile(wallpaperType: WallpaperType): Promise&lt;number&gt;

E
ester.zhou 已提交
558
Obtains the wallpaper of the specified type. This API uses a promise to return the result.
Z
zengyawen 已提交
559

E
ester.zhou 已提交
560
**Required permissions**: ohos.permission.SET_WALLPAPER and ohos.permission.READ_USER_STORAGE
Z
zengyawen 已提交
561 562 563

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
564
**Parameters**
Z
zengyawen 已提交
565

E
ester.zhou 已提交
566 567 568
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes | Wallpaper type. |
Z
zengyawen 已提交
569

E
ester.zhou 已提交
570 571 572 573 574 575 576 577
**Return value**

| Type | Description |
| -------- | -------- |
| Promise&lt;number&gt; | 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. |

**Example**

E
ester.zhou 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  ```js
  wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
      console.log(`success to getFile: ` + JSON.stringify(data));
  }).catch((error) => {
      console.error(`failed to getFile because: ` + JSON.stringify(error));
  });
  ```


## wallpaper.getPixelMap

getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;

Obtains the pixel image for the wallpaper of the specified type. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.GET_WALLPAPER and ohos.permission.READ_USER_STORAGE

**System capability**: SystemCapability.MiscServices.Wallpaper

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the operation is successful, the result is returned. Otherwise, error information is returned.|

**Example**

  ```js
  wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, function (err, data) {
      console.info('wallpaperXTS ===> testGetPixelMapCallbackSystem err : ' + JSON.stringify(err));
      console.info('wallpaperXTS ===> testGetPixelMapCallbackSystem data : ' + JSON.stringify(data));
  });
  ```


## wallpaper.getPixelMap

getPixelMap(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;

Obtains the pixel image for the wallpaper of the specified type. This API uses a promise to return the result.

**Required permissions**: ohos.permission.GET_WALLPAPER and ohos.permission.READ_USER_STORAGE

**System capability**: SystemCapability.MiscServices.Wallpaper

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|

**Return value**

| Type| Description|
| -------- | -------- |
| Promise&lt;void&gt; | Promise used to return the result. If the operation is successful, the result is returned. Otherwise, error information is returned.|

**Example**

  ```js
  wallpaper.getPixelMap(WALLPAPER_SYSTEM).then((data) => {
      console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem data : ' + data);
      console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem data : ' + JSON.stringify(data));
  }).catch((err) => {
      console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem err : ' + err);
      console.info('wallpaperXTS ===> testGetPixelMapPromiseSystem err : ' + JSON.stringify(err));
  });
  ```
Z
zengyawen 已提交
647 648 649 650 651 652 653 654 655 656


## wallpaper.on('colorChange')

on(type: 'colorChange', callback: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void

Subscribes to the wallpaper color change event.

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
657
**Parameters**
Z
zengyawen 已提交
658

E
ester.zhou 已提交
659 660 661
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| type | string | Yes | Type of the event to subscribe to. The value **colorChange** indicates subscribing to the wallpaper color change event. |
E
ester.zhou 已提交
662
| callback | function | Yes | Callback triggered when the wallpaper color changes. The wallpaper type and main colors are returned.<br>- colors<br>  Main color information of the wallpaper. For details, see [RgbaColor](#rgbacolor).<br>- wallpaperType<br>  Wallpaper type. |
E
ester.zhou 已提交
663 664 665

**Example**

E
ester.zhou 已提交
666 667 668 669 670 671
  ```js
  let listener = (colors, wallpaperType) => {
      console.log(`wallpaper color changed.`);
  };
  wallpaper.on('colorChange', listener);
  ```
Z
zengyawen 已提交
672 673 674 675 676 677 678 679 680 681


## wallpaper.off('colorChange')

off(type: 'colorChange', callback?: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void

Unsubscribes from the wallpaper color change event.

**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
682
**Parameters**
Z
zengyawen 已提交
683

E
ester.zhou 已提交
684 685 686
| Name | Type | Mandatory | Description |
| -------- | -------- | -------- | -------- |
| type | string | Yes | Type of the event to unsubscribe from. The value **colorChange** indicates unsubscribing from the wallpaper color change event. |
E
ester.zhou 已提交
687
| callback | function | No |   Callback for the wallpaper color change event. If this parameter is not specified, all callbacks corresponding to the wallpaper color change event are invoked.<br>- colors<br>  Main color information of the wallpaper. For details, see [RgbaColor](#rgbacolor).<br>- wallpaperType<br>  Wallpaper type. |
E
ester.zhou 已提交
688 689 690

**Example**

E
ester.zhou 已提交
691 692 693 694 695
  ```js
  let listener = (colors, wallpaperType) => {
      console.log(`wallpaper color changed.`);
  };
  wallpaper.on('colorChange', listener);
E
ester.zhou 已提交
696 697 698 699 700
// Unsubscribe from the listener.
wallpaper.off('colorChange', listener);
//Unsubscribe from all subscriptions of the colorChange type.
wallpaper.off('colorChange');
```
Z
zengyawen 已提交
701 702 703 704 705 706 707 708


## RgbaColor

**System capability**: SystemCapability.MiscServices.Wallpaper

| Name | Type | Readable | Writable | Description |
| -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
709 710 711 712
| red | number | Yes | Yes | Red color. The value ranges from 0 to 255. |
| green | number | Yes | Yes | Green color. The value ranges from 0 to 255. |
| blue | number | Yes | Yes | Blue color. The value ranges from 0 to 255. |
| alpha | number | Yes | Yes | Alpha value. The value ranges from 0 to 255. |