js-apis-wallpaper.md 35.8 KB
Newer Older
E
ester.zhou 已提交
1
# @ohos.wallpaper (Wallpaper)
Z
zengyawen 已提交
2

E
esterzhou 已提交
3
The **wallpaper** module is a system service module in OpenHarmony that provides the wallpaper management service. You can use the APIs of this module to show, set, and switch between wallpapers.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
> **NOTE**
E
ester.zhou 已提交
6
> 
E
ester.zhou 已提交
7
> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10 11 12


## Modules to Import


E
ester.zhou 已提交
13
```js
Z
zengyawen 已提交
14 15 16
import wallpaper from '@ohos.wallpaper';
```

E
ester.zhou 已提交
17
## WallpaperType<sup>7+</sup>
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
Enumerates the wallpaper types.
Z
zengyawen 已提交
20 21 22

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

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


E
ester.zhou 已提交
29
## RgbaColor<sup>(deprecated)</sup>
Z
zengyawen 已提交
30

E
ester.zhou 已提交
31
Defines the RGBA color space for the wallpaper.
Z
zengyawen 已提交
32

E
ester.zhou 已提交
33 34 35
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9.
Z
zengyawen 已提交
36

E
ester.zhou 已提交
37
**System capability**: SystemCapability.MiscServices.Wallpaper
E
ester.zhou 已提交
38

E
ester.zhou 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| 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.|


## wallpaper.getColorsSync<sup>9+</sup>

getColorsSync(wallpaperType: WallpaperType): Array&lt;RgbaColor&gt;

Obtains the main color information of the wallpaper of the specified type.
E
ester.zhou 已提交
52

Z
zengyawen 已提交
53 54
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
55 56
**System API**: This is a system API.

E
ester.zhou 已提交
57
**Parameters**
E
ester.zhou 已提交
58 59

| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
60
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
61
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
62

E
ester.zhou 已提交
63
**Return value**
Z
zengyawen 已提交
64

E
ester.zhou 已提交
65 66
| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
67
| Array&lt;[RgbaColor](#rgbacolordeprecated)&gt; | Promise used to return the main color information of the wallpaper.|
Z
zengyawen 已提交
68

E
ester.zhou 已提交
69
**Example**
Z
zengyawen 已提交
70

E
ester.zhou 已提交
71
```js
E
esterzhou 已提交
72 73 74 75 76 77
try {
    let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
    console.log(`success to getColorsSync: ${JSON.stringify(colors)}`);
} catch (error) {
    console.error(`failed to getColorsSync because: ${JSON.stringify(error)}`);
}
E
ester.zhou 已提交
78
```
Z
zengyawen 已提交
79

E
ester.zhou 已提交
80
## wallpaper.getMinHeightSync<sup>9+</sup>
Z
zengyawen 已提交
81

E
ester.zhou 已提交
82
getMinHeightSync(): number
E
ester.zhou 已提交
83

E
ester.zhou 已提交
84
Obtains the minimum height of this wallpaper.
E
ester.zhou 已提交
85

E
ester.zhou 已提交
86 87
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
88 89
**System API**: This is a system API.

E
ester.zhou 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
**Return value**

| Type| Description|
| -------- | -------- |
| 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.|

**Example**

```js
let minHeight = wallpaper.getMinHeightSync();
```

## wallpaper.getMinWidthSync<sup>9+</sup>

getMinWidthSync(): number

Obtains the minimum width of this wallpaper.

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

E
ester.zhou 已提交
110 111
**System API**: This is a system API.

E
ester.zhou 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
**Return value**

| Type| Description|
| -------- | -------- |
| number | Promise used to return the minimum wallpaper width, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default width should be used instead.|

**Example**

```js
let minWidth = wallpaper.getMinWidthSync();
```

## wallpaper.restore<sup>9+</sup>

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

Resets the wallpaper of the specified type to the default wallpaper. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.SET_WALLPAPER

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

E
ester.zhou 已提交
134 135
**System API**: This is a system API.

E
ester.zhou 已提交
136 137 138 139
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
140
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
esterzhou 已提交
141
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the wallpaper is reset, **err** is **undefined**. Otherwise, **err** is an error object.|
E
ester.zhou 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

**Example**

```js
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
    if (error) {
        console.error(`failed to restore because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to restore.`);
});
```

## wallpaper.restore<sup>9+</sup>

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

Resets the wallpaper of the specified type to the default wallpaper. This API uses a promise to return the result.

**Required permissions**: ohos.permission.SET_WALLPAPER
E
ester.zhou 已提交
162 163 164

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

E
ester.zhou 已提交
165 166
**System API**: This is a system API.

E
ester.zhou 已提交
167 168 169 170
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
171
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
172 173 174 175 176

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
177
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
178 179 180

**Example**

E
ester.zhou 已提交
181 182 183 184 185 186 187
```js 
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to restore.`);
  }).catch((error) => {
    console.error(`failed to restore because: ${JSON.stringify(error)}`);
});
```
E
ester.zhou 已提交
188

E
ester.zhou 已提交
189
## wallpaper.setImage<sup>9+</sup>
Z
zengyawen 已提交
190

E
ester.zhou 已提交
191
setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
192

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

E
ester.zhou 已提交
195
**Required permissions**: ohos.permission.SET_WALLPAPER
E
ester.zhou 已提交
196

Z
zengyawen 已提交
197 198
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
199 200
**System API**: This is a system API.

E
ester.zhou 已提交
201
**Parameters**
Z
zengyawen 已提交
202

E
ester.zhou 已提交
203
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
204
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
205
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | Yes| URI of a JPEG or PNG file, or pixel map of a PNG file.|
E
ester.zhou 已提交
206
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
esterzhou 已提交
207
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the wallpaper is set, **err** is **undefined**. Otherwise, **err** is an error object.|
E
ester.zhou 已提交
208 209 210

**Example**

E
ester.zhou 已提交
211
```js
E
esterzhou 已提交
212
// The source type is string.
E
ester.zhou 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
    if (error) {
        console.error(`failed to setImage because: ${JSON.stringify(error)}`);
        return;
     }
    console.log(`success to setImage.`);
});
  
