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

E
ester.zhou 已提交
3
The **wallpaper** module is part of the theme framework and provides the system-level wallpaper management service in OpenHarmony. You can use the APIs of this module to show, set, and switch between wallpapers.
Z
zengyawen 已提交
4

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


## Modules to Import


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


## WallpaperType

Defines the wallpaper type.

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

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


## wallpaper.getColors

getColors(wallpaperType: WallpaperType, callback: AsyncCallback<Array<RgbaColor>>): void

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

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

E
ester.zhou 已提交
38
**Parameters**
E
ester.zhou 已提交
39 40

| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
41
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
42 43
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
| callback | AsyncCallback<Array<[RgbaColor](#rgbacolor)>> | Yes| Callback used to return the main color information of the wallpaper.|
E
ester.zhou 已提交
44 45

**Example**
Z
zengyawen 已提交
46

E
ester.zhou 已提交
47
  ```js
Z
zengyawen 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
  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<Array<RgbaColor>>

E
ester.zhou 已提交
62
Obtains the main color information of the wallpaper of the specified type. This API uses a promise to return the result.
Z
zengyawen 已提交
63 64 65

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

E
ester.zhou 已提交
66
**Parameters**
Z
zengyawen 已提交
67

E
ester.zhou 已提交
68
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
69
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
70
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
Z
zengyawen 已提交
71

E
ester.zhou 已提交
72 73
**Return value**

E
ester.zhou 已提交
74
| Type| Description|
E
ester.zhou 已提交
75
| -------- | -------- |
E
ester.zhou 已提交
76
| Promise<Array<[RgbaColor](#rgbacolor)>> | Promise used to return the main color information of the wallpaper.|
E
ester.zhou 已提交
77 78 79

**Example**

E
ester.zhou 已提交
80 81 82 83 84 85 86
  ```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 已提交
87 88 89 90 91 92


## wallpaper.getId

getId(wallpaperType: WallpaperType, callback: AsyncCallback<number>): void

E
ester.zhou 已提交
93
Obtains the ID of the wallpaper of the specified type. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
94 95 96

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

E
ester.zhou 已提交
97
**Parameters**
Z
zengyawen 已提交
98

E
ester.zhou 已提交
99
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
100
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
101 102
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
| callback | AsyncCallback<number> | Yes| Callback used to return the wallpaper ID. If the wallpaper of the specified type is configured, a number greater than or equal to **0** is returned. Otherwise, **-1** is returned. The value ranges from -1 to 2^31-1.|
E
ester.zhou 已提交
103 104 105

**Example**

E
ester.zhou 已提交
106 107 108 109 110 111 112 113 114
  ```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 已提交
115 116 117 118 119 120


## wallpaper.getId

getId(wallpaperType: WallpaperType): Promise<number>

E
ester.zhou 已提交
121
Obtains the ID of the wallpaper of the specified type. This API uses a promise to return the result.
Z
zengyawen 已提交
122 123 124

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

E
ester.zhou 已提交
125

E
ester.zhou 已提交
126
**Parameters**
Z
zengyawen 已提交
127

E
ester.zhou 已提交
128
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
129
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
130
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
Z
zengyawen 已提交
131

E
ester.zhou 已提交
132
**Return value**
Z
zengyawen 已提交
133

E
ester.zhou 已提交
134
| Type| Description|
E
ester.zhou 已提交
135
| -------- | -------- |
E
ester.zhou 已提交
136
| Promise<number> | Promise used to return the wallpaper ID. If this type of wallpaper is configured, a number greater than or equal to **0** is returned. Otherwise, **-1** is returned. The value ranges from -1 to 2^31-1.|
E
ester.zhou 已提交
137 138 139

**Example**

E
ester.zhou 已提交
140 141 142 143 144 145 146
  ```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 已提交
147 148 149 150 151 152


## wallpaper.getMinHeight

getMinHeight(callback: AsyncCallback<number>): void

E
ester.zhou 已提交
153
Obtains the minimum height of this wallpaper. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
154 155 156

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

E
ester.zhou 已提交
157
**Parameters**
Z
zengyawen 已提交
158

E
ester.zhou 已提交
159
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
160
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
161
| callback | AsyncCallback<number> | Yes| Callback used to return the minimum wallpaper height, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default height should be used instead.|
E
ester.zhou 已提交
162 163 164

**Example**

E
ester.zhou 已提交
165 166 167 168 169 170 171 172 173
  ```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 已提交
174 175 176 177 178 179


## wallpaper.getMinHeight

getMinHeight(): Promise<number>

E
ester.zhou 已提交
180
Obtains the minimum height of this wallpaper. This API uses a promise to return the result.
Z
zengyawen 已提交
181 182 183

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

E
ester.zhou 已提交
184

E
ester.zhou 已提交
185
**Return value**
Z
zengyawen 已提交
186

E
ester.zhou 已提交
187
| Type| Description|
E
ester.zhou 已提交
188
| -------- | -------- |
E
ester.zhou 已提交
189
| Promise<number> | Promise used to return the minimum wallpaper height, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default height should be used instead.|
Z
zengyawen 已提交
190

E
ester.zhou 已提交
191 192
**Example**

E
ester.zhou 已提交
193 194 195 196 197 198 199
  ```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 已提交
200 201 202 203 204 205


## wallpaper.getMinWidth

getMinWidth(callback: AsyncCallback<number>): void

E
ester.zhou 已提交
206
Obtains the minimum width of this wallpaper. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
207 208 209

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

E
ester.zhou 已提交
210

E
ester.zhou 已提交
211
**Parameters**
Z
zengyawen 已提交
212

E
ester.zhou 已提交
213
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
214
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
215
| callback | AsyncCallback<number> | Yes| Callback used to return the minimum wallpaper width, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default width should be used instead.|
Z
zengyawen 已提交
216

E
ester.zhou 已提交
217 218
**Example**

E
ester.zhou 已提交
219 220 221 222 223 224 225 226 227
  ```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 已提交
228 229 230 231 232 233


## wallpaper.getMinWidth

getMinWidth(): Promise<number>

E
ester.zhou 已提交
234
Obtains the minimum width of this wallpaper. This API uses a promise to return the result.
Z
zengyawen 已提交
235 236 237

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

E
ester.zhou 已提交
238
**Return value**
Z
zengyawen 已提交
239

E
ester.zhou 已提交
240
| Type| Description|
E
ester.zhou 已提交
241
| -------- | -------- |
E
ester.zhou 已提交
242
| Promise<number> | 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.|
E
ester.zhou 已提交
243 244 245

**Example**

E
ester.zhou 已提交
246 247 248 249 250 251 252
  ```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 已提交
253 254 255 256 257 258


## wallpaper.isChangePermitted

isChangePermitted(callback: AsyncCallback<boolean>): void

E
ester.zhou 已提交
259
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 已提交
260 261 262

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

E
ester.zhou 已提交
263
**Parameters**
Z
zengyawen 已提交
264

E
ester.zhou 已提交
265
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
266
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
267
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. Returns **true** if the application is allowed to change the wallpaper for the current user; returns **false** otherwise.|
E
ester.zhou 已提交
268 269 270

**Example**

E
ester.zhou 已提交
271 272 273 274 275 276 277 278 279
  ```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 已提交
280 281 282 283 284 285


## wallpaper.isChangePermitted

isChangePermitted(): Promise<boolean>

E
ester.zhou 已提交
286
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 已提交
287 288 289

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

E
ester.zhou 已提交
290
**Return value**
Z
zengyawen 已提交
291

E
ester.zhou 已提交
292
| Type| Description|
E
ester.zhou 已提交
293
| -------- | -------- |
E
ester.zhou 已提交
294
| Promise<boolean> | Promise used to return the result. Returns **true** if the application is allowed to change the wallpaper for the current user; returns **false** otherwise.|
E
ester.zhou 已提交
295 296 297

**Example**

E
ester.zhou 已提交
298 299 300 301 302 303 304
  ```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 已提交
305 306 307 308 309 310


## wallpaper.isOperationAllowed

isOperationAllowed(callback: AsyncCallback<boolean>): void

E
ester.zhou 已提交
311
Checks whether the user is allowed to set wallpapers. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
312 313 314

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

E
ester.zhou 已提交
315
**Parameters**
Z
zengyawen 已提交
316

E
ester.zhou 已提交
317
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
318
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
319
| callback | AsyncCallback<boolean> | Yes| Callback used to return the result. Returns **true** if the user is allowed to set wallpapers; returns **false** otherwise.|
E
ester.zhou 已提交
320 321 322

**Example**

E
ester.zhou 已提交
323 324 325 326 327 328 329 330 331
  ```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 已提交
332 333 334 335 336 337


## wallpaper.isOperationAllowed

isOperationAllowed(): Promise<boolean>

E
ester.zhou 已提交
338
Checks whether the user is allowed to set wallpapers. This API uses a promise to return the result.
Z
zengyawen 已提交
339 340 341

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

E
ester.zhou 已提交
342
**Return value**
Z
zengyawen 已提交
343

E
ester.zhou 已提交
344
| Type| Description|
E
ester.zhou 已提交
345
| -------- | -------- |
E
ester.zhou 已提交
346
| Promise<boolean> | Promise used to return the result. Returns **true** if the user is allowed to set wallpapers; returns **false** otherwise.|
E
ester.zhou 已提交
347 348 349

**Example**

E
ester.zhou 已提交
350 351 352 353 354 355 356
  ```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 已提交
357 358 359 360 361 362


## wallpaper.reset

reset(wallpaperType: WallpaperType, callback: AsyncCallback<void>): void

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

E
ester.zhou 已提交
365
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
366 367 368

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

E
ester.zhou 已提交
369
**Parameters**
Z
zengyawen 已提交
370

E
ester.zhou 已提交
371
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
372
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
373 374
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the operation is successful, the result of removal is returned. Otherwise, error information is returned.|
E
ester.zhou 已提交
375 376 377

**Example**

E
ester.zhou 已提交
378 379 380 381 382 383 384 385 386
  ```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 已提交
387 388 389 390 391 392


## wallpaper.reset

reset(wallpaperType: WallpaperType): Promise<void>

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

E
ester.zhou 已提交
395
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
396 397 398

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

E
ester.zhou 已提交
399
**Parameters**
Z
zengyawen 已提交
400

E
ester.zhou 已提交
401
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
402
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
403
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
Z
zengyawen 已提交
404

E
ester.zhou 已提交
405 406
**Return value**

E
ester.zhou 已提交
407
| Type| Description|
E
ester.zhou 已提交
408
| -------- | -------- |
E
ester.zhou 已提交
409
| Promise<void> | Promise used to return the result. If the operation is successful, the result is returned. Otherwise, error information is returned.|
E
ester.zhou 已提交
410 411 412

**Example**

E
ester.zhou 已提交
413 414 415 416 417 418 419
  ```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 已提交
420 421 422 423 424 425


## wallpaper.setWallpaper

setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback<void>): void

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

