js-apis-wallpaper.md 35.0 KB
Newer Older
1
# @ohos.wallpaper (壁纸)
Z
zengyawen 已提交
2

3 4
壁纸管理服务是OpenHarmony中系统服务,是主题框架的部分组成,主要为系统提供壁纸管理服务能力,支持系统显示、设置、切换壁纸等功能。

5 6
> **说明:**
> 
Z
zengyawen 已提交
7 8 9 10 11
> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块

Z
zengyawen 已提交
12

13
```js
Z
zengyawen 已提交
14 15 16 17 18
import wallpaper from '@ohos.wallpaper';
```

## WallpaperType

19
定义壁纸的枚举类型。
Z
zengyawen 已提交
20

21
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
22

23 24 25 26
| 名称 | 值 |说明 |
| -------- | -------- |-------- |
| WALLPAPER_SYSTEM | 0 |主屏幕壁纸标识。 |
| WALLPAPER_LOCKSCREEN | 1 |锁屏壁纸标识。 |
Z
zengyawen 已提交
27 28


29
## RgbaColor
Z
zengyawen 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
定义壁纸颜色信息结构。

**系统能力**: SystemCapability.MiscServices.Wallpaper

| 名称 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| red | number | 是 | 是 | 表示红色值,范围为 0 到 255。 |
| green | number | 是 | 是 | 表示绿色值,范围为 0 到 255。 |
| blue | number | 是 | 是 | 表示蓝色值,范围为 0 到 255。 |
| alpha | number | 是 | 是 | 表示 alpha 值,范围为 0 到 255。 |


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

getColorsSync(wallpaperType: WallpaperType): Array&lt;RgbaColor&gt;
Z
zengyawen 已提交
46 47 48

获取指定类型壁纸的主要颜色信息。

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |

**返回值**

| 类型 | 说明 |
| -------- | -------- |
| Array&lt;[RgbaColor](#rgbacolor)&gt; | 返回壁纸的主要颜色信息。 |

**示例**

```js
let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```

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

getIdSync(wallpaperType: WallpaperType): number

获取指定类型壁纸的ID。
74

75
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
76

77 78
**参数:**

79 80 81
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
82 83 84 85 86

**返回值**

| 类型 | 说明 |
| -------- | -------- |
87
| number | 返回壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。 |
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

**示例**

```js
let id = wallpaper.getIdSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```

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

getMinHeightSync(): number

获取壁纸的最小高度值。

**系统能力**: SystemCapability.MiscServices.Wallpaper

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| number | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
Z
zengyawen 已提交
108

109
**示例:**
110

111
```js
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
let minHeight = wallpaper.getMinHeightSync();
```

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

getMinWidthSync(): number

获取壁纸的最小宽度值。

**系统能力**: SystemCapability.MiscServices.Wallpaper

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| number | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |

**示例:**

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

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

isChangeAllowed(): boolean

是否允许应用改变当前用户的壁纸。

**系统能力**: SystemCapability.MiscServices.Wallpaper

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |

**示例:**

```js
let isChangeAllowed = wallpaper.isChangeAllowed();
```

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

isUserChangeAllowed(): boolean

是否允许用户设置壁纸。

**系统能力**: SystemCapability.MiscServices.Wallpaper

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| boolean | 返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |

**示例:**

```js
let isUserChangeAllowed = wallpaper.isUserChangeAllowed();
```

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

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

移除指定类型的壁纸,恢复为默认显示的壁纸。

**需要权限**:ohos.permission.SET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,移除壁纸成功,error为undefined 否则返回error信息。 |

**示例:**

```js
wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
196
    if (error) {
197
        console.error(`failed to restore because: ${JSON.stringify(error)}`);
198 199
        return;
    }
200
    console.log(`success to restore.`);
201 202
});
```
Z
zengyawen 已提交
203

204
## wallpaper.restore<sup>9+</sup>
Z
zengyawen 已提交
205

206
restore(wallpaperType: WallpaperType): Promise&lt;void&gt;
Z
zengyawen 已提交
207

208
移除指定类型的壁纸,恢复为默认显示的壁纸。
Z
zengyawen 已提交
209

210
**需要权限**:ohos.permission.SET_WALLPAPER
211

212
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
213

214 215
**参数:**

216 217 218
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
Z
zengyawen 已提交
219

220 221
**返回值:**

222 223
| 类型 | 说明 |
| -------- | -------- |
224
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
Z
zengyawen 已提交
225

226
**示例:**
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

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

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

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

将指定资源设置为指定类型的壁纸。

**需要权限**:ohos.permission.SET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined 否则返回error信息。 |

**示例:**

```js
// source类型为string
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.`);
});
Z
zengyawen 已提交
266
  