// The source type is image.PixelMap.
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.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
        if (error) {
            console.error(`failed to setImage because: ${JSON.stringify(error)}`);
            return;
        }
        console.log(`success to setImage.`);
    });
}).catch((error) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});
```
Z
zengyawen 已提交
243

E
ester.zhou 已提交
244
## wallpaper.setImage<sup>9+</sup>
Z
zengyawen 已提交
245

E
ester.zhou 已提交
246
setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;
Z
zengyawen 已提交
247

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

E
ester.zhou 已提交
250
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
251

E
ester.zhou 已提交
252
**System capability**: SystemCapability.MiscServices.Wallpaper
E
ester.zhou 已提交
253

E
ester.zhou 已提交
254 255
**System API**: This is a system API.

E
ester.zhou 已提交
256
**Parameters**
Z
zengyawen 已提交
257

E
ester.zhou 已提交
258
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
259
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
260
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | Yes| URI of a JPEG or PNG file, or pixel map of a PNG file.|
E
ester.zhou 已提交
261
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
Z
zengyawen 已提交
262

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

E
ester.zhou 已提交
265
| Type| Description|
E
ester.zhou 已提交
266
| -------- | -------- |
E
ester.zhou 已提交
267
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
268 269 270

**Example**

E
ester.zhou 已提交
271
```js
E
esterzhou 已提交
272
// The source type is string.
E
ester.zhou 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to setImage.`);
}).catch((error) => {
    console.error(`failed to setImage because: ${JSON.stringify(error)}`);
});

// The source type is image.PixelMap.
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.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.log(`success to setImage.`);
    }).catch((error) => {
        console.error(`failed to setImage because: ${JSON.stringify(error)}`);
    });
}).catch((error) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
});
```
Z
zengyawen 已提交
299

E
ester.zhou 已提交
300
## wallpaper.getImage<sup>9+</sup>
Z
zengyawen 已提交
301