E
ester.zhou 已提交
428
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
429 430 431

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

E
ester.zhou 已提交
432 433
**Parameters**

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

**Example**

E
ester.zhou 已提交
442 443
  ```js
  //The source type is string.
E
ester.zhou 已提交
444 445 446 447 448 449 450 451 452
  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 已提交
453
  // The source type is image.PixelMap.
E
ester.zhou 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  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));
  });
E
ester.zhou 已提交
473
  ```
Z
zengyawen 已提交
474 475 476 477 478 479


## wallpaper.setWallpaper

setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise<void>

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

E
ester.zhou 已提交
482
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
483 484 485

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

E
ester.zhou 已提交
486
**Parameters**
Z
zengyawen 已提交
487

E
ester.zhou 已提交
488
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
489
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
490 491
| source | string \|[PixelMap](js-apis-image.md#pixelmap7) | Yes| URI of a JPEG or PNG file, or bitmap of a PNG file.|
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
Z
zengyawen 已提交
492

E
ester.zhou 已提交
493 494
**Return value**

E
ester.zhou 已提交
495
| Type| Description|
E
ester.zhou 已提交
496
| -------- | -------- |
E
ester.zhou 已提交
497
| Promise<void> | Promise used to return the result. If the operation is successful, the setting result is returned. Otherwise, error information is returned.|
E
ester.zhou 已提交
498 499 500

**Example**

E
ester.zhou 已提交
501 502
  ```js
  // The source type is string.
E
ester.zhou 已提交
503 504 505 506 507 508 509
  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 已提交
510
  // The source type is image.PixelMap.
E
ester.zhou 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
  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));
  });
E
ester.zhou 已提交
528
  ```
Z
zengyawen 已提交
529 530 531 532 533

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

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

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

536
**Required permissions**: ohos.permission.GET_WALLPAPER
Z
zengyawen 已提交
537 538 539

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

E
ester.zhou 已提交
540
**Parameters**
Z
zengyawen 已提交
541

E
ester.zhou 已提交
542
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
543
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
544 545
| 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.|
E
ester.zhou 已提交
546 547 548

**Example**

E
ester.zhou 已提交
549 550 551 552 553 554 555 556 557
  ```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 已提交
558 559 560 561 562

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

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

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

565
**Required permissions**: ohos.permission.GET_WALLPAPER
Z
zengyawen 已提交
566 567 568

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

E
ester.zhou 已提交
569
**Parameters**
Z
zengyawen 已提交
570

E
ester.zhou 已提交
571
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
572
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
573
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
Z
zengyawen 已提交
574

E
ester.zhou 已提交
575 576
**Return value**

E
ester.zhou 已提交
577
| Type| Description|
E
ester.zhou 已提交
578
| -------- | -------- |
E
ester.zhou 已提交
579
| 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.|
E
ester.zhou 已提交
580 581 582

**Example**

E
ester.zhou 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595
  ```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;

E
ester.zhou 已提交
596
Obtains the pixel map for the wallpaper of the specified type. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
597

598
**Required permissions**: ohos.permission.GET_WALLPAPER
E
ester.zhou 已提交
599 600 601

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

E
ester.zhou 已提交
602 603
**System API**: This is a system API and cannot be called by third-party applications.

E
ester.zhou 已提交
604 605 606 607 608
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | Yes| Wallpaper type.|
E
ester.zhou 已提交
609
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. Returns the pixel map size of the wallpaper if the operation is successful; returns an error message otherwise.|
E
ester.zhou 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

**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;

E
ester.zhou 已提交
625
Obtains the pixel map for the wallpaper of the specified type. This API uses a promise to return the result.
E
ester.zhou 已提交
626

627
**Required permissions**: ohos.permission.GET_WALLPAPER
E
ester.zhou 已提交
628 629 630

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

E
ester.zhou 已提交
631 632
**System API**: This is a system API and cannot be called by third-party applications.

E
ester.zhou 已提交
633 634 635 636 637 638 639 640 641 642
**Parameters**

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

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
643
| Promise&lt;void&gt; | Promise used to return the result. Returns the pixel map size of the wallpaper if the operation is successful; returns an error message otherwise.|
E
ester.zhou 已提交
644 645 646 647

**Example**

  ```js
E
ester.zhou 已提交
648
  wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
E
ester.zhou 已提交
649 650 651 652 653 654 655
      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 已提交
656 657 658 659 660 661 662 663 664 665


## 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 已提交
666
**Parameters**
Z
zengyawen 已提交
667

E
ester.zhou 已提交
668
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
669
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
670 671
| type | string | Yes| Type of the event to subscribe to. The value **'colorChange'** indicates subscribing to the wallpaper color change event.|
| 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 已提交
672 673 674

**Example**

E
ester.zhou 已提交
675 676 677 678 679 680
  ```js
  let listener = (colors, wallpaperType) => {
      console.log(`wallpaper color changed.`);
  };
  wallpaper.on('colorChange', listener);
  ```
Z
zengyawen 已提交
681 682 683 684 685 686 687 688 689 690


## 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 已提交
691
**Parameters**
Z
zengyawen 已提交
692

E
ester.zhou 已提交
693
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
694
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
695 696
| type | string | Yes| Type of the event to unsubscribe from. The value **colorChange** indicates unsubscribing from the wallpaper color change event.|
| 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 已提交
697 698 699

**Example**

E
ester.zhou 已提交
700 701 702 703 704
  ```js
  let listener = (colors, wallpaperType) => {
      console.log(`wallpaper color changed.`);
  };
  wallpaper.on('colorChange', listener);
E
ester.zhou 已提交
705 706 707 708
  // Unsubscribe from the listener.
  wallpaper.off('colorChange', listener);
  // Unsubscribe from all subscriptions of the colorChange type.
  wallpaper.off('colorChange');
E
ester.zhou 已提交
709
  ```
Z
zengyawen 已提交
710 711 712 713 714 715


## RgbaColor

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

E
ester.zhou 已提交
716
| Name| Type| Readable| Writable| Description|
Z
zengyawen 已提交
717
| -------- | -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
718 719 720 721
| 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.|