267 268 269 270 271 272 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
// source类型为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)}`);
});
```

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

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

将指定资源设置为指定类型的壁纸。

**需要权限**:ohos.permission.SET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |

**示例:**

314
```js
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
// source类型为string
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)}`);
});

// source类型为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)}`);
});
```

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

getFileSync(wallpaperType: WallpaperType): number;

获取指定类型的壁纸文件。

**需要权限**:ohos.permission.GET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
| number | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |

**示例:**

```js
let file = wallpaper.getFileSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
```

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

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

获取壁纸图片的像素图。

**需要权限**:ohos.permission.GET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**系统接口**:此接口为系统接口。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
388
| callback | AsyncCallback&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

**示例:**

```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)}`);
});
```


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

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

获取壁纸图片的像素图。

**需要权限**:ohos.permission.GET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**系统接口**:此接口为系统接口。

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |

**返回值:**

| 类型 | 说明 |
| -------- | -------- |
425
| Promise&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
426 427 428 429 430 431

**示例:**

```js
wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
    console.log(`success to getImage: ${JSON.stringify(data)}`);
Z
zengyawen 已提交
432
  }).catch((error) => {
433
    console.error(`failed to getImage because: ${JSON.stringify(error)}`);
434 435
});
```
Z
zengyawen 已提交
436

437
## wallpaper.on('colorChange')<sup>9+</sup>
438

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
on(type: 'colorChange', callback: (colors: Array&lt;RgbaColor&gt;, wallpaperType: WallpaperType) =&gt; void): void

订阅壁纸颜色变化结果上报事件。

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 取值为'colorChange',表示壁纸颜色变化结果上报事件。 |
| callback | function | 是 | 壁纸颜色变化触发该回调方法,返回壁纸类型和壁纸的主要颜色信息。<br/>- colors<br/>  壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)<br/>- wallpaperType<br/>  壁纸类型。 |

**示例:**

```js
let listener = (colors, wallpaperType) => {
    console.log(`wallpaper color changed.`);
};
wallpaper.on('colorChange', listener);
```

## wallpaper.off('colorChange')<sup>9+</sup>

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

取消订阅壁纸颜色变化结果上报事件。

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| type | string | 是 | 取值为'colorChange',表示取消订阅壁纸颜色变化结果上报事件。 |
| callback | function | 否 |   表示要取消的壁纸颜色变化的回调,不填写该参数则取消订阅该type对应的所有回调。<br/>- colors<br/>  壁纸的主要颜色信息,其类型见[RgbaColor](#rgbacolor)<br/>- wallpaperType<br/>  壁纸类型。 |

**示例:**

```js
let listener = (colors, wallpaperType) => {
    console.log(`wallpaper color changed.`);
};
wallpaper.on('colorChange', listener);
// 取消订阅listener
wallpaper.off('colorChange', listener);
// 取消所有'colorChange'类型的订阅
wallpaper.off('colorChange');
```

## wallpaper.getColors<sup>(deprecated)</sup>

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

获取指定类型壁纸的主要颜色信息。

> **说明:**
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getColorsSync<sup>9+</sup>](#wallpapergetcolorssync9)代替。

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | 是 | 回调函数,返回壁纸的主要颜色信息。 |

**示例:**

```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)}`);
});
```

## wallpaper.getColors<sup>(deprecated)</sup>

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

获取指定类型壁纸的主要颜色信息。

526 527 528 529
> **说明:**
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getColorsSync<sup>9+</sup>](#wallpapergetcolorssync9)代替。

530 531 532 533
**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

534 535 536
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
537

538
**返回值:**
539

540
| 类型 | 说明 |
541
| -------- | -------- |
542
| Promise&lt;Array&lt;[RgbaColor](#rgbacolor)&gt;&gt; | 返回壁纸的主要颜色信息。 |
543

544
**示例:**
545

546
```js
547 548 549 550 551
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)}`);
});
552
```
553