E
ester.zhou 已提交
302
getImage(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;
Z
zengyawen 已提交
303

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

E
ester.zhou 已提交
306
**Required permissions**: ohos.permission.GET_WALLPAPER
E
ester.zhou 已提交
307

Z
zengyawen 已提交
308 309
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
310 311
**System API**: This is a system API.

E
ester.zhou 已提交
312
**Parameters**
Z
zengyawen 已提交
313

E
ester.zhou 已提交
314
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
315
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
316
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
esterzhou 已提交
317
| callback | AsyncCallback&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | Yes| Callback used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.|
E
ester.zhou 已提交
318 319 320

**Example**

E
ester.zhou 已提交
321 322 323 324 325 326 327 328 329
```js
wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM, function (error, data) {
    if (error) {
        console.error(`failed to getImage because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getImage: ${JSON.stringify(data)}`);
});
```
Z
zengyawen 已提交
330 331


E
ester.zhou 已提交
332
## wallpaper.getImage<sup>9+</sup>
Z
zengyawen 已提交
333

E
ester.zhou 已提交
334
getImage(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;
Z
zengyawen 已提交
335

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

E
ester.zhou 已提交
338
**Required permissions**: ohos.permission.GET_WALLPAPER
Z
zengyawen 已提交
339

E
ester.zhou 已提交
340
**System capability**: SystemCapability.MiscServices.Wallpaper
E
ester.zhou 已提交
341

E
ester.zhou 已提交
342 343 344 345 346 347
**System API**: This is a system API.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
348
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
349

E
ester.zhou 已提交
350
**Return value**
Z
zengyawen 已提交
351

E
ester.zhou 已提交
352
| Type| Description|
E
ester.zhou 已提交
353
| -------- | -------- |
E
esterzhou 已提交
354
| Promise&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | Promise used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.|
Z
zengyawen 已提交
355

E
ester.zhou 已提交
356 357
**Example**

E
ester.zhou 已提交
358 359 360
```js
wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
    console.log(`success to getImage: ${JSON.stringify(data)}`);
E
ester.zhou 已提交
361
  }).catch((error) => {
E
ester.zhou 已提交
362 363 364
    console.error(`failed to getImage because: ${JSON.stringify(error)}`);
});
```
Z
zengyawen 已提交
365

E
ester.zhou 已提交
366
## wallpaper.on('colorChange')<sup>(deprecated)</sup>
E
ester.zhou 已提交
367

E
ester.zhou 已提交
368
on(type: 'colorChange', callback: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
E
ester.zhou 已提交
369

E
ester.zhou 已提交
370
Subscribes to the wallpaper color change event.
E
ester.zhou 已提交
371

E
ester.zhou 已提交
372 373 374 375
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9.

E
ester.zhou 已提交
376 377
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
378
**Parameters**
E
ester.zhou 已提交
379

E
ester.zhou 已提交
380 381 382
| 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 已提交
383
| 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](#rgbacolordeprecated).<br>- wallpaperType<br>  Wallpaper type.|
E
ester.zhou 已提交
384 385 386

**Example**

E
ester.zhou 已提交
387
```js
E
esterzhou 已提交
388 389 390 391 392 393 394 395
try {
    let listener = (colors, wallpaperType) => {
        console.log(`wallpaper color changed.`);
    };
    wallpaper.on('colorChange', listener);
} catch (error) {
    console.error(`failed to on because: ${JSON.stringify(error)}`);
}
E
ester.zhou 已提交
396
```
Z
zengyawen 已提交
397

E
ester.zhou 已提交
398
## wallpaper.off('colorChange')<sup>(deprecated)</sup>
Z
zengyawen 已提交
399

E
ester.zhou 已提交
400
off(type: 'colorChange', callback?: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void
Z
zengyawen 已提交
401

E
ester.zhou 已提交
402
Unsubscribes from the wallpaper color change event.
Z
zengyawen 已提交
403

E
ester.zhou 已提交
404 405 406 407
> **NOTE**
> 
> This API is supported since API version 7 and deprecated since API version 9.

E
ester.zhou 已提交
408
**System capability**: SystemCapability.MiscServices.Wallpaper
E
ester.zhou 已提交
409

E
ester.zhou 已提交
410
**Parameters**
Z
zengyawen 已提交
411

E
ester.zhou 已提交
412
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
413
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
414
| type | string | Yes| Type of the event to unsubscribe from. The value **'colorChange'** indicates unsubscribing from the wallpaper color change event.|
E
ester.zhou 已提交
415
| callback | function | No|   Callback for the wallpaper color change event. If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.<br>- colors<br>  Main color information of the wallpaper. For details, see [RgbaColor](#rgbacolordeprecated).<br>- wallpaperType<br>  Wallpaper type.|
Z
zengyawen 已提交
416

E
ester.zhou 已提交
417 418
**Example**

E
ester.zhou 已提交
419 420 421 422
```js
let listener = (colors, wallpaperType) => {
    console.log(`wallpaper color changed.`);
};
E
esterzhou 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
try {
    wallpaper.on('colorChange', listener);
} catch (error) {
    console.error(`failed to on because: ${JSON.stringify(error)}`);
}

try {
    // Unsubscribe from the listener.
    wallpaper.off('colorChange', listener);
} catch (error) {
    console.error(`failed to off because: ${JSON.stringify(error)}`);
}

try {
    // Unsubscribe from all subscriptions of the colorChange type.
    wallpaper.off('colorChange');
} catch (error) {
    console.error(`failed to off because: ${JSON.stringify(error)}`);
}
E
ester.zhou 已提交
442
```
Z
zengyawen 已提交
443

E
ester.zhou 已提交
444
## wallpaper.getColors<sup>(deprecated)</sup>
Z
zengyawen 已提交
445

E
ester.zhou 已提交
446
getColors(wallpaperType: WallpaperType, callback: AsyncCallback&lt;Array&lt;RgbaColor&gt;&gt;): void
Z
zengyawen 已提交
447

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

E
ester.zhou 已提交
450 451
> **NOTE**
> 
E
ester.zhou 已提交
452
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
453

Z
zengyawen 已提交
454 455
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
456
**Parameters**
Z
zengyawen 已提交
457

E
ester.zhou 已提交
458 459
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
460 461
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
| callback | AsyncCallback&lt;Array&lt;[RgbaColor](#rgbacolordeprecated)&gt;&gt; | Yes| Callback used to return the main color information of the wallpaper.|
E
ester.zhou 已提交
462 463 464

**Example**

E
ester.zhou 已提交
465 466 467 468 469 470 471 472 473
```js
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: ${JSON.stringify(data)}`);
});
```
Z
zengyawen 已提交
474

E
ester.zhou 已提交
475
## wallpaper.getColors<sup>(deprecated)</sup>
Z
zengyawen 已提交
476

E
ester.zhou 已提交
477
getColors(wallpaperType: WallpaperType): Promise&lt;Array&lt;RgbaColor&gt;&gt;
E
ester.zhou 已提交
478

E
ester.zhou 已提交
479
Obtains the main color information of the wallpaper of the specified type. This API uses a promise to return the result.
E
ester.zhou 已提交
480

E
ester.zhou 已提交
481 482
> **NOTE**
> 
E
ester.zhou 已提交
483
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
484 485 486

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

E
ester.zhou 已提交
487 488 489 490
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
491
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
492

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

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
497
| Promise&lt;Array&lt;[RgbaColor](#rgbacolordeprecated)&gt;&gt; | Promise used to return the main color information of the wallpaper.|
E
ester.zhou 已提交
498 499 500

**Example**

E
ester.zhou 已提交
501 502 503 504 505 506 507
```js
wallpaper.getColors(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
    console.log(`success to getColors: ${JSON.stringify(data)}`);
  }).catch((error) => {
    console.error(`failed to getColors because: ${JSON.stringify(error)}`);
});
```
E
ester.zhou 已提交
508

E
ester.zhou 已提交
509
## wallpaper.getId<sup>(deprecated)</sup>
Z
zengyawen 已提交
510

E
ester.zhou 已提交
511
getId(wallpaperType: WallpaperType, callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
512

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

E
ester.zhou 已提交
515 516
> **NOTE**
> 
E
ester.zhou 已提交
517
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
518

Z
zengyawen 已提交
519 520
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
521
**Parameters**
Z
zengyawen 已提交
522

E
ester.zhou 已提交
523
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
524
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
525
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
esterzhou 已提交
526
| 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).|
E
ester.zhou 已提交
527 528 529

**Example**

E
ester.zhou 已提交
530 531 532 533 534 535 536 537 538
```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 已提交
539

E
ester.zhou 已提交
540
## wallpaper.getId<sup>(deprecated)</sup>
Z
zengyawen 已提交
541

E
ester.zhou 已提交
542
getId(wallpaperType: WallpaperType): Promise&lt;number&gt;
Z
zengyawen 已提交
543

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

E
ester.zhou 已提交
546 547
> **NOTE**
> 
E
ester.zhou 已提交
548
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
549

Z
zengyawen 已提交
550 551
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
552 553 554 555
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
556
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
557

E
ester.zhou 已提交
558
**Return value**
Z
zengyawen 已提交
559

E
ester.zhou 已提交
560
| Type| Description|
E
ester.zhou 已提交
561
| -------- | -------- |
E
esterzhou 已提交
562
| 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).|
E
ester.zhou 已提交
563 564 565

**Example**

E
ester.zhou 已提交
566 567 568
```js
wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
    console.log(`success to getId: ${JSON.stringify(data)}`);
E
ester.zhou 已提交
569
  }).catch((error) => {
E
ester.zhou 已提交
570 571 572
    console.error(`failed to getId because: ${JSON.stringify(error)}`);
});
```
Z
zengyawen 已提交
573

E
ester.zhou 已提交
574
## wallpaper.getMinHeight<sup>(deprecated)</sup>
Z
zengyawen 已提交
575

E
ester.zhou 已提交
576
getMinHeight(callback: AsyncCallback&lt;number&gt;): void
E
ester.zhou 已提交
577

E
ester.zhou 已提交
578
Obtains the minimum height of this wallpaper. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
579

E
ester.zhou 已提交
580 581
> **NOTE**
> 
E
ester.zhou 已提交
582
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
583 584 585

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

E
ester.zhou 已提交
586
**Parameters**
E
ester.zhou 已提交
587

E
ester.zhou 已提交
588 589 590
| 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.|
E
ester.zhou 已提交
591 592 593

**Example**

E
ester.zhou 已提交
594 595 596 597 598 599 600 601 602
```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)}`);
});
```
E
ester.zhou 已提交
603

E
ester.zhou 已提交
604
## wallpaper.getMinHeight<sup>(deprecated)</sup>
Z
zengyawen 已提交
605

E
ester.zhou 已提交
606
getMinHeight(): Promise&lt;number&gt;
Z
zengyawen 已提交
607

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

E
ester.zhou 已提交
610 611
> **NOTE**
> 
E
ester.zhou 已提交
612
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
613

Z
zengyawen 已提交
614 615
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
616
**Return value**
Z
zengyawen 已提交
617

E
ester.zhou 已提交
618 619 620
| 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.|
E
ester.zhou 已提交
621 622 623

**Example**

E
ester.zhou 已提交
624 625 626 627 628 629 630
```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 已提交
631

E
ester.zhou 已提交
632
## wallpaper.getMinWidth<sup>(deprecated)</sup>
Z
zengyawen 已提交
633

E
ester.zhou 已提交
634
getMinWidth(callback: AsyncCallback&lt;number&gt;): void
Z
zengyawen 已提交
635

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

E
ester.zhou 已提交
638 639
> **NOTE**
> 
E
ester.zhou 已提交
640
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
641

Z
zengyawen 已提交
642 643
**System capability**: SystemCapability.MiscServices.Wallpaper

E
ester.zhou 已提交
644
**Parameters**
Z
zengyawen 已提交
645

E
ester.zhou 已提交
646 647 648
| 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.|
E
ester.zhou 已提交
649 650 651

**Example**

E
ester.zhou 已提交
652 653 654 655 656 657 658 659 660
```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 已提交
661

E
ester.zhou 已提交
662
## wallpaper.getMinWidth<sup>(deprecated)</sup>
Z
zengyawen 已提交
663

E
ester.zhou 已提交
664
getMinWidth(): Promise&lt;number&gt;
E
ester.zhou 已提交
665

E
ester.zhou 已提交
666
Obtains the minimum width of this wallpaper. This API uses a promise to return the result.
E
ester.zhou 已提交
667

E
ester.zhou 已提交
668 669
> **NOTE**
> 
E
ester.zhou 已提交
670
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
671 672 673 674 675 676 677

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

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
678
| Promise&lt;number&gt; | Promise used to return the minimum wallpaper width, in pixels. If the return value is **0**, no wallpaper is set. In this case, the default width should be used instead.|
E
ester.zhou 已提交
679 680 681

**Example**

E
ester.zhou 已提交
682 683 684 685 686 687 688
```js
wallpaper.getMinWidth().then((data) => {
    console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
  }).catch((error) => {
    console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
});
```
E
ester.zhou 已提交
689

E
ester.zhou 已提交
690
## wallpaper.isChangePermitted<sup>(deprecated)</sup>
Z
zengyawen 已提交
691

E
ester.zhou 已提交
692
isChangePermitted(callback: AsyncCallback&lt;boolean&gt;): void
Z
zengyawen 已提交
693

E
ester.zhou 已提交
694
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 已提交
695

E
ester.zhou 已提交
696 697
> **NOTE**
> 
E
ester.zhou 已提交
698
> This API is supported since API version 7 and deprecated since API version 9.
Z
zengyawen 已提交
699 700 701

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

E
ester.zhou 已提交
702
**Parameters**
Z
zengyawen 已提交
703

E
ester.zhou 已提交
704
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
705
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
706
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether to allow the application to change the wallpaper for the current user. The value **true** means that the operation is allowed, and **false** means the opposite.|
E
ester.zhou 已提交
707 708 709

**Example**

E
ester.zhou 已提交
710 711 712 713 714 715 716 717 718
```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 已提交
719