554
## wallpaper.getId<sup>(deprecated)</sup>
Z
zengyawen 已提交
555 556 557 558 559

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

获取指定类型壁纸的ID。

560
> **说明:**
561 562
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getIdSync<sup>9+</sup>](#wallpapergetidsync9)代替。
563

564
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
565

566 567
**参数:**

568 569 570
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
571
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的ID。如果配置了指定类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。 |
Z
zengyawen 已提交
572

573
**示例:**
574

575 576 577
```js
wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
    if (error) {
578
        console.error(`failed to getId because: ${JSON.stringify(error)}`);
579 580
        return;
    }
581
    console.log(`success to getId: ${JSON.stringify(data)}`);
582 583
});
```
Z
zengyawen 已提交
584

585
## wallpaper.getId<sup>(deprecated)</sup>
Z
zengyawen 已提交
586 587 588 589 590

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

获取指定类型壁纸的ID。

591
> **说明:**
592 593
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getIdSync<sup>9+</sup>](#wallpapergetidsync9)代替。
Z
zengyawen 已提交
594

595
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
596

597 598
**参数:**

599 600 601
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
Z
zengyawen 已提交
602

603 604
**返回值:**

605 606
| 类型 | 说明 |
| -------- | -------- |
607
| Promise&lt;number&gt; | 壁纸的ID。如果配置了这种壁纸类型的壁纸就返回一个大于等于0的数,否则返回-1。取值范围是-1到(2^31-1)。 |
Z
zengyawen 已提交
608

609
**示例:**
610

611 612
```js
wallpaper.getId(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
613
    console.log(`success to getId: ${JSON.stringify(data)}`);
Z
zengyawen 已提交
614
  }).catch((error) => {
615
    console.error(`failed to getId because: ${JSON.stringify(error)}`);
616 617
});
```
Z
zengyawen 已提交
618

619
## wallpaper.getMinHeight<sup>(deprecated)</sup>
Z
zengyawen 已提交
620 621 622 623 624

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

获取壁纸的最小高度值。

625
> **说明:**
626 627
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getMinHeightSync<sup>9+</sup>](#wallpapergetminheightsync9)代替。
628

629
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
630

631 632
**参数:**

633 634 635
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
Z
zengyawen 已提交
636

637
**示例:**
638

639 640 641
```js
wallpaper.getMinHeight((error, data) => {
    if (error) {
642
        console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
643 644
        return;
    }
645
    console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
646 647
});
```
Z
zengyawen 已提交
648

649
## wallpaper.getMinHeight<sup>(deprecated)</sup>
Z
zengyawen 已提交
650 651 652 653 654

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

获取壁纸的最小高度值。

655
> **说明:**
656 657
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getMinHeightSync<sup>9+</sup>](#wallpapergetminheightsync9)代替。
Z
zengyawen 已提交
658

659
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
660

661 662
**返回值:**

663 664 665
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;number&gt; | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
Z
zengyawen 已提交
666

667
**示例:**
668

669 670
```js
wallpaper.getMinHeight().then((data) => {
671
    console.log(`success to getMinHeight: ${JSON.stringify(data)}`);
672
}).catch((error) => {
673
    console.error(`failed to getMinHeight because: ${JSON.stringify(error)}`);
674 675
});
```
Z
zengyawen 已提交
676

677
## wallpaper.getMinWidth<sup>(deprecated)</sup>
Z
zengyawen 已提交
678 679 680 681 682

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

获取壁纸的最小宽度值。

683
> **说明:**
684 685
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getMinWidthSync<sup>9+</sup>](#wallpapergetminwidthsync9)代替。
Z
zengyawen 已提交
686

687
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
688

689 690
**参数:**

691 692 693
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
Z
zengyawen 已提交
694

695
**示例:**
696

697 698 699
```js
wallpaper.getMinWidth((error, data) => {
    if (error) {
700
        console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
701 702
        return;
    }
703
    console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
704 705
});
```
Z
zengyawen 已提交
706

707
## wallpaper.getMinWidth<sup>(deprecated)</sup>
Z
zengyawen 已提交
708 709 710 711 712

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

获取壁纸的最小宽度值。

713
> **说明:**
714 715
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getMinWidthSync<sup>9+</sup>](#wallpapergetminwidthsync9)代替。
716

717
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
718

719 720
**返回值:**

721 722 723
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;number&gt; | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
Z
zengyawen 已提交
724

725
**示例:**
726

727 728
```js
wallpaper.getMinWidth().then((data) => {
729
    console.log(`success to getMinWidth: ${JSON.stringify(data)}`);
Z
zengyawen 已提交
730
  }).catch((error) => {
731
    console.error(`failed to getMinWidth because: ${JSON.stringify(error)}`);
732 733
});
```
Z
zengyawen 已提交
734

735
## wallpaper.isChangePermitted<sup>(deprecated)</sup>
Z
zengyawen 已提交
736 737 738 739 740

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

是否允许应用改变当前用户的壁纸。

741
> **说明:**
742 743
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.isChangeAllowed<sup>9+</sup>](#wallpaperischangeallowed9)代替。
744

745
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
746

747 748
**参数:**

749 750 751
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
Z
zengyawen 已提交
752

753
**示例:**
754

755 756 757
```js
wallpaper.isChangePermitted((error, data) => {
    if (error) {
758
        console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
759 760
        return;
    }
761
    console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
762 763
});
```
Z
zengyawen 已提交
764

765
## wallpaper.isChangePermitted<sup>(deprecated)</sup>
Z
zengyawen 已提交
766 767 768 769 770

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

是否允许应用改变当前用户的壁纸。

771
> **说明:**
772 773
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.isChangeAllowed<sup>9+</sup>](#wallpaperischangeallowed9)代替。
774

775
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
776

777 778
**返回值:**

779 780 781
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;boolean&gt; | 返回是否允许应用改变当前用户的壁纸。如果允许返回true,否则返回false。 |
Z
zengyawen 已提交
782

783
**示例:**
784

785 786
```js
wallpaper.isChangePermitted().then((data) => {
787
    console.log(`success to isChangePermitted: ${JSON.stringify(data)}`);
788
}).catch((error) => {
789
    console.error(`failed to isChangePermitted because: ${JSON.stringify(error)}`);
790 791
});
```
Z
zengyawen 已提交
792

793
## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
Z
zengyawen 已提交
794 795 796 797 798

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

是否允许用户设置壁纸。

799
> **说明:**
800 801
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.isUserChangeAllowed<sup>9+</sup>](#wallpaperisuserchangeallowed9)代替。
802

803
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
804

805 806
**参数:**

807 808 809
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callback | AsyncCallback&lt;boolean&gt; | 是 | 回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
Z
zengyawen 已提交
810

811
**示例:**
812

813 814 815
```js
wallpaper.isOperationAllowed((error, data) => {
    if (error) {
816
        console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
817 818
        return;
    }
819
    console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
820 821
});
```
Z
zengyawen 已提交
822

823
## wallpaper.isOperationAllowed<sup>(deprecated)</sup>
Z
zengyawen 已提交
824 825 826 827 828

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

是否允许用户设置壁纸。

829
> **说明:**
830 831
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.isUserChangeAllowed<sup>9+</sup>](#wallpaperisuserchangeallowed9)代替。
832

833
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
834

835 836
**返回值:**

837 838 839
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;boolean&gt; | 异步回调函数,返回是否允许用户设置壁纸。如果允许返回true,否则返回false。 |
Z
zengyawen 已提交
840

841
**示例:**
842 843 844 845

```js
wallpaper.isOperationAllowed().then((data) => {
    console.log(`success to isOperationAllowed: ${JSON.stringify(data)}`);
846
  }).catch((error) => {
847
    console.error(`failed to isOperationAllowed because: ${JSON.stringify(error)}`);
848 849
});
```
850

851
## wallpaper.reset<sup>(deprecated)</sup>
Z
zengyawen 已提交
852

853
reset(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
854

855
移除指定类型的壁纸,恢复为默认显示的壁纸。
Z
zengyawen 已提交
856

857
> **说明:**
858
> 
859
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.restore<sup>9+</sup>](#wallpaperrestore9)代替。
860

861
**需要权限**:ohos.permission.SET_WALLPAPER
Z
zengyawen 已提交
862

863
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
864

865 866
**参数:**

867 868 869
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
870
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,移除壁纸成功,error为undefined 否则返回error信息。 |
Z
zengyawen 已提交
871

872
**示例:**
873

874 875 876 877 878
```js
wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
    if (error) {
        console.error(`failed to reset because: ${JSON.stringify(error)}`);
        return;
879
    }
880
    console.log(`success to reset.`);
881 882
});
```
Z
zengyawen 已提交
883

884
## wallpaper.reset<sup>(deprecated)</sup>
Z
zengyawen 已提交
885

886
reset(wallpaperType: WallpaperType): Promise&lt;void&gt;
Z
zengyawen 已提交
887

888
移除指定类型的壁纸,恢复为默认显示的壁纸。
Z
zengyawen 已提交
889

890
> **说明:**
891 892
>
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.restore<sup>9+</sup>](#wallpaperrestore9)代替。
893

Z
zengyawen 已提交
894 895
**需要权限**:ohos.permission.SET_WALLPAPER

896
**系统能力**: SystemCapability.MiscServices.Wallpaper
Z
zengyawen 已提交
897

898 899
**参数:**

900 901 902
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
Z
zengyawen 已提交
903

904 905
**返回值:**

906 907
| 类型 | 说明 |
| -------- | -------- |
908
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
Z
zengyawen 已提交
909

910
**示例:**
911

912
```js
913 914 915 916
wallpaper.reset(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to reset.`);
}).catch((error) => {
    console.error(`failed to reset because: ${JSON.stringify(error)}`);
917 918
});
```
919

920
## wallpaper.setWallpaper<sup>(deprecated)</sup>
921

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

将指定资源设置为指定类型的壁纸。

926 927 928 929
> **说明:**
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.setImage<sup>9+</sup>](#wallpapersetimage9)代替。

930 931 932 933 934 935
**需要权限**:ohos.permission.SET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

936 937 938 939
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
940
| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined 否则返回error信息。 |
941 942

**示例:**
943

944 945 946
```js
// source类型为string
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
947 948 949 950 951 952
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.`);
953
});
954

955 956 957 958 959 960 961 962 963
// source类型为image.PixelMap
import image from '@ohos.multimedia.image';
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts = {
    "desiredSize": {
        "height": 3648,
        "width": 2736
    }
};
964 965 966 967
imageSource.createPixelMap(opts).then((pixelMap) => {
    wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error) => {
        if (error) {
            console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
968
            return;
969 970
        }
        console.log(`success to setWallpaper.`);
971
    });
972 973
}).catch((error) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
974 975
});
```
976