E
ester.zhou 已提交
720
## wallpaper.isChangePermitted<sup>(deprecated)</sup>
Z
zengyawen 已提交
721

E
ester.zhou 已提交
722
isChangePermitted(): Promise&lt;boolean&gt;
Z
zengyawen 已提交
723

E
ester.zhou 已提交
724
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 已提交
725

E
ester.zhou 已提交
726
> **NOTE**
E
ester.zhou 已提交
727
> 
E
ester.zhou 已提交
728
> This API is supported since API version 7 and deprecated since API version 9.
Z
zengyawen 已提交
729 730 731

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

E
ester.zhou 已提交
732 733
**Return value**

E
ester.zhou 已提交
734
| Type| Description|
E
ester.zhou 已提交
735
| -------- | -------- |
E
ester.zhou 已提交
736
| Promise&lt;boolean&gt; | Promise used to return whether to allow the application to change the wallpaper for the current user. The value **true** means that the operation is allowed, and **false** means the opposite.|
E
ester.zhou 已提交
737 738 739

**Example**

E
ester.zhou 已提交
740 741 742 743 744 745 746
```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 已提交
747

E
ester.zhou 已提交
748
## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
E
ester.zhou 已提交
749

E
ester.zhou 已提交
750
isOperationAllowed(callback: AsyncCallback&lt;boolean&gt;): void
E
ester.zhou 已提交
751

E
ester.zhou 已提交
752
Checks whether the user is allowed to set wallpapers. This API uses an asynchronous callback to return the result.
E
ester.zhou 已提交
753

E
ester.zhou 已提交
754 755
> **NOTE**
> 
E
ester.zhou 已提交
756
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
757 758 759 760 761 762 763

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
764
| callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return whether the user is allowed to set wallpapers. The value **true** means that the operation is allowed, and **false** means the opposite.|
E
ester.zhou 已提交
765 766 767

**Example**

E
ester.zhou 已提交
768 769 770 771 772 773 774 775 776
```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)}`);
});
```
E
ester.zhou 已提交
777

E
ester.zhou 已提交
778
## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
E
ester.zhou 已提交
779

E
ester.zhou 已提交
780
isOperationAllowed(): Promise&lt;boolean&gt;
E
ester.zhou 已提交
781

E
ester.zhou 已提交
782
Checks whether the user is allowed to set wallpapers. This API uses a promise to return the result.
E
ester.zhou 已提交
783

E
ester.zhou 已提交
784 785
> **NOTE**
> 
E
ester.zhou 已提交
786
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
787 788 789 790 791 792 793

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

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
794
| Promise&lt;boolean&gt; | Promise used to return whether the user is allowed to set wallpapers. The value **true** means that the operation is allowed, and **false** means the opposite.|
E
ester.zhou 已提交
795 796 797

**Example**

E
ester.zhou 已提交
798 799 800
```js
wallpaper.isOperationAllowed().then((data) => {
    console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
E
ester.zhou 已提交
801
  }).catch((error) => {
E
ester.zhou 已提交
802 803 804
    console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
});
```
E
ester.zhou 已提交
805

E
ester.zhou 已提交
806
## wallpaper.reset<sup>(deprecated)</sup>
Z
zengyawen 已提交
807

E
ester.zhou 已提交
808
reset(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
809

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

E
ester.zhou 已提交
812 813
> **NOTE**
> 
E
ester.zhou 已提交
814
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
815

E
ester.zhou 已提交
816
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
817 818 819

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

E
ester.zhou 已提交
820 821
**Parameters**

E
ester.zhou 已提交
822
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
823
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
824
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
esterzhou 已提交
825
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the wallpaper is reset, **err** is **undefined**. Otherwise, **err** is an error object.|
E
ester.zhou 已提交
826 827 828

**Example**

E
ester.zhou 已提交
829 830 831 832 833 834 835 836 837
```js
wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
    if (error) {
        console.error(`failed to reset because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to reset.`);
});
```
Z
zengyawen 已提交
838

E
ester.zhou 已提交
839
## wallpaper.reset<sup>(deprecated)</sup>
Z
zengyawen 已提交
840

E
ester.zhou 已提交
841
reset(wallpaperType: WallpaperType): Promise&lt;void&gt;
Z
zengyawen 已提交
842

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

E
ester.zhou 已提交
845
> **NOTE**
E
ester.zhou 已提交
846
>
E
ester.zhou 已提交
847
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
848

E
ester.zhou 已提交
849
**Required permissions**: ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
850 851 852

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

E
ester.zhou 已提交
853
**Parameters**
Z
zengyawen 已提交
854

E
ester.zhou 已提交
855
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
856
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
857
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
Z
zengyawen 已提交
858

E
ester.zhou 已提交
859 860
**Return value**

E
ester.zhou 已提交
861
| Type| Description|
E
ester.zhou 已提交
862
| -------- | -------- |
E
ester.zhou 已提交
863
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
864 865 866

**Example**

E
ester.zhou 已提交
867 868 869 870 871 872 873
```js
wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to reset.`);
}).catch((error) => {
    console.error(`failed to reset because: ${JSON.stringify(error)}`);
});
```
E
ester.zhou 已提交
874

E
ester.zhou 已提交
875
## wallpaper.setWallpaper<sup>(deprecated)</sup>
E
ester.zhou 已提交
876

E
ester.zhou 已提交
877
setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
E
ester.zhou 已提交
878 879 880

Sets a specified source as the wallpaper of a specified type. This API uses an asynchronous callback to return the result.

E
ester.zhou 已提交
881 882
> **NOTE**
> 
E
ester.zhou 已提交
883
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
884

E
ester.zhou 已提交
885 886 887 888 889 890 891 892
**Required permissions**: ohos.permission.SET_WALLPAPER

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
893
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | Yes| URI of a JPEG or PNG file, or pixel map of a PNG file.|
E
ester.zhou 已提交
894
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
esterzhou 已提交
895
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the wallpaper is set, **err** is **undefined**. Otherwise, **err** is an error object.|
E
ester.zhou 已提交
896 897 898

**Example**

E
ester.zhou 已提交
899
```js
E
esterzhou 已提交
900
// The source type is string.
E
ester.zhou 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
    if (error) {
        console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
       return;
       }
    console.log(`success to setWallpaper.`);
});