977
## wallpaper.setWallpaper<sup>(deprecated)</sup>
978

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

将指定资源设置为指定类型的壁纸。

983 984 985 986
> **说明:**
> 
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.setImage<sup>9+</sup>](#wallpapersetimage9)代替。

987 988 989 990 991 992
**需要权限**:ohos.permission.SET_WALLPAPER

**系统能力**: SystemCapability.MiscServices.Wallpaper

**参数:**

993 994 995 996
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| source | string \| [image.PixelMap](js-apis-image.md#pixelmap7) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
997 998 999

**返回值:**

1000 1001
| 类型 | 说明 |
| -------- | -------- |
1002
| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
1003 1004

**示例:**
1005

1006 1007 1008
```js
// source类型为string
let wallpaperPath = "/data/data/ohos.acts.aafwk.plrdtest.form/files/Cup_ic.jpg";
1009 1010 1011 1012
wallpaper.setWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
    console.log(`success to setWallpaper.`);
  }).catch((error) => {
    console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
1013
});
1014
  
1015 1016 1017 1018 1019 1020 1021 1022 1023
// source类型为image.PixelMap
import image from '@ohos.multimedia.image';
let imageSource = image.createImageSource("file://" + wallpaperPath);
let opts = {
    "desiredSize": {
        "height": 3648,
        "width": 2736
    }
};
1024 1025 1026
imageSource.createPixelMap(opts).then((pixelMap) => {
    wallpaper.setWallpaper(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
        console.log(`success to setWallpaper.`);
1027
    }).catch((error) => {
1028
        console.error(`failed to setWallpaper because: ${JSON.stringify(error)}`);
1029
    });
1030 1031
  }).catch((error) => {
    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
1032 1033
});
```
1034

1035 1036

## wallpaper.getFile<sup>(deprecated)</sup>
1037 1038 1039 1040 1041

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

获取指定类型的壁纸文件。

1042
> **说明:**
1043 1044
> 
> 从 API version 8开始支持,从API version 9开始废弃。建议使用[wallpaper.getFileSync<sup>9+</sup>](#wallpapergetfilesync9)代替。
1045

1046
**需要权限**:ohos.permission.GET_WALLPAPER
1047

1048
**系统能力**: SystemCapability.MiscServices.Wallpaper
1049

1050 1051
**参数:**

1052 1053 1054 1055
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
| callback | AsyncCallback&lt;number&gt; | 是 | 回调函数,调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
1056

1057
**示例:**
1058

1059 1060 1061
```js
wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error, data) => {
    if (error) {
1062
        console.error(`failed to getFile because: ${JSON.stringify(error)}`);
1063 1064
        return;
    }
1065
    console.log(`success to getFile: ${JSON.stringify(data)}`);
1066 1067
});
```
1068

1069
## wallpaper.getFile<sup>(deprecated)</sup>
1070 1071 1072 1073 1074

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

获取指定类型的壁纸文件。

1075 1076
> **说明:**
>
1077
> 从 API version 8开始支持,从API version 9开始废弃。建议使用[wallpaper.getFileSync<sup>9+</sup>](#wallpapergetfilesync9)代替。
1078

1079
**需要权限**:ohos.permission.GET_WALLPAPER
1080

1081
**系统能力**: SystemCapability.MiscServices.Wallpaper
1082

1083 1084
**参数:**

1085 1086 1087
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
1088

1089 1090
**返回值:**

1091 1092 1093
| 类型 | 说明 |
| -------- | -------- |
| Promise&lt;number&gt; | 调用成功则返回壁纸文件描述符ID,调用失败则返回error信息。 |
1094

1095
**示例:**
1096

1097 1098
```js
wallpaper.getFile(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data) => {
1099
    console.log(`success to getFile: ${JSON.stringify(data)}`);
1100
  }).catch((error) => {
1101
    console.error(`failed to getFile because: ${JSON.stringify(error)}`);
1102 1103
});
```
Z
zengyawen 已提交
1104

1105
## wallpaper.getPixelMap<sup>(deprecated)</sup>
1106 1107 1108 1109 1110

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

获取壁纸图片的像素图。

1111 1112
> **说明:**
>
1113
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getImage<sup>9+</sup>](#wallpapergetimage9)代替。
1114

1115
**需要权限**:ohos.permission.GET_WALLPAPER
1116 1117 1118

**系统能力**: SystemCapability.MiscServices.Wallpaper

1119
**系统接口**:此接口为系统接口。
1120

1121 1122
**参数:**

1123 1124 1125
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
1126
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
1127 1128

**示例:**
1129

1130
```js
1131 1132 1133 1134 1135 1136
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)}`);
1137
  });
1138
```
1139

1140
## wallpaper.getPixelMap<sup>(deprecated)</sup>
1141 1142 1143 1144 1145

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

获取壁纸图片的像素图。

1146 1147
> **说明:**
>
1148
> 从 API version 7开始支持,从API version 9开始废弃。建议使用[wallpaper.getImage<sup>9+</sup>](#wallpapergetimage9)代替。
1149

1150
**需要权限**:ohos.permission.GET_WALLPAPER
1151 1152 1153

**系统能力**: SystemCapability.MiscServices.Wallpaper

1154
**系统接口**:此接口为系统接口。
1155

1156 1157
**参数:**

1158 1159 1160
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| wallpaperType | [WallpaperType](#wallpapertype) | 是 | 壁纸类型。 |
1161 1162 1163

**返回值:**

1164 1165
| 类型 | 说明 |
| -------- | -------- |
1166
| Promise&lt;image.PixelMap&gt; | 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
1167 1168

**示例:**
1169

1170
```js
1171 1172 1173 1174
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)}`);
1175 1176
});
```