// The source type is image.PixelMap.
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) => {
        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 已提交
931

E
ester.zhou 已提交
932
## wallpaper.setWallpaper<sup>(deprecated)</sup>
E
ester.zhou 已提交
933

E
ester.zhou 已提交
934
setWallpaper(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;
E
ester.zhou 已提交
935

E
ester.zhou 已提交
936
Sets a specified source as the wallpaper of a specified type. This API uses a promise to return the result.
E
ester.zhou 已提交
937

E
ester.zhou 已提交
938 939
> **NOTE**
> 
E
ester.zhou 已提交
940
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
941 942 943 944 945 946 947 948 949

**Required permissions**: ohos.permission.SET_WALLPAPER

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
esterzhou 已提交
950
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | Yes| URI of a JPEG or PNG file, or pixel map of a PNG file.|
E
ester.zhou 已提交
951
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
952 953 954 955 956

**Return value**

| Type| Description|
| -------- | -------- |
E
ester.zhou 已提交
957
| Promise&lt;void&gt; | Promise that returns no value.|
E
ester.zhou 已提交
958 959 960

**Example**

E
ester.zhou 已提交
961
```js
E
esterzhou 已提交
962
// The source type is string.
E
ester.zhou 已提交
963 964 965
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to setWallpaper.`);
E
ester.zhou 已提交
966
  }).catch((error) => {
E
ester.zhou 已提交
967 968
    console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
});
E
ester.zhou 已提交
969
  
E
ester.zhou 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
// The source type is image.PixelMap.
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(() => {
        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 已提交
989 990


E
ester.zhou 已提交
991
## wallpaper.getFile<sup>(deprecated)</sup>
Z
zengyawen 已提交
992 993 994

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

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

E
ester.zhou 已提交
997 998
> **NOTE**
> 
E
ester.zhou 已提交
999
> This API is supported since API version 8 and deprecated since API version 9.
E
ester.zhou 已提交
1000

1001
**Required permissions**: ohos.permission.GET_WALLPAPER
Z
zengyawen 已提交
1002 1003 1004

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

E
ester.zhou 已提交
1005
**Parameters**
Z
zengyawen 已提交
1006

E
ester.zhou 已提交
1007
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
1008
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
1009
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
1010
| 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 已提交
1011 1012 1013

**Example**

E
ester.zhou 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022
```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 已提交
1023

E
ester.zhou 已提交
1024
## wallpaper.getFile<sup>(deprecated)</sup>
Z
zengyawen 已提交
1025 1026 1027

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

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

E
ester.zhou 已提交
1030 1031
> **NOTE**
>
E
ester.zhou 已提交
1032
> This API is supported since API version 8 and deprecated since API version 9.
E
ester.zhou 已提交
1033

1034
**Required permissions**: ohos.permission.GET_WALLPAPER
Z
zengyawen 已提交
1035 1036 1037

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

E
ester.zhou 已提交
1038
**Parameters**
Z
zengyawen 已提交
1039

E
ester.zhou 已提交
1040
| Name| Type| Mandatory| Description|
E
ester.zhou 已提交
1041
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
1042
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
Z
zengyawen 已提交
1043

E
ester.zhou 已提交
1044 1045
**Return value**

E
ester.zhou 已提交
1046
| Type| Description|
E
ester.zhou 已提交
1047
| -------- | -------- |
E
ester.zhou 已提交
1048
| 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 已提交
1049 1050 1051

**Example**

E
ester.zhou 已提交
1052 1053 1054
```js
wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
    console.log(`success to getFile: ${JSON.stringify(data)}`);
E
ester.zhou 已提交
1055
  }).catch((error) => {
E
ester.zhou 已提交
1056 1057 1058
    console.error(`failed to getFile because: ${JSON.stringify(error)}`);
});
```
E
ester.zhou 已提交
1059

E
ester.zhou 已提交
1060
## wallpaper.getPixelMap<sup>(deprecated)</sup>
E
ester.zhou 已提交
1061 1062 1063

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

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

E
ester.zhou 已提交
1066 1067
> **NOTE**
>
E
ester.zhou 已提交
1068
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
1069

1070
**Required permissions**: ohos.permission.GET_WALLPAPER
E
ester.zhou 已提交
1071 1072 1073

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

E
ester.zhou 已提交
1074
**System API**: This is a system API.
E
ester.zhou 已提交
1075

E
ester.zhou 已提交
1076 1077 1078 1079
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
1080
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
esterzhou 已提交
1081
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes| Callback used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.|
E
ester.zhou 已提交
1082 1083 1084

**Example**

E
ester.zhou 已提交
1085 1086 1087 1088 1089 1090 1091
```js
wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, function (error, data) {
    if (error) {
        console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
        return;
    }
    console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
E
ester.zhou 已提交
1092
  });
E
ester.zhou 已提交
1093
```
E
ester.zhou 已提交
1094

E
ester.zhou 已提交
1095
## wallpaper.getPixelMap<sup>(deprecated)</sup>
E
ester.zhou 已提交
1096 1097 1098

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

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

E
ester.zhou 已提交
1101 1102
> **NOTE**
>
E
ester.zhou 已提交
1103
> This API is supported since API version 7 and deprecated since API version 9.
E
ester.zhou 已提交
1104

1105
**Required permissions**: ohos.permission.GET_WALLPAPER
E
ester.zhou 已提交
1106 1107 1108

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

E
ester.zhou 已提交
1109
**System API**: This is a system API.
E
ester.zhou 已提交
1110

E
ester.zhou 已提交
1111 1112 1113 1114
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
E
ester.zhou 已提交
1115
| wallpaperType | [WallpaperType](#wallpapertype7) | Yes| Wallpaper type.|
E
ester.zhou 已提交
1116 1117 1118 1119 1120

**Return value**

| Type| Description|
| -------- | -------- |
E
esterzhou 已提交
1121
| Promise&lt;image.PixelMap&gt; | Promise used to return the result. If the operation is successful, the pixel map of the wallpaper is returned. Otherwise, error information is returned.|
E
ester.zhou 已提交
1122 1123 1124

**Example**

E
ester.zhou 已提交
1125 1126 1127 1128 1129 1130 1131
```js
wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
    console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
  }).catch((error) => {
    console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
});
```