js-apis-userFileManager.md 127.0 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.filemanagement.userFileManager (用户数据管理)
Y
yangbo 已提交
2

Z
zhang-daiyue 已提交
3 4
该模块提供用户数据管理能力,包括访问、修改用户等用户公共媒体数据信息等常用功能。

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

## 导入模块
W
wangqinxiao 已提交
11

Z
zhang-daiyue 已提交
12 13
```ts
import userFileManager from '@ohos.filemanagement.userFileManager';
Y
yangbo 已提交
14 15 16 17 18 19 20 21
```

## userFileManager.getUserFileMgr

getUserFileMgr(context: Context): UserFileManager

获取用户数据管理模块的实例,用于访问和修改用户等用户公共媒体数据信息(如音频、视频、图片、文档等)。

H
huweiqi 已提交
22
**模型约束**: 此接口仅可在Stage模型下使用。
zyjhandsome's avatar
zyjhandsome 已提交
23

Y
yangbo 已提交
24 25
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zengyawen 已提交
26
**参数:**
Y
yangbo 已提交
27 28 29

| 参数名  | 类型    | 必填 | 说明                       |
| ------- | ------- | ---- | -------------------------- |
H
huweiqi 已提交
30
| context | [Context](js-apis-inner-app-context.md) | 是   | 传入Ability实例的Context。 |
Y
yangbo 已提交
31 32 33 34 35

**返回值:**

| 类型                            | 说明    |
| ----------------------------- | :---- |
H
huweiqi 已提交
36
| [UserFileManager](#userfilemanager) | 媒体库实例。 |
Y
yangbo 已提交
37 38 39 40

**示例:**

```ts
41
//此处获取的userFileManager实例mgr为全局对象,后续使用到mgr的地方默认为使用此处获取的对象,如未添加此段代码报mgr未定义的错误请自行添加
Y
yangbo 已提交
42
const context = getContext(this);
H
huweiqi 已提交
43
let mgr = userFileManager.getUserFileMgr(context);
Y
yangbo 已提交
44
```
W
wangqinxiao 已提交
45

Y
yangbo 已提交
46 47
## UserFileManager

Z
zhang-daiyue 已提交
48 49 50 51 52
### getPhotoAssets

getPhotoAssets(options: FetchOptions, callback: AsyncCallback<FetchResult<FileAsset>>): void;

获取图片和视频资源,使用callback方式返回结果。
Y
yangbo 已提交
53 54 55

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
56 57
**需要权限**:ohos.permission.READ_IMAGEVIDEO

Y
yangbo 已提交
58 59 60 61
**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
62 63
| options  | [FetchOptions](#fetchoptions)        | 是   | 图片和视频检索选项。              |
| callback |  AsyncCallback<[FetchResult](#fetchresult)<[FileAsset](#fileasset)>> | 是   | callback返回图片和视频检索结果集。 |
Y
yangbo 已提交
64 65 66 67

**示例:**

```ts
H
huweiqi 已提交
68 69
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
async function example() {
  console.info('getPhotoAssets');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };

  mgr.getPhotoAssets(fetchOptions, async (err, fetchResult) => {
    if (fetchResult != undefined) {
      console.info('fetchResult success');
      let fileAsset = await fetchResult.getFirstObject();
      if (fileAsset != undefined) {
H
huweiqi 已提交
83
        console.info('fileAsset.displayName : ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
84 85
      }
    } else {
H
huweiqi 已提交
86
      console.error('fetchResult fail' + err);
Z
zhang-daiyue 已提交
87
    }
H
huweiqi 已提交
88
  });
Y
yangbo 已提交
89 90 91
}
```

Z
zhang-daiyue 已提交
92 93 94
### getPhotoAssets

getPhotoAssets(options: FetchOptions): Promise<FetchResult<FileAsset>>;
Y
yangbo 已提交
95

Z
zhang-daiyue 已提交
96
获取图片和视频资源,使用Promise方式返回结果。
Y
yangbo 已提交
97 98 99

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
100 101
**需要权限**:ohos.permission.READ_IMAGEVIDEO

Y
yangbo 已提交
102 103
**参数:**

Z
zhang-daiyue 已提交
104 105
| 参数名  | 类型                | 必填 | 说明             |
| ------- | ------------------- | ---- | ---------------- |
H
huweiqi 已提交
106
| options | [FetchOptions](#fetchoptions)   | 是   | 图片和视频检索选项。     |
Y
yangbo 已提交
107

Z
zengyawen 已提交
108
**返回值:**
Y
yangbo 已提交
109

Z
zhang-daiyue 已提交
110 111
| 类型                        | 说明           |
| --------------------------- | -------------- |
H
huweiqi 已提交
112
| Promise<[FetchResult](#fetchresult)<[FileAsset](#fileasset)>> | Promise对象,返回图片和视频数据结果集。 |
Y
yangbo 已提交
113 114 115 116

**示例:**

```ts
H
huweiqi 已提交
117 118
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
119 120 121 122 123 124 125 126
async function example() {
  console.info('getPhotoAssets');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
  try {
127
    let fetchResult = await mgr.getPhotoAssets(fetchOptions);
Z
zhang-daiyue 已提交
128 129 130 131
    if (fetchResult != undefined) {
      console.info('fetchResult success');
      let fileAsset = await fetchResult.getFirstObject();
      if (fileAsset != undefined) {
H
huweiqi 已提交
132
        console.info('fileAsset.displayName :' + fileAsset.displayName);
Z
zhang-daiyue 已提交
133
      }
Y
yangbo 已提交
134
    }
Z
zhang-daiyue 已提交
135
  } catch (err) {
H
huweiqi 已提交
136
    console.error('getPhotoAssets failed, message = ', err);
Z
zhang-daiyue 已提交
137
  }
Y
yangbo 已提交
138 139
}
```
Z
zhang-daiyue 已提交
140
### createPhotoAsset
Y
yangbo 已提交
141

Z
zhang-daiyue 已提交
142
createPhotoAsset(displayName: string, albumUri: string, callback: AsyncCallback<FileAsset>): void;
Y
yangbo 已提交
143

H
huweiqi 已提交
144
指定待创建的图片或者视频的文件名和所在相册的uri,创建图片或视频资源,使用callback方式返回结果。
Y
yangbo 已提交
145 146 147

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
148
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
149 150 151 152 153

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
154 155 156
| displayName  | string        | 是   | 创建的图片或者视频文件名。              |
| albumUri  | string        | 是   | 创建的图片或者视频所在相册的uri。              |
| callback |  AsyncCallback<[FileAsset](#fileasset)> | 是   | callback返回创建的图片和视频结果。 |
Y
yangbo 已提交
157 158 159 160

**示例:**

```ts
H
huweiqi 已提交
161 162
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
163
async function example() {
H
huweiqi 已提交
164
  console.info('createPhotoAssetDemo');
Z
zhang-daiyue 已提交
165 166 167 168
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    predicates: predicates
  };
H
huweiqi 已提交
169 170
  let albums = await mgr.getPhotoAlbums(fetchOptions);
  let album = await albums.getFirstObject();
H
huweiqi 已提交
171
  let testFileName = 'testFile' + Date.now() + '.jpg';
H
huweiqi 已提交
172
  mgr.createPhotoAsset(testFileName, album.albumUri, (err, fileAsset) => {
Z
zhang-daiyue 已提交
173 174 175 176
    if (fileAsset != undefined) {
      console.info('createPhotoAsset file displayName' + fileAsset.displayName);
      console.info('createPhotoAsset successfully');
    } else {
H
huweiqi 已提交
177
      console.error('createPhotoAsset failed, message = ', err);
Z
zhang-daiyue 已提交
178
    }
H
huweiqi 已提交
179
  });
Z
zhang-daiyue 已提交
180 181
}
```
Y
yangbo 已提交
182

Z
zhang-daiyue 已提交
183 184 185 186
### createPhotoAsset

createPhotoAsset(displayName: string, callback: AsyncCallback<FileAsset>): void;

H
huweiqi 已提交
187
指定待创建的图片或者视频的文件名,创建图片或视频资源,使用callback方式返回结果。
Z
zhang-daiyue 已提交
188 189 190 191 192 193 194 195 196

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
197 198
| displayName  | string        | 是   | 创建的图片或者视频文件名。              |
| callback |  AsyncCallback<[FileAsset](#fileasset)> | 是   | callback返回创建的图片和视频结果。 |
Z
zhang-daiyue 已提交
199 200 201 202 203

**示例:**

```ts
async function example() {
H
huweiqi 已提交
204
  console.info('createPhotoAssetDemo');
H
huweiqi 已提交
205
  let testFileName = 'testFile' + Date.now() + '.jpg';
H
huweiqi 已提交
206
  mgr.createPhotoAsset(testFileName, (err, fileAsset) => {
Z
zhang-daiyue 已提交
207 208 209 210
    if (fileAsset != undefined) {
      console.info('createPhotoAsset file displayName' + fileAsset.displayName);
      console.info('createPhotoAsset successfully');
    } else {
H
huweiqi 已提交
211
      console.error('createPhotoAsset failed, message = ', err);
Z
zhang-daiyue 已提交
212
    }
H
huweiqi 已提交
213
  });
Y
yangbo 已提交
214 215 216
}
```

Z
zhang-daiyue 已提交
217
### createPhotoAsset
Y
yangbo 已提交
218

H
huweiqi 已提交
219
createPhotoAsset(displayName: string, albumUri?: string): Promise<FileAsset>;
Y
yangbo 已提交
220

H
huweiqi 已提交
221
指定待创建的图片或者视频的文件名和所在相册的uri,创建图片或视频资源,使用Promise方式返回结果。
Y
yangbo 已提交
222 223 224

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
225
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
226 227 228

**参数:**

Z
zhang-daiyue 已提交
229 230
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
231 232
| displayName  | string        | 是   | 创建的图片或者视频文件名。              |
| albumUri  | string        | 否   | 创建的图片或者视频所在相册的uri。              |
Y
yangbo 已提交
233

Z
zengyawen 已提交
234
**返回值:**
Y
yangbo 已提交
235 236 237

| 类型                        | 说明           |
| --------------------------- | -------------- |
H
huweiqi 已提交
238
| Promise<[FileAsset](#fileasset)> | Promise对象,返回创建的图片和视频结果。 |
Y
yangbo 已提交
239 240 241 242

**示例:**

```ts
Z
zhang-daiyue 已提交
243
async function example() {
H
huweiqi 已提交
244
  console.info('createPhotoAssetDemo');
Z
zhang-daiyue 已提交
245
  try {
H
huweiqi 已提交
246
    let testFileName = 'testFile' + Date.now() + '.jpg';
H
huweiqi 已提交
247
    let fileAsset = await mgr.createPhotoAsset(testFileName);
Z
zhang-daiyue 已提交
248 249 250
    console.info('createPhotoAsset file displayName' + fileAsset.displayName);
    console.info('createPhotoAsset successfully');
  } catch (err) {
H
huweiqi 已提交
251
    console.error('createPhotoAsset failed, message = ', err);
Z
zhang-daiyue 已提交
252
  }
Y
yangbo 已提交
253 254 255
}
```

H
huweiqi 已提交
256 257 258 259 260 261 262 263 264 265 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 314 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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
### createPhotoAsset

createPhotoAsset(displayName: string, createOption: PhotoCreateOptions, callback: AsyncCallback<FileAsset>): void;

指定待创建的图片或者视频的文件名和创建选项,创建图片或视频资源,使用callback方式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName  | string        | 是   | 创建的图片或者视频文件名。              |
| createOption  | [PhotoCreateOptions](#photocreateoptions10)        | 是   | 图片或视频的创建选项。              |
| callback |  AsyncCallback<[FileAsset](#fileasset)> | 是   | callback返回创建的图片和视频结果。 |

**示例:**

```ts
async function example() {
  console.info('createPhotoAssetDemo');
  let testFileName = 'testFile' + Date.now() + '.jpg';
  let createOption = {
    subType: userFileManager.PhotoSubType.DEFAULT
  }
  mgr.createPhotoAsset(testFileName, createOption, (err, fileAsset) => {
    if (fileAsset != undefined) {
      console.info('createPhotoAsset file displayName' + fileAsset.displayName);
      console.info('createPhotoAsset successfully');
    } else {
      console.error('createPhotoAsset failed, message = ', err);
    }
  });
}
```

### createPhotoAsset

createPhotoAsset(displayName: string, createOption: PhotoCreateOptions): Promise<FileAsset>;

指定待创建的图片或者视频的文件名和创建选项,创建图片或视频资源,使用Promise方式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName  | string        | 是   | 创建的图片或者视频文件名。              |
| createOption  |  [PhotoCreateOptions](#photocreateoptions10)       | 是   | 图片或视频的创建选项。              |

**返回值:**

| 类型                        | 说明           |
| --------------------------- | -------------- |
| Promise<[FileAsset](#fileasset)> | Promise对象,返回创建的图片和视频结果。 |

**示例:**

```ts
async function example() {
  console.info('createPhotoAssetDemo');
  try {
    let testFileName = 'testFile' + Date.now() + '.jpg';
    let createOption = {
      subType: userFileManager.PhotoSubType.DEFAULT
    }
    let fileAsset = await mgr.createPhotoAsset(testFileName, createOption);
    console.info('createPhotoAsset file displayName' + fileAsset.displayName);
    console.info('createPhotoAsset successfully');
  } catch (err) {
    console.error('createPhotoAsset failed, message = ', err);
  }
}
```

### createAudioAsset<sup>10+</sup>

createAudioAsset(displayName: string, callback: AsyncCallback&lt;FileAsset&gt;): void;

创建音频文件资源,使用callback方式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName  | string        | 是   | 创建的音频文件名。              |
| callback |  AsyncCallback&lt;[FileAsset](#fileasset)&gt; | 是   | callback返回创建的音频资源结果。 |

**示例:**

```ts
async function example() {
  console.info('createAudioAssetDemo');
  let testFileName = 'testFile' + Date.now() + '.mp3';
  mgr.createAudioAsset(testFileName, (err, fileAsset) => {
    if (fileAsset != undefined) {
      console.info('createAudioAsset file displayName' + fileAsset.displayName);
      console.info('createAudioAsset successfully');
    } else {
      console.error('createAudioAsset failed, message = ', err);
    }
  });
}
```

### createAudioAsset<sup>10+</sup>

createAudioAsset(displayName: string): Promise&lt;FileAsset&gt;;

创建音频文件资源,使用Promise方式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| displayName  | string        | 是   | 创建的音频文件名。              |

**返回值:**

| 类型                        | 说明           |
| --------------------------- | -------------- |
| Promise&lt;[FileAsset](#fileasset)&gt; | Promise对象,返回创建的音频资源结果。 |

**示例:**

```ts
async function example() {
  console.info('createAudioAssetDemo');
  try {
    let testFileName = 'testFile' + Date.now() + '.mp3';
    let fileAsset = await mgr.createAudioAsset(testFileName);
    console.info('createAudioAsset file displayName' + fileAsset.displayName);
    console.info('createAudioAsset successfully');
  } catch (err) {
    console.error('createAudioAsset failed, message = ', err);
  }
}
```

Z
zhang-daiyue 已提交
408
### getPhotoAlbums
Y
yangbo 已提交
409

Z
zhang-daiyue 已提交
410
getPhotoAlbums(options: AlbumFetchOptions, callback: AsyncCallback&lt;FetchResult&lt;Album&gt;&gt;): void;
Y
yangbo 已提交
411

H
huweiqi 已提交
412

Z
zhang-daiyue 已提交
413
获取相册,使用callback方式返回结果。
Y
yangbo 已提交
414 415 416

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
417 418
**需要权限**:ohos.permission.READ_IMAGEVIDEO

Y
yangbo 已提交
419 420
**参数:**

Z
zhang-daiyue 已提交
421 422
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
423 424
| options  | [AlbumFetchOptions](#albumfetchoptions)        | 是   | 相册检索选项。              |
| callback |  AsyncCallback&lt;[FetchResult](#fetchresult)&lt;[Album](#album)&gt;&gt; | 是   | callback返回相册检索结果。 |
Y
yangbo 已提交
425 426 427 428

**示例:**

```ts
H
huweiqi 已提交
429 430
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
431
async function example() {
H
huweiqi 已提交
432
  console.info('getPhotoAlbumsDemo');
Z
zhang-daiyue 已提交
433 434 435 436 437 438 439 440 441 442 443 444
  let predicates = new dataSharePredicates.DataSharePredicates();
  let albumFetchOptions = {
    predicates: predicates
  };

  mgr.getPhotoAlbums(albumFetchOptions, (err, fetchResult) => {
    if (fetchResult != undefined) {
      console.info('albums.count = ' + fetchResult.getCount());
      fetchResult.getFirstObject((err, album) => {
        if (album != undefined) {
          console.info('first album.albumName = ' + album.albumName);
        } else {
H
huweiqi 已提交
445
          console.error('album is undefined, err = ', err);
Z
zhang-daiyue 已提交
446
        }
H
huweiqi 已提交
447
      });
H
huweiqi 已提交
448
    } else {
H
huweiqi 已提交
449
      console.error('getPhotoAlbums fail, message = ', err);
Z
zhang-daiyue 已提交
450
    }
H
huweiqi 已提交
451
  });
Y
yangbo 已提交
452 453 454
}
```

Z
zhang-daiyue 已提交
455
### getPhotoAlbums
Y
yangbo 已提交
456

Z
zhang-daiyue 已提交
457
getPhotoAlbums(options: AlbumFetchOptions): Promise&lt;FetchResult&lt;Album&gt;&gt;;
Y
yangbo 已提交
458

459
获取相册,使用Promise方式返回结果。
Y
yangbo 已提交
460 461 462

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
463 464
**需要权限**:ohos.permission.READ_IMAGEVIDEO

Y
yangbo 已提交
465 466
**参数:**

Z
zhang-daiyue 已提交
467 468
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
469
| options  | [AlbumFetchOptions](#albumfetchoptions)        | 是   | 相册检索选项。              |
Z
zhang-daiyue 已提交
470

Z
zengyawen 已提交
471
**返回值:**
Z
zhang-daiyue 已提交
472 473 474

| 类型                        | 说明           |
| --------------------------- | -------------- |
H
huweiqi 已提交
475
| Promise&lt;[FetchResult](#fetchresult)&lt;[Album](#album)&gt;&gt; | Promise对象,返回相册检索结果。 |
Y
yangbo 已提交
476 477 478 479

**示例:**

```ts
H
huweiqi 已提交
480 481
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
482
async function example() {
H
huweiqi 已提交
483
  console.info('getPhotoAlbumsDemo');
Z
zhang-daiyue 已提交
484 485 486 487 488 489 490 491 492 493
  let predicates = new dataSharePredicates.DataSharePredicates();
  let albumFetchOptions = {
    predicates: predicates
  };
  try {
    let fetchResult = await mgr.getPhotoAlbums(albumFetchOptions);
    console.info('album.count = ' + fetchResult.getCount());
    const album = await fetchResult.getFirstObject();
    console.info('first album.albumName = ' + album.albumName);
  } catch (err) {
H
huweiqi 已提交
494
    console.error('getPhotoAlbums fail, message = ' + err);
Z
zhang-daiyue 已提交
495
  }
Y
yangbo 已提交
496 497 498
}
```

H
huweiqi 已提交
499
### createAlbum<sup>10+</sup>
Y
yangbo 已提交
500

H
huweiqi 已提交
501
createAlbum(name: string, callback: AsyncCallback&lt;Album&gt;): void;
Y
yangbo 已提交
502

H
huweiqi 已提交
503 504 505 506 507 508 509
创建相册,使用callback方式返回结果。

待创建的相册名参数规格为:
- 相册名字符串长度为1~255。
- 不允许出现的非法英文字符,包括:<br> . .. \ / : * ? " ' ` < > | { } [ ]
- 英文字符大小写不敏感。
- 相册名不允许重名。
李云帆 已提交
510

Y
yangbo 已提交
511 512
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
513
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
514 515 516

**参数:**

Z
zhang-daiyue 已提交
517 518
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
519 520
| name  | string         | 是   | 待创建相册的相册名。              |
| callback |  AsyncCallback&lt;[Album](#album)&gt; | 是   | callback返回创建的相册实例。 |
Y
yangbo 已提交
521 522 523 524

**示例:**

```ts
Z
zhang-daiyue 已提交
525
async function example() {
H
huweiqi 已提交
526 527 528 529 530 531
  console.info('createAlbumDemo');
  let albumName = 'newAlbumName' + new Date().getTime();
  mgr.createAlbum(albumName, (err, album) => {
    if (err) {
      console.error('createAlbumCallback failed with err: ' + err);
      return;
Z
zhang-daiyue 已提交
532
    }
H
huweiqi 已提交
533
    console.info('createAlbumCallback successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri);
Z
zhang-daiyue 已提交
534
  });
Y
yangbo 已提交
535 536 537
}
```

H
huweiqi 已提交
538
### createAlbum<sup>10+</sup>
Y
yangbo 已提交
539

H
huweiqi 已提交
540
createAlbum(name: string): Promise&lt;Album&gt;;
Y
yangbo 已提交
541

H
huweiqi 已提交
542
创建相册,使用Promise方式返回结果。
Y
yangbo 已提交
543

H
huweiqi 已提交
544 545 546 547 548
待创建的相册名参数规格为:
- 相册名字符串长度为1~255。
- 不允许出现的非法英文字符,包括:<br> . .. \ / : * ? " ' ` < > | { } [ ]
- 英文字符大小写不敏感。
- 相册名不允许重名。
李云帆 已提交
549

Y
yangbo 已提交
550 551
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
552
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
553 554 555

**参数:**

Z
zhang-daiyue 已提交
556 557
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
558
| name  | string         | 是   | 待创建相册的相册名。              |
Y
yangbo 已提交
559

Z
zengyawen 已提交
560
**返回值:**
Y
yangbo 已提交
561

Z
zhang-daiyue 已提交
562 563
| 类型                        | 说明           |
| --------------------------- | -------------- |
H
huweiqi 已提交
564
| Promise&lt;[Album](#album)&gt; | Promise对象,返回创建的相册实例。 |
Y
yangbo 已提交
565 566 567 568

**示例:**

```ts
Z
zhang-daiyue 已提交
569
async function example() {
H
huweiqi 已提交
570 571 572 573 574 575 576
  console.info('createAlbumDemo');
  let albumName = 'newAlbumName' + new Date().getTime();
  mgr.createAlbum(albumName).then((album) => {
    console.info('createAlbumPromise successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri);
  }).catch((err) => {
    console.error('createAlbumPromise failed with err: ' + err);
  });
Y
yangbo 已提交
577 578 579
}
```

H
huweiqi 已提交
580
### deleteAlbums<sup>10+</sup>
Y
yangbo 已提交
581

H
huweiqi 已提交
582
deleteAlbums(albums: Array&lt;Album&gt;, callback: AsyncCallback&lt;void&gt;): void;
Y
yangbo 已提交
583

H
huweiqi 已提交
584 585 586
删除相册,使用callback方式返回结果。

删除相册前需先确保相册存在,只能删除用户相册。
李云帆 已提交
587

Y
yangbo 已提交
588 589
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
590
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
591 592 593

**参数:**

Z
zhang-daiyue 已提交
594 595
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
596 597
| albums  | Array&lt;[Album](#album)&gt;         | 是   | 待删除相册的数组。              |
| callback |  AsyncCallback&lt;void&gt; | 是   | callback返回void。 |
Y
yangbo 已提交
598 599 600 601

**示例:**

```ts
Z
zhang-daiyue 已提交
602
async function example() {
H
huweiqi 已提交
603 604
  // 示例代码为删除名称包含newAlbumName的第一个相册。
  console.info('deleteAlbumsDemo');
Z
zhang-daiyue 已提交
605
  let predicates = new dataSharePredicates.DataSharePredicates();
H
huweiqi 已提交
606
  predicates.like('album_name', '%newAlbumName%');
Z
zhang-daiyue 已提交
607 608 609 610
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
H
huweiqi 已提交
611 612 613 614 615 616
  let fetchResult = await mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC, fetchOptions);
  let album = await fetchResult.getFirstObject();
  mgr.deleteAlbums([album], (err) => {
    if (err) {
      console.error('deletePhotoAlbumsCallback failed with err: ' + err);
      return;
Y
yangbo 已提交
617
    }
H
huweiqi 已提交
618
    console.info('deletePhotoAlbumsCallback successfully');
H
huweiqi 已提交
619
  });
H
huweiqi 已提交
620
  fetchResult.close();
Y
yangbo 已提交
621 622 623
}
```

H
huweiqi 已提交
624
### deleteAlbums<sup>10+</sup>
Y
yangbo 已提交
625

H
huweiqi 已提交
626
deleteAlbums(albums: Array&lt;Album&gt;): Promise&lt;void&gt;;
Y
yangbo 已提交
627

H
huweiqi 已提交
628 629 630
删除相册,使用Promise方式返回结果。

删除相册前需先确保相册存在,只能删除用户相册。
李云帆 已提交
631

Y
yangbo 已提交
632 633
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
634
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
635 636 637

**参数:**

Z
zhang-daiyue 已提交
638 639
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
H
huweiqi 已提交
640
| albums  |  Array&lt;[Album](#album)&gt;          | 是   | 待删除相册的数组。              |
Y
yangbo 已提交
641

Z
zengyawen 已提交
642
**返回值:**
Y
yangbo 已提交
643

Z
zhang-daiyue 已提交
644 645
| 类型                        | 说明           |
| --------------------------- | -------------- |
H
huweiqi 已提交
646
| Promise&lt;void&gt; | Promise对象,返回void。 |
Y
yangbo 已提交
647 648 649 650

**示例:**

```ts
Z
zhang-daiyue 已提交
651
async function example() {
H
huweiqi 已提交
652 653
  // 示例代码为删除名称包含newAlbumName的第一个相册。
  console.info('deleteAlbumsDemo');
Z
zhang-daiyue 已提交
654
  let predicates = new dataSharePredicates.DataSharePredicates();
H
huweiqi 已提交
655
  predicates.like('album_name', '%newAlbumName%');
Z
zhang-daiyue 已提交
656 657 658 659
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
H
huweiqi 已提交
660 661 662 663 664 665 666 667
  let fetchResult = await mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC, fetchOptions);
  let album = await fetchResult.getFirstObject();
  mgr.deleteAlbums([album]).then(() => {
    console.info('deletePhotoAlbumsPromise successfully');
    }).catch((err) => {
      console.error('deletePhotoAlbumsPromise failed with err: ' + err);
  });
  fetchResult.close();
Y
yangbo 已提交
668 669
}
```
Z
zengyawen 已提交
670

H
huweiqi 已提交
671
### getAlbums<sup>10+</sup>
Y
yangbo 已提交
672

H
huweiqi 已提交
673
getAlbums(type: AlbumType, subType: AlbumSubType, options: FetchOptions, callback: AsyncCallback&lt;FetchResult&lt;Album&gt;&gt;): void;
Y
yangbo 已提交
674

H
huweiqi 已提交
675
根据检索选项和相册类型获取相册,使用callback方式返回结果。
Y
yangbo 已提交
676

H
huweiqi 已提交
677
获取相册前需先保证相册存在。
Y
yangbo 已提交
678

李云帆 已提交
679
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
680

H
huweiqi 已提交
681
**需要权限**:ohos.permission.READ_IMAGEVIDEO
Y
yangbo 已提交
682

H
huweiqi 已提交
683
**参数:**
Y
yangbo 已提交
684

H
huweiqi 已提交
685 686 687 688 689 690
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| type  | [AlbumType](#albumtype10)         | 是   | 相册类型。              |
| subType  | [AlbumSubType](#albumsubtype10)         | 是   | 相册子类型。              |
| options  | [FetchOptions](#fetchoptions)         | 是   |  检索选项。              |
| callback |  AsyncCallback&lt;[FetchResult](#fetchresult)&lt;[Album](#album)&gt;&gt; | 是   | callback返回获取相册的结果集。 |
Y
yangbo 已提交
691

H
huweiqi 已提交
692
**示例:**
H
huweiqi 已提交
693

Y
yangbo 已提交
694
```ts
Z
zhang-daiyue 已提交
695
async function example() {
H
huweiqi 已提交
696 697
  // 示例代码中为获取相册名中包含newAlbumName的第一个相册。
  console.info('getAlbumsDemo');
Z
zhang-daiyue 已提交
698
  let predicates = new dataSharePredicates.DataSharePredicates();
H
huweiqi 已提交
699
  predicates.like('album_name', '%newAlbumName%');
Z
zhang-daiyue 已提交
700 701 702 703
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
H
huweiqi 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
  mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC, fetchOptions, async (err, fetchResult) => {
    if (err) {
      console.error('getAlbumsCallback failed with err: ' + err);
      return;
    }
    if (fetchResult == undefined) {
      console.error('getAlbumsCallback fetchResult is undefined');
      return;
    }
    let album = await fetchResult.getFirstObject();
    console.info('getAlbumsCallback successfully, albumName: ' + album.albumName);
    fetchResult.close();
  });
}
```
Z
zhang-daiyue 已提交
719

H
huweiqi 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
### getAlbums<sup>10+</sup>

getAlbums(type: AlbumType, subType: AlbumSubType, callback: AsyncCallback&lt;FetchResult&lt;Album&gt;&gt;): void;

根据相册类型获取相册,使用callback方式返回结果。

获取相册前需先保证相册存在。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| type  | [AlbumType](#albumtype10)         | 是   | 相册类型。              |
| subType  | [AlbumSubType](#albumsubtype10)         | 是   | 相册子类型。              |
| callback |  AsyncCallback&lt;[FetchResult](#fetchresult)&lt;[Album](#album)&gt;&gt; | 是   | callback返回获取相册的结果集。 |

**示例:**

```ts
async function example() {
  // 示例代码中为获取统相册VIDEO,默认已预置。
  console.info('getAlbumsDemo');
  mgr.getAlbums(userFileManager.AlbumType.SYSTEM, userFileManager.AlbumSubType.VIDEO, async (err, fetchResult) => {
    if (err) {
      console.error('getAlbumsCallback failed with err: ' + err);
      return;
Z
zhang-daiyue 已提交
750
    }
H
huweiqi 已提交
751 752 753 754 755 756 757
    if (fetchResult == undefined) {
      console.error('getAlbumsCallback fetchResult is undefined');
      return;
    }
    let album = await fetchResult.getFirstObject();
    console.info('getAlbumsCallback successfully, albumUri: ' + album.albumUri);
    fetchResult.close();
Z
zhang-daiyue 已提交
758
  });
Y
yangbo 已提交
759 760
}
```
Z
zengyawen 已提交
761

H
huweiqi 已提交
762
### getAlbums<sup>10+</sup>
Y
yangbo 已提交
763

H
huweiqi 已提交
764
getAlbums(type: AlbumType, subType: AlbumSubType, options?: FetchOptions): Promise&lt;FetchResult&lt;Album&gt;&gt;;
Y
yangbo 已提交
765

H
huweiqi 已提交
766
根据检索选项和相册类型获取相册,使用Promise方式返回结果。
Y
yangbo 已提交
767

H
huweiqi 已提交
768
获取相册前需先保证相册存在。
Y
yangbo 已提交
769

李云帆 已提交
770 771
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
772
**需要权限**:ohos.permission.READ_IMAGEVIDEO
Y
yangbo 已提交
773

H
huweiqi 已提交
774
**参数:**
Y
yangbo 已提交
775

H
huweiqi 已提交
776 777 778 779 780
| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| type  | [AlbumType](#albumtype10)         | 是   | 相册类型。              |
| subType  | [AlbumSubType](#albumsubtype10)         | 是   | 相册子类型。              |
| options  | [FetchOptions](#fetchoptions)         | 否   |  检索选项,不填时默认根据相册类型检索。              |
Y
yangbo 已提交
781

Z
zengyawen 已提交
782
**返回值:**
Y
yangbo 已提交
783

H
huweiqi 已提交
784 785 786
| 类型                        | 说明           |
| --------------------------- | -------------- |
| Promise&lt;[FetchResult](#fetchresult)&lt;[Album](#album)&gt;&gt; | Promise对象,返回获取相册的结果集。 |
Y
yangbo 已提交
787

H
huweiqi 已提交
788
**示例:**
Y
yangbo 已提交
789 790

```ts
Z
zhang-daiyue 已提交
791
async function example() {
H
huweiqi 已提交
792 793
  // 示例代码中为获取相册名中包含newAlbumName的第一个相册。
  console.info('getAlbumsDemo');
Z
zhang-daiyue 已提交
794
  let predicates = new dataSharePredicates.DataSharePredicates();
H
huweiqi 已提交
795
  predicates.like('album_name', '%newAlbumName%');
Z
zhang-daiyue 已提交
796 797 798 799
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
H
huweiqi 已提交
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 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 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
  mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC, fetchOptions).then( async (fetchResult) => {
    if (fetchResult == undefined) {
      console.error('getAlbumsPromise fetchResult is undefined');
      return;
    }
    let album = await fetchResult.getFirstObject();
    console.info('getAlbumsPromise successfully, albumName: ' + album.albumName);
    fetchResult.close();
  }).catch((err) => {
    console.error('getAlbumsPromise failed with err: ' + err);
  });
}
```

### getPrivateAlbum

getPrivateAlbum(type: PrivateAlbumType, callback: AsyncCallback&lt;FetchResult&lt;PrivateAlbum&gt;&gt;): void;

获取系统相册,使用 callback 方式返回系统相册的数组。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| type  | [PrivateAlbumType](#privatealbumtype)        | 是   | 系统相册类型。              |
| callback |  AsyncCallback&lt;[FetchResult](#fetchresult)&lt;[PrivateAlbum](#privatealbum)&gt;&gt; | 是   | callback返回相册检索结果。 |

**示例:**

```ts
async function example() {
  console.info('getPrivateAlbumDemo');
  mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH, async (err, fetchResult) => {
    if (fetchResult != undefined) {
      let trashAlbum = await fetchResult.getFirstObject();
      console.info('first album.albumName = ' + trashAlbum.albumName);
    } else {
      console.error('getPrivateAlbum failed. message = ', err);
    }
  });
}
```

### getPrivateAlbum

getPrivateAlbum(type: PrivateAlbumType): Promise&lt;FetchResult&lt;PrivateAlbum&gt;&gt;;

获取系统相册,使用Promise方式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| type  | [PrivateAlbumType](#privatealbumtype)        | 是   | 系统相册类型。              |

**返回值:**

| 类型                        | 说明           |
| --------------------------- | -------------- |
| Promise&lt;[FetchResult](#fetchresult)&lt;[PrivateAlbum](#privatealbum)&gt;&gt; | Promise对象,返回相册检索结果。 |

**示例:**

```ts
async function example() {
  console.info('getPrivateAlbumDemo');
  try {
    let fetchResult = await mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH);
    let trashAlbum = await fetchResult.getFirstObject();
    console.info('first album.albumName = ' + trashAlbum.albumName);
  } catch (err) {
    console.error('getPrivateAlbum failed. message = ', err);
  }
}
```

### getAudioAssets

getAudioAssets(options: FetchOptions, callback: AsyncCallback&lt;FetchResult&lt;FileAsset&gt;&gt;): void;

获取音频文件,使用callback方式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| options  | [FetchOptions](#fetchoptions)        | 是   | 检索选项。              |
| callback |  AsyncCallback&lt;[FetchResult](#fetchresult)&lt;[FileAsset](#fileasset)&gt;&gt; | 是   | callback返回音频检索结果。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('getAudioAssets');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };

  mgr.getAudioAssets(fetchOptions, async (err, fetchResult) => {
    if (fetchResult != undefined) {
      console.info('fetchFileResult success');
      let fileAsset = await fetchResult.getFirstObject();
      if (fileAsset != undefined) {
        console.info('fileAsset.displayName :' + fileAsset.displayName);
      }
    } else {
      console.error('fetchFileResult fail' + err);
    }
  });
}
```

### getAudioAssets

getAudioAssets(options: FetchOptions): Promise&lt;FetchResult&lt;FileAsset&gt;&gt;;


获取音频文件,使用callback方式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

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

**参数:**

| 参数名   | 类型                     | 必填 | 说明                      |
| -------- | ------------------------ | ---- | ------------------------- |
| options  | [FetchOptions](#fetchoptions)        | 是   | 检索选项。              |

**返回值:**

| 类型                        | 说明           |
| --------------------------- | -------------- |
| Promise&lt;[FetchResult](#fetchresult)&lt;[FileAsset](#fileasset)&gt;&gt; | Promise对象,返回音频检索结果。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('getAudioAssets');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
  try {
    var fetchResult = await mgr.getAudioAssets(fetchOptions);
  } catch (err) {
    console.error('getAudioAssets failed, message = ', err);
  }

  if (fetchResult != undefined) {
    console.info('fetchFileResult success');
    let fileAsset = await fetchResult.getFirstObject();
    if (fileAsset != undefined) {
      console.info('fileAsset.displayName :' + fileAsset.displayName);
    }
  }
}
```

### delete

delete(uri: string, callback: AsyncCallback&lt;void&gt;): void;

删除媒体文件,删除的文件进入到回收站。

**需要权限**:ohos.permission.READ_IMAGEVIDEO 和 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 和 ohos.permission.WRITE_AUDIO

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| uri | string | 是   | 媒体文件uri。 |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('deleteAssetDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
  try {
    const fetchResult = await mgr.getPhotoAssets(fetchOptions);
    var asset = await fetchResult.getFirstObject();
  } catch (err) {
    console.info('fetch failed, message =', err);
  }

  if (asset == undefined) {
    console.error('asset not exist');
    return;
  }
  mgr.delete(asset.uri, (err) => {
    if (err == undefined) {
      console.info('delete successfully');
    } else {
      console.error('delete failed with error: ' + err);
    }
  });
}
```

### delete

delete(uri: string): Promise&lt;void&gt;;

删除媒体文件,删除的文件进入到回收站。

**需要权限**:ohos.permission.READ_IMAGEVIDEO 和 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 和 ohos.permission.WRITE_AUDIO

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| uri | string | 是   | 媒体文件uri。 |

**返回值:**

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;void&gt;| Promise对象,返回void。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('deleteDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
  try {
    const fetchResult = await mgr.getPhotoAssets(fetchOptions);
Z
zhang-daiyue 已提交
1065 1066
    var asset = await fetchResult.getFirstObject();
  } catch (err) {
H
huweiqi 已提交
1067
    console.info('fetch failed, message =', err);
Z
zhang-daiyue 已提交
1068 1069 1070
  }

  if (asset == undefined) {
H
huweiqi 已提交
1071
    console.error('asset not exist');
Z
zhang-daiyue 已提交
1072 1073 1074 1075
    return;
  }
  try {
    await mgr.delete(asset.uri);
H
huweiqi 已提交
1076
    console.info('delete successfully');
Z
zhang-daiyue 已提交
1077
  } catch (err) {
H
huweiqi 已提交
1078
    console.error('delete failed with error: ' + err);
Z
zhang-daiyue 已提交
1079
  }
Y
yangbo 已提交
1080 1081 1082
}
```

Z
zhang-daiyue 已提交
1083
### on
Y
yangbo 已提交
1084

Z
zhang-daiyue 已提交
1085
on(type: ChangeEvent, callback: Callback&lt;void&gt;): void
Y
yangbo 已提交
1086

Z
zhang-daiyue 已提交
1087
打开文件管理库变更通知,使用callback方式返回异步结果。
李云帆 已提交
1088

1089 1090
此接口即将废弃,请使用[on<sup>10+</sup>](#on10)的新接口

李云帆 已提交
1091
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
1092 1093 1094

**参数:**

Z
zhang-daiyue 已提交
1095 1096 1097
| 参数名   | 类型                 | 必填 | 说明                                                         |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
| type     | [ChangeEvent](#changeevent)               | 是   | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更 |
H
huweiqi 已提交
1098
| callback | Callback&lt;void&gt; | 是   | callback返回void                                                   |
Y
yangbo 已提交
1099 1100 1101 1102

**示例:**

```ts
Z
zhang-daiyue 已提交
1103
async function example() {
H
huweiqi 已提交
1104
  console.info('onDemo');
H
huweiqi 已提交
1105 1106 1107 1108 1109 1110
  let count = 0;
  mgr.on('imageChange', () => {
    count++;
    // image file had changed, do something
  });
  try {
H
huweiqi 已提交
1111
    let testFileName = 'testFile' + Date.now() + '.jpg';
H
huweiqi 已提交
1112 1113 1114 1115
    let fileAsset = await mgr.createPhotoAsset(testFileName);
    console.info('createPhotoAsset file displayName' + fileAsset.displayName);
    console.info('createPhotoAsset successfully');
  } catch (err) {
H
huweiqi 已提交
1116
    console.error('createPhotoAsset failed, message = ' + err);
H
huweiqi 已提交
1117 1118 1119
  }
  //sleep 1s
  if (count > 0) {
H
huweiqi 已提交
1120
    console.info('onDemo success');
H
huweiqi 已提交
1121
  } else {
H
huweiqi 已提交
1122
    console.error('onDemo fail');
H
huweiqi 已提交
1123 1124 1125 1126
  }
  mgr.off('imageChange', () => {
    // stop listening success
  });
Y
yangbo 已提交
1127 1128 1129
}
```

Z
zhang-daiyue 已提交
1130
### off
Y
yangbo 已提交
1131

Z
zhang-daiyue 已提交
1132
off(type: ChangeEvent, callback?: Callback&lt;void&gt;): void
Y
yangbo 已提交
1133

Z
zhang-daiyue 已提交
1134
关闭文件管理库变更通知,使用callback方式返回异步结果。
李云帆 已提交
1135

1136 1137
此接口即将废弃,请使用[off<sup>10+</sup>](#off10)的新接口

李云帆 已提交
1138
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
1139 1140 1141

**参数:**

Z
zhang-daiyue 已提交
1142 1143
| 参数名   | 类型                 | 必填 | 说明                                                         |
| -------- | -------------------- | ---- | ------------------------------------------------------------ |
H
huweiqi 已提交
1144 1145
| type     | [ChangeEvent](#changeevent)               | 是   | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更。 |
| callback | Callback&lt;void&gt; | 否   | callback返回void。                                                   |
Y
yangbo 已提交
1146 1147 1148 1149

**示例:**

```ts
Z
zhang-daiyue 已提交
1150
async function example() {
H
huweiqi 已提交
1151
  console.info('offDemo');
H
huweiqi 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
  let count = 0;
  mgr.on('imageChange', () => {
    count++;
    // image file had changed, do something
  });

  mgr.off('imageChange', () => {
    // stop listening success
  });

  try {
H
huweiqi 已提交
1163
    let testFileName = 'testFile' + Date.now() + '.jpg';
H
huweiqi 已提交
1164 1165 1166 1167
    let fileAsset = await mgr.createPhotoAsset(testFileName);
    console.info('createPhotoAsset file displayName' + fileAsset.displayName);
    console.info('createPhotoAsset successfully');
  } catch (err) {
H
huweiqi 已提交
1168
    console.error('createPhotoAsset failed, message = ' + err);
H
huweiqi 已提交
1169 1170 1171
  }
  //sleep 1s
  if (count == 0) {
H
huweiqi 已提交
1172
    console.info('offDemo success');
H
huweiqi 已提交
1173
  } else {
H
huweiqi 已提交
1174
    console.error('offDemo fail');
H
huweiqi 已提交
1175
  }
Y
yangbo 已提交
1176 1177 1178 1179 1180
}
```

### getActivePeers

Z
zhang-daiyue 已提交
1181
getActivePeers(callback: AsyncCallback&lt;Array&lt;PeerInfo&gt;&gt;): void;
Y
yangbo 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190

获取在线对端设备的信息,使用callback方式返回异步结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.DistributedCore

**参数:**

| 参数名   | 类型                              | 必填 | 说明         |
| -------- | --------------------------------- | ---- | ------------ |
H
huweiqi 已提交
1191
| callback | AsyncCallback&lt;Array&lt;[PeerInfo](#peerinfo)&gt;&gt; | 是   | 返回在线设备列表。 |
Y
yangbo 已提交
1192 1193 1194 1195

**示例:**

```ts
Z
zhang-daiyue 已提交
1196
async function example() {
H
huweiqi 已提交
1197
  console.info('getActivePeersDemo');
Z
zhang-daiyue 已提交
1198 1199
  mgr.getActivePeers((err, devicesInfo) => {
    if (devicesInfo != undefined) {
H
huweiqi 已提交
1200
      console.log('getActivePeers succeed.');
Z
zhang-daiyue 已提交
1201 1202 1203 1204
      for (let i = 0; i < devicesInfo.length; i++) {
        console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
      }
    } else {
H
huweiqi 已提交
1205
      console.error('getActivePeers failed. message = ', err);
Z
zhang-daiyue 已提交
1206 1207
    }
  });
Y
yangbo 已提交
1208 1209 1210 1211 1212
}
```

### getActivePeers

Z
zhang-daiyue 已提交
1213
getActivePeers(): Promise&lt;Array&lt;PeerInfo&gt;&gt;;
Y
yangbo 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222

获取在线对端设备的信息,使用promise方式返回异步结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.DistributedCore

**返回值:**

| 类型                        | 说明                          |
| --------------------------- | ----------------------------- |
H
huweiqi 已提交
1223
| Promise&lt;Array&lt;[PeerInfo](#peerinfo)&gt;&gt; | Promise对象,返回在线设备列表。 |
Y
yangbo 已提交
1224 1225 1226 1227

**示例:**

```ts
Z
zhang-daiyue 已提交
1228
async function example() {
H
huweiqi 已提交
1229
  console.info('getActivePeersDemo');
Z
zhang-daiyue 已提交
1230 1231 1232
  try {
    var devicesInfo = await mgr.getActivePeers();
  } catch (err) {
H
huweiqi 已提交
1233
    console.error('getActivePeers failed. message = ', err);
Z
zhang-daiyue 已提交
1234 1235
  }
  if (devicesInfo != undefined) {
H
huweiqi 已提交
1236
    console.log('getActivePeers succeed.');
Z
zhang-daiyue 已提交
1237 1238
    for (let i = 0; i < devicesInfo.length; i++) {
      console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
Y
yangbo 已提交
1239
    }
Z
zhang-daiyue 已提交
1240
  } else {
H
huweiqi 已提交
1241
    console.error('get distributed fail');
Z
zhang-daiyue 已提交
1242
  }
Y
yangbo 已提交
1243 1244 1245 1246 1247
}
```

### getAllPeers

Z
zhang-daiyue 已提交
1248
getAllPeers(callback: AsyncCallback&lt;Array&lt;PeerInfo&gt;&gt;): void;
Y
yangbo 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257

获取所有对端设备的信息,使用callback方式返回异步结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.DistributedCore

**参数:**

| 参数名   | 类型                              | 必填 | 说明         |
| -------- | --------------------------------- | ---- | ------------ |
H
huweiqi 已提交
1258
| callback | AsyncCallback&lt;Array&lt;[PeerInfo](#peerinfo)&gt;&gt; | 是   | 返回在线设备列表。 |
Y
yangbo 已提交
1259 1260 1261 1262

**示例:**

```ts
Z
zhang-daiyue 已提交
1263
async function example() {
H
huweiqi 已提交
1264
  console.info('getAllPeersDemo');
Z
zhang-daiyue 已提交
1265 1266
  mgr.getAllPeers((err, devicesInfo) => {
    if (devicesInfo != undefined) {
H
huweiqi 已提交
1267
      console.log('getAllPeers succeed.');
Z
zhang-daiyue 已提交
1268 1269 1270 1271
      for (let i = 0; i < devicesInfo.length; i++) {
        console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
      }
    } else {
H
huweiqi 已提交
1272
      console.error('getAllPeers failed. message = ', err);
Z
zhang-daiyue 已提交
1273 1274
    }
  });
Y
yangbo 已提交
1275 1276 1277 1278 1279
}
```

### getAllPeers

Z
zhang-daiyue 已提交
1280
getAllPeers(): Promise&lt;Array&lt;PeerInfo&gt;&gt;;
Y
yangbo 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289

获取所有对端设备的信息,使用promise方式返回异步结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.DistributedCore

**返回值:**

| 类型                        | 说明                          |
| --------------------------- | ----------------------------- |
H
huweiqi 已提交
1290
| Promise&lt;Array&lt;[PeerInfo](#peerinfo)&gt;&gt; | Promise对象,返回所有设备列表。 |
Y
yangbo 已提交
1291 1292 1293 1294

**示例:**

```ts
Z
zhang-daiyue 已提交
1295
async function example() {
H
huweiqi 已提交
1296
  console.info('getAllPeersDemo');
Z
zhang-daiyue 已提交
1297 1298 1299
  try {
    var devicesInfo = await mgr.getAllPeers();
  } catch (err) {
H
huweiqi 已提交
1300
    console.error('getAllPeers failed. message = ', err);
Z
zhang-daiyue 已提交
1301 1302
  }
  if (devicesInfo != undefined) {
H
huweiqi 已提交
1303
    console.log('getAllPeers succeed.');
Z
zhang-daiyue 已提交
1304 1305
    for (let i = 0; i < devicesInfo.length; i++) {
      console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
Y
yangbo 已提交
1306
    }
Z
zhang-daiyue 已提交
1307
  } else {
H
huweiqi 已提交
1308
    console.error('get distributed fail');
Z
zhang-daiyue 已提交
1309
  }
Y
yangbo 已提交
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
}
```

### release

release(callback: AsyncCallback&lt;void&gt;): void

释放UserFileManager实例。
当后续不需要使用UserFileManager实例中的方法时调用。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明                 |
| -------- | ------------------------- | ---- | -------------------- |
H
huweiqi 已提交
1326
| callback | AsyncCallback&lt;void&gt; | 是   | 回调表示成功还是失败。 |
Y
yangbo 已提交
1327 1328 1329 1330

**示例:**

```ts
Z
zhang-daiyue 已提交
1331 1332 1333 1334
async function example() {
  console.info('releaseDemo');
  mgr.release((err) => {
    if (err != undefined) {
H
huweiqi 已提交
1335
      console.error('release failed. message = ', err);
Z
zhang-daiyue 已提交
1336 1337 1338
    } else {
      console.info('release ok.');
    }
H
huweiqi 已提交
1339
  });
Y
yangbo 已提交
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
}
```

### release

release(): Promise&lt;void&gt;

释放UserFileManager实例。
当后续不需要使用UserFileManager 实例中的方法时调用。

李云帆 已提交
1350
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
1351 1352 1353 1354 1355

**返回值:**

| 类型                | 说明                              |
| ------------------- | --------------------------------- |
H
huweiqi 已提交
1356
| Promise&lt;void&gt; | Promise对象,返回void。 |
Y
yangbo 已提交
1357 1358 1359 1360

**示例:**

```ts
Z
zhang-daiyue 已提交
1361 1362 1363 1364 1365 1366
async function example() {
  console.info('releaseDemo');
  try {
    await mgr.release();
    console.info('release ok.');
  } catch (err) {
H
huweiqi 已提交
1367
    console.error('release failed. message = ', err);
Z
zhang-daiyue 已提交
1368
  }
Y
yangbo 已提交
1369 1370 1371
}
```

C
caochuan 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
### on<sup>10+</sup>

on(uri: string, forSubUri: boolean, callback: Callback&lt;ChangeData&gt;) : void

打开对指定uri的监听,使用callback方式返回异步结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名    | 类型                                        | 必填 | 说明                                                         |
| --------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
1384
| uri       | string                                      | 是   | FileAsset的uri, Album的uri或[DefaultChangeUri](#defaultchangeuri10)的值。 |
1385 1386
| forSubUri | boolean                                     | 是   | 是否模糊监听,uri为相册uri时,forSubUri 为true能监听到相册中文件的变化,如果是false只能监听相册本身变化。uri为fileAsset时,forSubUri 为true、false没有区别,uri为DefaultChangeUri时,forSubUri必须为true,如果为false将找不到该uri,收不到任何消息。 |
| callback  | Callback&lt;[ChangeData](#changedata10)&gt; | 是   | 返回要监听的[ChangeData](#changedata10)。注:uri可以注册多个不同的callback监听,[off<sup>10+</sup>](#off10)可以关闭该uri所有监听,也可以关闭指定callback的监听。 |
C
caochuan 已提交
1387 1388 1389 1390 1391

**示例:**

```ts
async function example() {
1392
  console.info('onDemo');
1393 1394 1395 1396 1397 1398 1399 1400 1401
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOptions);
  let fileAsset = await fetchResult.getFirstObject();
  if (fileAsset != undefined) {
    console.info('fileAsset.displayName : ' + fileAsset.displayName);
1402
  }
1403 1404 1405 1406 1407 1408
  let onCallback1 = (changeData) => {
      console.info('onCallback1 success, changData: ' + JSON.stringify(changeData));
    //file had changed, do something
  }
  let onCallback2 = (changeData) => {
      console.info('onCallback2 success, changData: ' + JSON.stringify(changeData));
1409 1410
    //file had changed, do something
  }
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
  // 注册onCallback1监听
  mgr.on(fileAsset.uri, false, onCallback1); 
  // 注册onCallback2监听
  mgr.on(fileAsset.uri, false, onCallback2);

  fileAsset.favorite(true, (err) => {
    if (err == undefined) {
      console.info('favorite successfully');
    } else {
      console.error('favorite failed with error:' + err);
    }
  });
C
caochuan 已提交
1423 1424 1425 1426 1427 1428 1429
}
```

### off<sup>10+</sup>

 off(uri: string, callback?: Callback&lt;ChangeData&gt;): void

1430
关闭指定uri的监听,一个uri可以注册多个监听,存在多个callback监听时,可以取消指定注册的callback的监听;不指定callback时解除该uri的所有监听。
C
caochuan 已提交
1431 1432 1433 1434 1435 1436 1437

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                                        | 必填 | 说明                                                         |
| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ |
1438
| uri      | string                                      | 是   | FileAsset的uri, Album的uri或[DefaultChangeUri](#defaultchangeuri10)的值。 |
1439
| callback | Callback&lt;[ChangeData](#changedata10)&gt; | 否   | 解除[on<sup>10+</sup>](#on10)注册时的callback的监听,不填时,解除该uri的所有监听。注:off指定注册的callback后不会进入此回调。 |
C
caochuan 已提交
1440 1441 1442 1443 1444

**示例:**

```ts
async function example() {
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
  console.info('offDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOptions = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOptions);
  let fileAsset = await fetchResult.getFirstObject();
  if (fileAsset != undefined) {
    console.info('fileAsset.displayName : ' + fileAsset.displayName);
1455
  }
1456 1457
  let onCallback1 = (changeData) => {
    console.info('onCallback1 on');
1458
  }
1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
  let onCallback2 = (changeData) => {
    console.info('onCallback2 on');
  }
  // 注册onCallback1监听
  mgr.on(fileAsset.uri, false, onCallback1);
  // 注册onCallback2监听
  mgr.on(fileAsset.uri, false, onCallback2);
  // 关闭onCallback1监听,onCallback2 继续监听
  mgr.off(fileAsset.uri, onCallback1);
  fileAsset.favorite(true, (err) => {
    if (err == undefined) {
      console.info('favorite successfully');
    } else {
      console.error('favorite failed with error:' + err);
    }
  });
C
caochuan 已提交
1475 1476 1477
}
```

Y
yangbo 已提交
1478 1479 1480 1481 1482 1483
## FileAsset

提供封装文件属性的方法。

### 属性

H
huweiqi 已提交
1484
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
1485 1486 1487

| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
H
huweiqi 已提交
1488
| uri                       | string                   | 是   | 否   | 文件资源uri(如:file://media/image/2)。         |
Z
zhang-daiyue 已提交
1489
| fileType   | [FileType](#filetype) | 是   | 否   | 媒体文件类型                                               |
H
huweiqi 已提交
1490
| displayName               | string                   | 是   | 是   | 显示文件名,包含后缀名。                                 |
Y
yangbo 已提交
1491

Z
zhang-daiyue 已提交
1492
### get
Y
yangbo 已提交
1493

Z
zhang-daiyue 已提交
1494
get(member: string): MemberType;
Y
yangbo 已提交
1495

H
huweiqi 已提交
1496
获取FileAsset成员参数。
Y
yangbo 已提交
1497 1498 1499 1500 1501

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

Z
zhang-daiyue 已提交
1502 1503
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1504
| member | string | 是    | 成员参数名称例如:ImageVideoKey.URI。 |
Y
yangbo 已提交
1505 1506

**示例:**
H
huweiqi 已提交
1507

Z
zhang-daiyue 已提交
1508
```ts
H
huweiqi 已提交
1509 1510
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1511
async function example() {
H
huweiqi 已提交
1512
  console.info('fileAssetGetDemo');
Z
zhang-daiyue 已提交
1513 1514 1515
  try {
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
H
huweiqi 已提交
1516
      fetchColumns: ['title'],
Z
zhang-daiyue 已提交
1517
      predicates: predicates
Y
yangbo 已提交
1518
    };
Z
zhang-daiyue 已提交
1519 1520
    let fetchResult = await mgr.getPhotoAssets(fetchOption);
    let fileAsset = await fetchResult.getFirstObject();
H
huweiqi 已提交
1521 1522
    let title = userFileManager.ImageVideoKey.TITLE;
    let fileAssetTitle = fileAsset.get(title.toString());
Z
zhang-daiyue 已提交
1523 1524
    console.info('fileAsset Get fileAssetTitle = ', fileAssetTitle);
  } catch (err) {
H
huweiqi 已提交
1525
    console.error('release failed. message = ', err);
Z
zhang-daiyue 已提交
1526
  }
Y
yangbo 已提交
1527 1528 1529
}
```

Z
zhang-daiyue 已提交
1530
### set
Y
yangbo 已提交
1531

Z
zhang-daiyue 已提交
1532
set(member: string, value: string): void;
Y
yangbo 已提交
1533

H
huweiqi 已提交
1534
设置FileAsset成员参数。
Y
yangbo 已提交
1535 1536 1537

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
1538
**参数:**
Y
yangbo 已提交
1539

Z
zhang-daiyue 已提交
1540 1541
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1542 1543
| member | string | 是    | 成员参数名称例如:ImageVideoKey.URI。 |
| value | string | 是    | 设置成员参数名称,只能修改ImageVideoKey.DISPLAY_NAME的值。 |
Y
yangbo 已提交
1544 1545

**示例:**
H
huweiqi 已提交
1546

Z
zhang-daiyue 已提交
1547
```ts
H
huweiqi 已提交
1548 1549
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1550
async function example() {
H
huweiqi 已提交
1551
  console.info('fileAssetSetDemo');
Z
zhang-daiyue 已提交
1552 1553 1554 1555 1556
  try {
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
Y
yangbo 已提交
1557
    };
Z
zhang-daiyue 已提交
1558 1559
    let fetchResult = await mgr.getPhotoAssets(fetchOption);
    let fileAsset = await fetchResult.getFirstObject();
H
huweiqi 已提交
1560 1561
    let displayName = userFileManager.ImageVideoKey.DISPLAY_NAME;
    fileAsset.set(displayName, 'newDisplayName1');
Z
zhang-daiyue 已提交
1562
  } catch (err) {
H
huweiqi 已提交
1563
    console.error('release failed. message = ', err);
Z
zhang-daiyue 已提交
1564
  }
Y
yangbo 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573
}
```

### commitModify

commitModify(callback: AsyncCallback&lt;void&gt;): void

修改文件的元数据,使用callback方式返回异步结果。

Z
zhang-daiyue 已提交
1574
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
1575 1576 1577 1578 1579 1580 1581

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1582
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回void。 |
Y
yangbo 已提交
1583 1584 1585

**示例:**

Z
zhang-daiyue 已提交
1586
```ts
H
huweiqi 已提交
1587 1588
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1589
async function example() {
H
huweiqi 已提交
1590
  console.info('commitModifyDemo');
Z
zhang-daiyue 已提交
1591 1592 1593 1594 1595 1596 1597
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  let fileAsset = await fetchResult.getFirstObject();
H
huweiqi 已提交
1598 1599 1600 1601
  let displayName = userFileManager.ImageVideoKey.DISPLAY_NAME;
  let fileAssetDisplayName = fileAsset.get(displayName);
  console.info('fileAsset get fileAssetDisplayName = ', fileAssetDisplayName);
  fileAsset.set(displayName, 'newDisplayName2');
Z
zhang-daiyue 已提交
1602 1603
  fileAsset.commitModify((err) => {
    if (err == undefined) {
H
huweiqi 已提交
1604 1605
      let newFileAssetDisplayName = fileAsset.get(displayName);
      console.info('fileAsset get newFileAssetDisplayName = ', newFileAssetDisplayName);
Z
zhang-daiyue 已提交
1606
    } else {
H
huweiqi 已提交
1607
      console.error('commitModify failed, message =', err);
Z
zhang-daiyue 已提交
1608 1609
    }
  });
Y
yangbo 已提交
1610 1611 1612 1613 1614 1615 1616 1617 1618
}
```

### commitModify

commitModify(): Promise&lt;void&gt;

修改文件的元数据,使用promise方式返回异步结果。

Z
zhang-daiyue 已提交
1619
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
1620 1621 1622 1623 1624 1625 1626

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**返回值:**

| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1627
| Promise&lt;void&gt; | Promise对象,返回void。 |
Y
yangbo 已提交
1628

Z
zhang-daiyue 已提交
1629
**示例:**  
Y
yangbo 已提交
1630

Z
zhang-daiyue 已提交
1631
```ts
H
huweiqi 已提交
1632 1633
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1634
async function example() {
H
huweiqi 已提交
1635
  console.info('commitModifyDemo');
Z
zhang-daiyue 已提交
1636 1637 1638 1639 1640 1641 1642
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  let fileAsset = await fetchResult.getFirstObject();
H
huweiqi 已提交
1643 1644 1645 1646
  let displayName = userFileManager.ImageVideoKey.DISPLAY_NAME;
  let fileAssetDisplayName = fileAsset.get(displayName);
  console.info('fileAsset get fileAssetDisplayName = ', fileAssetDisplayName);
  fileAsset.set(displayName, 'newDisplayName3');
Z
zhang-daiyue 已提交
1647
  try {
H
huweiqi 已提交
1648
    await fileAsset.commitModify();
H
huweiqi 已提交
1649 1650
    let newFileAssetDisplayName = fileAsset.get(displayName);
    console.info('fileAsset get newFileAssetDisplayName = ', newFileAssetDisplayName);
Z
zhang-daiyue 已提交
1651
  } catch (err) {
H
huweiqi 已提交
1652
    console.error('release failed. message = ', err);
Z
zhang-daiyue 已提交
1653
  }
Y
yangbo 已提交
1654 1655 1656 1657 1658 1659 1660 1661 1662
}
```

### open

open(mode: string, callback: AsyncCallback&lt;number&gt;): void

打开当前文件,使用callback方式返回异步结果。

H
huweiqi 已提交
1663
**注意**:当前写操作是互斥的操作,写操作完成后需要调用close进行释放。
Y
yangbo 已提交
1664

H
huweiqi 已提交
1665
**需要权限**:ohos.permission.READ_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 或 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
1666 1667 1668

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
1669
**参数:**
Y
yangbo 已提交
1670 1671 1672

| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
H
huweiqi 已提交
1673 1674
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
| callback | AsyncCallback&lt;number&gt; | 是    | callback返回文件描述符。                            |
Y
yangbo 已提交
1675 1676 1677

**示例:**

Z
zhang-daiyue 已提交
1678
```ts
Y
yangbo 已提交
1679
async function example() {
H
huweiqi 已提交
1680
  console.info('openDemo');
H
huweiqi 已提交
1681
   let testFileName = 'testFile' + Date.now() + '.jpg';
H
huweiqi 已提交
1682
  const fileAsset = await mgr.createPhotoAsset(testFileName);
Z
zhang-daiyue 已提交
1683 1684 1685
  fileAsset.open('rw', (err, fd) => {
    if (fd != undefined) {
      console.info('File fd' + fd);
H
huweiqi 已提交
1686
      fileAsset.close(fd);
Z
zhang-daiyue 已提交
1687
    } else {
H
huweiqi 已提交
1688
      console.error('File err' + err);
Z
zhang-daiyue 已提交
1689 1690
    }
  });
Y
yangbo 已提交
1691 1692 1693 1694 1695 1696 1697 1698 1699
}
```

### open

open(mode: string): Promise&lt;number&gt;

打开当前文件,使用promise方式返回异步结果。

H
huweiqi 已提交
1700
**注意**:当前写操作是互斥的操作,写操作完成后需要调用close进行释放。
Y
yangbo 已提交
1701

H
huweiqi 已提交
1702
**需要权限**:ohos.permission.READ_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 或 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
1703 1704 1705 1706 1707 1708 1709

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
H
huweiqi 已提交
1710
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
Y
yangbo 已提交
1711 1712 1713 1714 1715

**返回值:**

| 类型                    | 说明            |
| --------------------- | ------------- |
H
huweiqi 已提交
1716
| Promise&lt;number&gt; | Promise对象,返回文件描述符。 |
Y
yangbo 已提交
1717 1718 1719

**示例:**

Z
zhang-daiyue 已提交
1720
```ts
Y
yangbo 已提交
1721
async function example() {
H
huweiqi 已提交
1722
  console.info('openDemo');
Z
zhang-daiyue 已提交
1723
  try {
H
huweiqi 已提交
1724
    let testFileName = 'testFile' + Date.now() + '.jpg';
H
huweiqi 已提交
1725
    const fileAsset = await mgr.createPhotoAsset(testFileName);
H
huweiqi 已提交
1726
    let fd = await fileAsset.open('rw');
Z
zhang-daiyue 已提交
1727 1728
    if (fd != undefined) {
      console.info('File fd' + fd);
H
huweiqi 已提交
1729
      fileAsset.close(fd);
Z
zhang-daiyue 已提交
1730
    } else {
H
huweiqi 已提交
1731
      console.error(' open File fail');
Z
zhang-daiyue 已提交
1732 1733
    }
  } catch (err) {
H
huweiqi 已提交
1734
    console.error('open Demo err' + err);
Z
zhang-daiyue 已提交
1735
  }
Y
yangbo 已提交
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
}
```

### close

close(fd: number, callback: AsyncCallback&lt;void&gt;): void

关闭当前文件,使用callback方式返回异步结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1751 1752
| fd       | number                    | 是    | 文件描述符。 |
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回void。 |
Y
yangbo 已提交
1753 1754 1755

**示例:**

Z
zhang-daiyue 已提交
1756
```ts
H
huweiqi 已提交
1757 1758
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1759
async function example() {
H
huweiqi 已提交
1760
  console.info('closeDemo');
Z
zhang-daiyue 已提交
1761 1762 1763 1764 1765
  try {
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
Y
yangbo 已提交
1766
    };
Z
zhang-daiyue 已提交
1767 1768 1769 1770 1771 1772 1773 1774
    let fetchResult = await mgr.getPhotoAssets(fetchOption);
    const fileAsset = await fetchResult.getFirstObject();
    let fd = await fileAsset.open('rw');
    console.info('file fd', fd);
    fileAsset.close(fd, (err) => {
      if (err == undefined) {
        console.info('asset close succeed.');
      } else {
H
huweiqi 已提交
1775
        console.error('close failed, message = ' + err);
Z
zhang-daiyue 已提交
1776
      }
Y
yangbo 已提交
1777
    });
Z
zhang-daiyue 已提交
1778
  } catch (err) {
H
huweiqi 已提交
1779
    console.error('close failed, message = ' + err);
Z
zhang-daiyue 已提交
1780
  }
Y
yangbo 已提交
1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
}
```

### close

close(fd: number): Promise&lt;void&gt;

关闭当前文件,使用promise方式返回异步结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
H
huweiqi 已提交
1796
| fd   | number | 是    | 文件描述符。 |
Y
yangbo 已提交
1797 1798 1799 1800 1801

**返回值:**

| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1802
| Promise&lt;void&gt; | Promise对象,返回void。 |
Y
yangbo 已提交
1803 1804 1805

**示例:**

Z
zhang-daiyue 已提交
1806
```ts
H
huweiqi 已提交
1807 1808
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1809
async function example() {
H
huweiqi 已提交
1810
  console.info('closeDemo');
Z
zhang-daiyue 已提交
1811 1812 1813 1814 1815
  try {
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
Y
yangbo 已提交
1816
    };
Z
zhang-daiyue 已提交
1817 1818 1819 1820
    let fetchResult = await mgr.getPhotoAssets(fetchOption);
    const asset = await fetchResult.getFirstObject();
    let fd = await asset.open('rw');
    console.info('file fd', fd);
H
huweiqi 已提交
1821
    await asset.close(fd);
Z
zhang-daiyue 已提交
1822 1823
    console.info('asset close succeed.');
  } catch (err) {
H
huweiqi 已提交
1824
    console.error('close failed, message = ' + err);
Z
zhang-daiyue 已提交
1825
  }
Y
yangbo 已提交
1826 1827 1828 1829 1830 1831 1832 1833 1834
}
```

### getThumbnail

getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void

获取文件的缩略图,使用callback方式返回异步结果。

Z
zhang-daiyue 已提交
1835
**需要权限**:ohos.permission.READ_IMAGEVIDEO 或 ohos.permission.READ_AUDIO
Y
yangbo 已提交
1836 1837 1838 1839 1840 1841 1842

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
H
huweiqi 已提交
1843
| callback | AsyncCallback&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 是    | callback返回缩略图的PixelMap。 |
Y
yangbo 已提交
1844 1845 1846

**示例:**

Z
zhang-daiyue 已提交
1847
```ts
H
huweiqi 已提交
1848 1849
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1850
async function example() {
H
huweiqi 已提交
1851
  console.info('getThumbnailDemo');
Z
zhang-daiyue 已提交
1852 1853 1854 1855 1856 1857 1858
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const asset = await fetchResult.getFirstObject();
H
huweiqi 已提交
1859
  console.info('asset displayName = ', asset.displayName);
Z
zhang-daiyue 已提交
1860 1861 1862 1863
  asset.getThumbnail((err, pixelMap) => {
    if (err == undefined) {
      console.info('getThumbnail successful ' + pixelMap);
    } else {
H
huweiqi 已提交
1864
      console.error('getThumbnail fail', err);
Z
zhang-daiyue 已提交
1865 1866
    }
  });
Y
yangbo 已提交
1867 1868 1869 1870 1871
}
```

### getThumbnail

H
huweiqi 已提交
1872
getThumbnail(size: image.Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
Y
yangbo 已提交
1873 1874 1875

获取文件的缩略图,传入缩略图尺寸,使用callback方式返回异步结果。

Z
zhang-daiyue 已提交
1876
**需要权限**:ohos.permission.READ_IMAGEVIDEO 或 ohos.permission.READ_AUDIO
Y
yangbo 已提交
1877 1878 1879 1880 1881 1882 1883

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
H
huweiqi 已提交
1884 1885
| size     | [image.Size](js-apis-image.md#size) | 是    | 缩略图尺寸。            |
| callback | AsyncCallback&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | 是    | callback返回缩略图的PixelMap。 |
Y
yangbo 已提交
1886 1887 1888

**示例:**

Z
zhang-daiyue 已提交
1889
```ts
H
huweiqi 已提交
1890 1891
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1892
async function example() {
H
huweiqi 已提交
1893
  console.info('getThumbnailDemo');
Z
zhang-daiyue 已提交
1894 1895 1896 1897 1898 1899 1900 1901
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let size = { width: 720, height: 720 };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const asset = await fetchResult.getFirstObject();
H
huweiqi 已提交
1902
  console.info('asset displayName = ', asset.displayName);
Z
zhang-daiyue 已提交
1903 1904 1905 1906
  asset.getThumbnail(size, (err, pixelMap) => {
    if (err == undefined) {
      console.info('getThumbnail successful ' + pixelMap);
    } else {
H
huweiqi 已提交
1907
      console.error('getThumbnail fail', err);
Y
yangbo 已提交
1908
    }
Z
zhang-daiyue 已提交
1909
  });
Y
yangbo 已提交
1910 1911 1912
}
```

Z
zhang-daiyue 已提交
1913
### getThumbnail
Y
yangbo 已提交
1914

H
huweiqi 已提交
1915
getThumbnail(size?: image.Size): Promise&lt;image.PixelMap&gt;
Y
yangbo 已提交
1916

Z
zhang-daiyue 已提交
1917
获取文件的缩略图,传入缩略图尺寸,使用promise方式返回异步结果。
Y
yangbo 已提交
1918

Z
zhang-daiyue 已提交
1919
**需要权限**:ohos.permission.READ_IMAGEVIDEO 或 ohos.permission.READ_AUDIO
Y
yangbo 已提交
1920 1921 1922 1923 1924

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

Z
zhang-daiyue 已提交
1925 1926
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
H
huweiqi 已提交
1927
| size | [image.Size](js-apis-image.md#size) | 否    | 缩略图尺寸。 |
Y
yangbo 已提交
1928 1929 1930

**返回值:**

Z
zhang-daiyue 已提交
1931 1932
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
H
huweiqi 已提交
1933
| Promise&lt;[image.PixelMap](js-apis-image.md#pixelmap7)&gt; | Promise对象,返回缩略图的PixelMap。 |
Y
yangbo 已提交
1934 1935 1936

**示例:**

Z
zhang-daiyue 已提交
1937
```ts
H
huweiqi 已提交
1938 1939
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1940
async function example() {
H
huweiqi 已提交
1941
  console.info('getThumbnailDemo');
Z
zhang-daiyue 已提交
1942 1943 1944 1945 1946 1947 1948 1949
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let size = { width: 720, height: 720 };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const asset = await fetchResult.getFirstObject();
H
huweiqi 已提交
1950
  console.info('asset displayName = ', asset.displayName);
Z
zhang-daiyue 已提交
1951 1952 1953
  asset.getThumbnail(size).then((pixelMap) => {
    console.info('getThumbnail successful ' + pixelMap);
  }).catch((err) => {
H
huweiqi 已提交
1954
    console.error('getThumbnail fail' + err);
Z
zhang-daiyue 已提交
1955
  });
Y
yangbo 已提交
1956 1957 1958
}
```

Z
zhang-daiyue 已提交
1959 1960 1961
### favorite

favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
Y
yangbo 已提交
1962

Z
zhang-daiyue 已提交
1963
将文件设置为收藏文件,使用callback方式返回异步结果。
Y
yangbo 已提交
1964

Z
zhang-daiyue 已提交
1965
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
1966 1967 1968 1969 1970

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

Z
zhang-daiyue 已提交
1971 1972
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
H
huweiqi 已提交
1973 1974
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
| callback   | AsyncCallback&lt;void&gt; | 是    | callback返回void。                              |
Y
yangbo 已提交
1975 1976 1977

**示例:**

Z
zhang-daiyue 已提交
1978
```ts
H
huweiqi 已提交
1979 1980
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
1981
async function example() {
H
huweiqi 已提交
1982
  console.info('favoriteDemo');
Z
zhang-daiyue 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const asset = await fetchResult.getFirstObject();
  asset.favorite(true, (err) => {
    if (err == undefined) {
H
huweiqi 已提交
1992
      console.info('favorite successfully');
Z
zhang-daiyue 已提交
1993
    } else {
H
huweiqi 已提交
1994
      console.error('favorite failed with error:' + err);
Z
zhang-daiyue 已提交
1995 1996
    }
  });
Y
yangbo 已提交
1997 1998 1999
}
```

Z
zhang-daiyue 已提交
2000 2001 2002
### favorite

favorite(isFavorite: boolean): Promise&lt;void&gt;
Y
yangbo 已提交
2003

Z
zhang-daiyue 已提交
2004
将文件设置为收藏文件,使用promise方式返回异步结果。
Y
yangbo 已提交
2005

Z
zhang-daiyue 已提交
2006
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
2007 2008 2009

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zhang-daiyue 已提交
2010 2011 2012 2013
**参数:**

| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
H
huweiqi 已提交
2014
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
Z
zhang-daiyue 已提交
2015

Y
yangbo 已提交
2016 2017
**返回值:**

Z
zhang-daiyue 已提交
2018 2019
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
2020
| Promise&lt;void&gt; | Promise对象,返回void。 |
Y
yangbo 已提交
2021 2022 2023

**示例:**

Z
zhang-daiyue 已提交
2024
```ts
H
huweiqi 已提交
2025 2026
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2027
async function example() {
H
huweiqi 已提交
2028
  console.info('favoriteDemo');
Z
zhang-daiyue 已提交
2029 2030 2031 2032 2033 2034 2035 2036
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const asset = await fetchResult.getFirstObject();
  asset.favorite(true).then(function () {
H
huweiqi 已提交
2037
    console.info('favorite successfully');
Z
zhang-daiyue 已提交
2038
  }).catch(function (err) {
H
huweiqi 已提交
2039
    console.error('favorite failed with error:' + err);
Z
zhang-daiyue 已提交
2040
  });
Y
yangbo 已提交
2041 2042 2043
}
```

H
huweiqi 已提交
2044
### setHidden<sup>10+</sup>
Y
yangbo 已提交
2045

H
huweiqi 已提交
2046
setHidden(hiddenState: boolean, callback: AsyncCallback&lt;void&gt;): void
Y
yangbo 已提交
2047

H
huweiqi 已提交
2048
将文件设置为隐私文件,使用callback方式返回异步结果。
Y
yangbo 已提交
2049

H
huweiqi 已提交
2050
隐私文件存在隐私相册中,对三方应用不开放,用户通过隐私相册去获取隐私文件后可以通过设置hiddenState为false来从隐私相册中移除。
Y
yangbo 已提交
2051

H
huweiqi 已提交
2052
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
2053 2054 2055

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
**参数:**

| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| hiddenState | boolean                   | 是    | 是否设置为隐藏文件,true:将文件资产放入隐藏相册;false:从隐藏相册中恢复。 |
| callback   | AsyncCallback&lt;void&gt; | 是    | callback返回void。                              |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('setHiddenDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const asset = await fetchResult.getFirstObject();
  asset.setHidden(true, (err) => {
    if (err == undefined) {
      console.info('setHidden successfully');
    } else {
      console.error('setHidden failed with error:' + err);
    }
  });
}
```

### setHidden<sup>10+</sup>

setHidden(hiddenState: boolean): Promise&lt;void&gt;

将文件设置为隐私文件,使用promise方式返回异步结果。

隐私文件存在隐私相册中,对三方应用不开放,用户通过隐私相册去获取隐私文件后可以通过设置hiddenState为false来从隐私相册中移除。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| hiddenState | boolean | 是    | 是否设置为隐藏文件,true:将文件资产放入隐藏相册;false:从隐藏相册中恢复。 |

**返回值:**

| 类型                  | 说明         |
| ------------------- | ---------- |
| Promise&lt;void&gt; | Promise对象,返回void。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  // 示例代码为将文件从隐藏相册中恢复,需要先在隐藏相册预置资源
  console.info('setHiddenDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let albumList = await mgr.getAlbums(userFileManager.AlbumType.SYSTEM, userFileManager.AlbumSubType.HIDDEN);
  const album = await albumList.getFirstObject();
  let fetchResult = await album.getPhotoAssets(fetchOption);
  const asset = await fetchResult.getFirstObject();
  asset.setHidden(false).then(() => {
    console.info('setHidden successfully');
  }).catch((err) => {
    console.error('setHidden failed with error:' + err);
Z
zhang-daiyue 已提交
2132
  });
Y
yangbo 已提交
2133 2134 2135
}
```

Z
zhang-daiyue 已提交
2136
## FetchResult
Y
yangbo 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147

文件检索结果集。

### getCount

getCount(): number

获取文件检索结果中的文件总数。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zengyawen 已提交
2148
**返回值:**
Y
yangbo 已提交
2149 2150 2151

| 类型     | 说明       |
| ------ | -------- |
H
huweiqi 已提交
2152
| number | 检索到的文件总数。 |
Y
yangbo 已提交
2153

H
huweiqi 已提交
2154
**示例:**
Y
yangbo 已提交
2155

Z
zhang-daiyue 已提交
2156
```ts
H
huweiqi 已提交
2157 2158
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2159
async function example() {
H
huweiqi 已提交
2160
  console.info('getCountDemo');
Z
zhang-daiyue 已提交
2161 2162 2163 2164 2165 2166 2167
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const fetchCount = fetchResult.getCount();
H
huweiqi 已提交
2168
  console.info('fetchCount = ', fetchCount);
Y
yangbo 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
}
```

### isAfterLast

isAfterLast(): boolean

检查结果集是否指向最后一行。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zengyawen 已提交
2180
**返回值:**
Y
yangbo 已提交
2181 2182 2183 2184 2185

| 类型      | 说明                                 |
| ------- | ---------------------------------- |
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |

H
huweiqi 已提交
2186
**示例:**
Y
yangbo 已提交
2187

Z
zhang-daiyue 已提交
2188
```ts
H
huweiqi 已提交
2189 2190
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2191
async function example() {
Z
zhang-daiyue 已提交
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  const fetchCount = fetchResult.getCount();
  console.info('count:' + fetchCount);
  let fileAsset = await fetchResult.getLastObject();
  if (!fetchResult.isAfterLast()) {
    console.info('fileAsset isAfterLast displayName = ', fileAsset.displayName);
  } else {
    console.info('fileAsset  not isAfterLast ');
  }
Y
yangbo 已提交
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
}
```

### close

close(): void

释放 FetchFileResult 实例并使其失效。无法调用其他方法。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2217
**示例:**
Y
yangbo 已提交
2218

Z
zhang-daiyue 已提交
2219
```ts
H
huweiqi 已提交
2220 2221
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2222
async function example() {
H
huweiqi 已提交
2223
  console.info('fetchResultCloseDemo');
Z
zhang-daiyue 已提交
2224 2225 2226 2227 2228
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
H
huweiqi 已提交
2229 2230 2231 2232 2233
  try {
    let fetchResult = await mgr.getPhotoAssets(fetchOption);
    await fetchResult.close();
    console.info('close succeed.');
  } catch (err) {
H
huweiqi 已提交
2234
    console.error('close fail. message = ' + err);
H
huweiqi 已提交
2235
  }
Y
yangbo 已提交
2236 2237 2238 2239 2240
}
```

### getFirstObject

Z
zhang-daiyue 已提交
2241
getFirstObject(callback: AsyncCallback&lt;T&gt;): void
Y
yangbo 已提交
2242

Z
zhang-daiyue 已提交
2243
获取文件检索结果中的第一个文件资产。此方法使用callback形式返回结果。
Y
yangbo 已提交
2244 2245 2246

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2247
**参数:**
Y
yangbo 已提交
2248 2249 2250

| 参数名   | 类型                                          | 必填 | 说明                                        |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
H
huweiqi 已提交
2251
| callback | AsyncCallback&lt;T&gt; | 是   | 异步获取结果集中的第一个完成后的回调。 |
Y
yangbo 已提交
2252

H
huweiqi 已提交
2253
**示例:**
Y
yangbo 已提交
2254

Z
zhang-daiyue 已提交
2255
```ts
H
huweiqi 已提交
2256 2257
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2258
async function example() {
H
huweiqi 已提交
2259
  console.info('getFirstObjectDemo');
Z
zhang-daiyue 已提交
2260 2261 2262 2263 2264 2265 2266 2267
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  fetchResult.getFirstObject((err, fileAsset) => {
    if (fileAsset != undefined) {
H
huweiqi 已提交
2268
      console.info('fileAsset displayName: ', fileAsset.displayName);
Z
zhang-daiyue 已提交
2269
    } else {
H
huweiqi 已提交
2270
      console.error('fileAsset failed with err:' + err);
Z
zhang-daiyue 已提交
2271 2272
    }
  });
Y
yangbo 已提交
2273 2274 2275 2276 2277
}
```

### getFirstObject

Z
zhang-daiyue 已提交
2278
getFirstObject(): Promise&lt;T&gt;
Y
yangbo 已提交
2279

Z
zhang-daiyue 已提交
2280
获取文件检索结果中的第一个文件资产。此方法使用promise方式来异步返回。
Y
yangbo 已提交
2281 2282 2283

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zengyawen 已提交
2284
**返回值:**
Y
yangbo 已提交
2285 2286 2287

| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
H
huweiqi 已提交
2288
| Promise&lt;T&gt; | Promise对象,返回结果集中第一个对象。 |
Y
yangbo 已提交
2289

H
huweiqi 已提交
2290
**示例:**
Y
yangbo 已提交
2291

Z
zhang-daiyue 已提交
2292
```ts
H
huweiqi 已提交
2293 2294
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2295
async function example() {
H
huweiqi 已提交
2296
  console.info('getFirstObjectDemo');
Z
zhang-daiyue 已提交
2297 2298 2299 2300 2301 2302 2303
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  let fileAsset = await fetchResult.getFirstObject();
H
huweiqi 已提交
2304
  console.info('fileAsset displayName: ', fileAsset.displayName);
Y
yangbo 已提交
2305 2306 2307 2308 2309
}
```

### getNextObject

Z
zhang-daiyue 已提交
2310
 getNextObject(callback: AsyncCallback&lt;T&gt;): void
Y
yangbo 已提交
2311 2312 2313 2314 2315

获取文件检索结果中的下一个文件资产。此方法使用callback形式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2316
**参数:**
Y
yangbo 已提交
2317 2318 2319

| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
H
huweiqi 已提交
2320
| callbacke | AsyncCallback&lt;T&gt; | 是   | 异步返回结果集中下一个之后的回调。 |
Y
yangbo 已提交
2321

H
huweiqi 已提交
2322
**示例:**
Y
yangbo 已提交
2323

Z
zhang-daiyue 已提交
2324
```ts
H
huweiqi 已提交
2325 2326
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2327
async function example() {
H
huweiqi 已提交
2328
  console.info('getNextObjectDemo');
Z
zhang-daiyue 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  await fetchResult.getFirstObject();
  if (fetchResult.isAfterLast()) {
    fetchResult.getNextObject((err, fileAsset) => {
      if (fileAsset != undefined) {
H
huweiqi 已提交
2339
        console.info('fileAsset displayName: ', fileAsset.displayName);
Z
zhang-daiyue 已提交
2340
      } else {
H
huweiqi 已提交
2341
        console.error('fileAsset failed with err: ' + err);
Z
zhang-daiyue 已提交
2342 2343 2344
      }
    });
  }
Y
yangbo 已提交
2345 2346 2347 2348 2349
}
```

### getNextObject

Z
zhang-daiyue 已提交
2350
 getNextObject(): Promise&lt;T&gt;
Y
yangbo 已提交
2351

Z
zhang-daiyue 已提交
2352
获取文件检索结果中的下一个文件资产。此方法使用promise方式来异步返回。
Y
yangbo 已提交
2353 2354 2355

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zengyawen 已提交
2356
**返回值:**
Y
yangbo 已提交
2357 2358 2359

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
2360
| Promise&lt;T&gt; | Promise对象,返回结果集中下一个对象。 |
Y
yangbo 已提交
2361

H
huweiqi 已提交
2362
**示例:**
Y
yangbo 已提交
2363

Z
zhang-daiyue 已提交
2364
```ts
H
huweiqi 已提交
2365 2366
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2367
async function example() {
H
huweiqi 已提交
2368
  console.info('getNextObjectDemo');
Z
zhang-daiyue 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  await fetchResult.getFirstObject();
  if (fetchResult.isAfterLast()) {
    let fileAsset = await fetchResult.getNextObject();
H
huweiqi 已提交
2378
    console.info('fileAsset displayName: ', fileAsset.displayName);
Z
zhang-daiyue 已提交
2379
  }
Y
yangbo 已提交
2380 2381 2382 2383 2384
}
```

### getLastObject

Z
zhang-daiyue 已提交
2385
getLastObject(callback: AsyncCallback&lt;T&gt;): void
Y
yangbo 已提交
2386

Z
zhang-daiyue 已提交
2387
获取文件检索结果中的最后一个文件资产。此方法使用callback回调来返回。
Y
yangbo 已提交
2388 2389 2390

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2391
**参数:**
Y
yangbo 已提交
2392 2393 2394

| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
H
huweiqi 已提交
2395
| callback | AsyncCallback&lt;T&gt; | 是   | 异步返回结果集中最后一个的回调。 |
Y
yangbo 已提交
2396

H
huweiqi 已提交
2397
**示例:**
Y
yangbo 已提交
2398

Z
zhang-daiyue 已提交
2399
```ts
H
huweiqi 已提交
2400 2401
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2402
async function example() {
H
huweiqi 已提交
2403
  console.info('getLastObjectDemo');
Z
zhang-daiyue 已提交
2404 2405 2406 2407 2408 2409 2410 2411
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  fetchResult.getLastObject((err, fileAsset) => {
    if (fileAsset != undefined) {
H
huweiqi 已提交
2412
      console.info('fileAsset displayName: ', fileAsset.displayName);
Z
zhang-daiyue 已提交
2413
    } else {
H
huweiqi 已提交
2414
      console.error('fileAsset failed with err: ' + err);
Z
zhang-daiyue 已提交
2415 2416
    }
  });
Y
yangbo 已提交
2417 2418 2419 2420 2421
}
```

### getLastObject

Z
zhang-daiyue 已提交
2422
getLastObject(): Promise&lt;T&gt;
Y
yangbo 已提交
2423

Z
zhang-daiyue 已提交
2424
获取文件检索结果中的最后一个文件资产。此方法使用Promise方式来返回。
Y
yangbo 已提交
2425 2426 2427

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

Z
zengyawen 已提交
2428
**返回值:**
Y
yangbo 已提交
2429 2430 2431

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
2432
| Promise&lt;T&gt; | Promise对象,返回结果集中最后一个对象。 |
Y
yangbo 已提交
2433

H
huweiqi 已提交
2434
**示例:**
Y
yangbo 已提交
2435

Z
zhang-daiyue 已提交
2436
```ts
H
huweiqi 已提交
2437 2438
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2439
async function example() {
H
huweiqi 已提交
2440
  console.info('getLastObjectDemo');
Z
zhang-daiyue 已提交
2441 2442 2443 2444 2445 2446 2447
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  let fileAsset = await fetchResult.getLastObject();
H
huweiqi 已提交
2448
  console.info('fileAsset displayName: ', fileAsset.displayName);
Y
yangbo 已提交
2449 2450 2451 2452 2453
}
```

### getPositionObject

Z
zhang-daiyue 已提交
2454
getPositionObject(index: number, callback: AsyncCallback&lt;T&gt;): void
Y
yangbo 已提交
2455

Z
zhang-daiyue 已提交
2456
获取文件检索结果中具有指定索引的文件资产。此方法使用callback来返回。
Y
yangbo 已提交
2457 2458 2459

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896
**参数:**

| 参数名       | 类型                                       | 必填   | 说明                 |
| -------- | ---------------------------------------- | ---- | ------------------ |
| index    | number                                   | 是    | 要获取的文件的索引,从0开始。     |
| callback | AsyncCallback&lt;T&gt; | 是    | 异步返回指定索引的文件资产的回调。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('getPositionObjectDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  fetchResult.getPositionObject(0, (err, fileAsset) => {
    if (fileAsset != undefined) {
      console.info('fileAsset displayName: ', fileAsset.displayName);
    } else {
      console.error('fileAsset failed with err: ' + err);
    }
  });
}
```

### getPositionObject

getPositionObject(index: number): Promise&lt;T&gt;

获取文件检索结果中具有指定索引的文件资产。此方法使用Promise形式返回文件Asset。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名    | 类型     | 必填   | 说明             |
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始。 |

**返回值:**

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;T&gt; | Promise对象,返回结果集中指定索引的一个对象。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('getPositionObjectDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  let fileAsset = await fetchResult.getPositionObject(0);
  console.info('fileAsset displayName: ', fileAsset.displayName);
}
```

### getAllObject<sup>10+</sup>

getAllObject(callback: AsyncCallback&lt;Array&lt;T&gt;&gt;): void

获取文件检索结果中的所有文件资产。此方法使用callback形式返回结果。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                                          | 必填 | 说明                                        |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;Array&lt;T&gt;&gt; | 是   | 异步获取结果集中的所有文件资产完成后的回调。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('getAllObjectDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  fetchResult.getAllObject((err, fileAssetList) => {
    if (fileAssetList != undefined) {
      console.info('fileAssetList length: ', fileAssetList.length);
    } else {
      console.error('fileAssetList failed with err:' + err);
    }
  });
}
```

### getAllObject<sup>10+</sup>

getAllObject(): Promise&lt;Array&lt;T&gt;&gt;

获取文件检索结果中的所有文件资产。此方法使用promise方式来异步返回。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**返回值:**

| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
| Promise&lt;Array&lt;T&gt;&gt; | Promise对象,返回结果集中所有文件资产数组。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('getAllObjectDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  let fetchResult = await mgr.getPhotoAssets(fetchOption);
  let fileAssetList = await fetchResult.getAllObject();
  console.info('fileAssetList length: ', fileAssetList.length);
}
```

## Album

实体相册

### 属性

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

| 名称           | 类型    | 可读   | 可写  | 说明   |
| ------------ | ------ | ---- | ---- | ------- |
| albumType<sup>8+</sup> | [AlbumType]( #albumtype10) | 是    | 否    | 相册类型。    |
| albumSubType<sup>8+</sup> | [AlbumSubType]( #albumsubtype10) | 是    | 否   | 相册子类型。    |
| albumName | string | 是    | 用户相册可写,预置相册不可写   | 相册名称。    |
| albumUri | string | 是    | 否    | 相册Uri。   |
| count | number | 是    | 否    |  相册中文件数量。 |
| coverUri | string | 是    | 用户相册可写,预置相册不可写	    | 封面文件Uri。 |

### getPhotoAssets

getPhotoAssets(options: FetchOptions, callback: AsyncCallback&lt;FetchResult&lt;FileAsset&gt;&gt;): void;

获取相册中的文件。该方法使用callback形式来返回文件。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| options | [FetchOptions](#fetchoptions) | 是   | 检索选项。 |
| callback | AsyncCallback&lt;[FetchResult](#fetchresult)&lt;[FileAsset](#fileasset)&gt;&gt; | 是   | callback返回图片和视频数据结果集。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('albumGetFileAssetsDemoCallback');

  let predicates = new dataSharePredicates.DataSharePredicates();
  let albumFetchOptions = {
    predicates: predicates
  };
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const albumList = await mgr.getPhotoAlbums(albumFetchOptions);
  const album = await albumList.getFirstObject();
  album.getPhotoAssets(fetchOption, (err, albumFetchResult) => {
    if (albumFetchResult != undefined) {
      console.info('album getPhotoAssets successfully, getCount: ' + albumFetchResult.getCount());
    } else {
      console.error('album getPhotoAssets failed with error: ' + err);
    }
  });
}
```

### getPhotoAssets

getPhotoAssets(options: FetchOptions): Promise&lt;FetchResult&lt;FileAsset&gt;&gt;;

获取相册中的文件。该方法使用Promise来返回文件。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| options | [FetchOptions](#fetchoptions) | 是   | 检索选项。 |

**返回值:**

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FetchResult](#fetchresult)&lt;[FileAsset](#fileasset)&gt;&gt; | Promise对象,返回图片和视频数据结果集。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('albumGetFileAssetsDemoPromise');

  let predicates = new dataSharePredicates.DataSharePredicates();
  let albumFetchOptions = {
    predicates: predicates
  };
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const albumList = await mgr.getPhotoAlbums(albumFetchOptions);
  const album = await albumList.getFirstObject();
  album.getPhotoAssets(fetchOption).then((albumFetchResult) => {
    console.info('album getFileAssets successfully, getCount: ' + albumFetchResult.getCount());
  }).catch((err) => {
    console.error('album getFileAssets failed with error: ' + err);
  });
}
```

### commitModify

commitModify(callback: AsyncCallback&lt;void&gt;): void;

更新相册属性修改到数据库中。该方法使用callback形式来返回结果。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('albumCommitModifyDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let albumFetchOptions = {
    predicates: predicates
  };
  const albumList = await mgr.getPhotoAlbums(albumFetchOptions);
  const album = await albumList.getFirstObject();
  album.albumName = 'hello';
  album.commitModify((err) => {
    if (err != undefined) {
      console.error('commitModify failed with error: ' + err);
    } else {
      console.info('commitModify successfully');
    }
  });
}
```

### commitModify

commitModify(): Promise&lt;void&gt;;

更新相册属性修改到数据库中。该方法使用Promise来返回结果。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**返回值:**

| 类型                  | 说明           |
| ------------------- | ------------ |
| Promise&lt;void&gt; | Promise对象,返回void。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  console.info('albumCommitModifyDemo');
  let predicates = new dataSharePredicates.DataSharePredicates();
  let albumFetchOptions = {
    predicates: predicates
  };
  try {
    var albumList = await mgr.getPhotoAlbums(albumFetchOptions);
  } catch (err) {
    console.error('getPhotoAlbums failed. message = ', err);
  }
  const album = await albumList.getFirstObject();
  album.albumName = 'hello';
  album.commitModify().then(() => {
    console.info('commitModify successfully');
  }).catch((err) => {
    console.error('commitModify failed with error: ' + err);
  });
}
```

### addPhotoAssets<sup>10+</sup>

addPhotoAssets(assets: Array&lt;FileAsset&gt;, callback: AsyncCallback&lt;void&gt;): void;

往相册中添加图片或者视频,需要先预置相册和文件资源。该方法使用callback形式来返回结果。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 待添加到相册中的图片或视频数组。 |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  try {
    console.info('addPhotoAssetsDemoCallback');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await mgr.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.addPhotoAssets([asset], (err) => {
      if (err === undefined) {
        console.info('album addPhotoAssets successfully');
      } else {
        console.error('album addPhotoAssets failed with error: ' + err);
      }
    });
  } catch (err) {
    console.error('addPhotoAssetsDemoCallback failed with error: ' + err);
  }
}
```

### addPhotoAssets<sup>10+</sup>

addPhotoAssets(assets: Array&lt;FileAsset&gt;): Promise&lt;void&gt;;

往相册中添加图片或者视频,需要先预置相册和文件资源。该方法使用Promise来返回结果。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 待添加到相册中的图片或视频数组。 |

**返回值:**

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
|Promise&lt;void&gt; | Promise对象,返回void。 |

**示例:**

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

async function example() {
  try {
    console.info('addPhotoAssetsDemoPromise');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await mgr.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.addPhotoAssets([asset]).then(() => {
      console.info('album addPhotoAssets successfully');
    }).catch((err) => {
      console.error('album addPhotoAssets failed with error: ' + err);
    });
  } catch (err) {
    console.error('addPhotoAssetsDemoPromise failed with error: ' + err);
  }
}
```

### removePhotoAssets<sup>10+</sup>

removePhotoAssets(assets: Array&lt;FileAsset&gt;, callback: AsyncCallback&lt;void&gt;): void;

从相册中移除图片或者视频,需要先预置相册和文件资源。该方法使用callback形式来返回结果。

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

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

**参数:**
Y
yangbo 已提交
2897

H
huweiqi 已提交
2898 2899 2900 2901
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 相册中待移除的图片或视频数组。 |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |
Y
yangbo 已提交
2902

H
huweiqi 已提交
2903
**示例:**
Y
yangbo 已提交
2904

Z
zhang-daiyue 已提交
2905
```ts
H
huweiqi 已提交
2906 2907
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2908
async function example() {
H
huweiqi 已提交
2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
  try {
    console.info('removePhotoAssetsDemoCallback');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await album.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.removePhotoAssets([asset], (err) => {
      if (err === undefined) {
        console.info('album removePhotoAssets successfully');
      } else {
        console.error('album removePhotoAssets failed with error: ' + err);
      }
    });
  } catch (err) {
    console.error('removePhotoAssetsDemoCallback failed with error: ' + err);
  }
Y
yangbo 已提交
2930 2931 2932
}
```

H
huweiqi 已提交
2933
### removePhotoAssets<sup>10+</sup>
Y
yangbo 已提交
2934

H
huweiqi 已提交
2935
removePhotoAssets(assets: Array&lt;FileAsset&gt;): Promise&lt;void&gt;;
Y
yangbo 已提交
2936

H
huweiqi 已提交
2937 2938 2939
从相册中移除图片或者视频,需要先预置相册和文件资源。该方法使用Promise来返回结果。

**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
2940 2941 2942

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2943
**参数:**
Y
yangbo 已提交
2944

H
huweiqi 已提交
2945 2946 2947
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 相册中待移除的图片或视频数组。 |
Y
yangbo 已提交
2948

Z
zengyawen 已提交
2949
**返回值:**
Y
yangbo 已提交
2950 2951 2952

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
2953
|Promise&lt;void&gt; | Promise对象,返回void。 |
Y
yangbo 已提交
2954

H
huweiqi 已提交
2955
**示例:**
Y
yangbo 已提交
2956

Z
zhang-daiyue 已提交
2957
```ts
H
huweiqi 已提交
2958 2959
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Y
yangbo 已提交
2960
async function example() {
H
huweiqi 已提交
2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979
  try {
    console.info('removePhotoAssetsDemoPromise');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.USER, userFileManager.AlbumSubType.USER_GENERIC);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await album.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.removePhotoAssets([asset]).then(() => {
      console.info('album removePhotoAssets successfully');
    }).catch((err) => {
      console.error('album removePhotoAssets failed with error: ' + err);
    });
  } catch (err) {
    console.error('removePhotoAssetsDemoPromise failed with error: ' + err);
  }
Y
yangbo 已提交
2980 2981 2982
}
```

H
huweiqi 已提交
2983
### recoverPhotoAssets<sup>10+</sup>
Y
yangbo 已提交
2984

H
huweiqi 已提交
2985
recoverPhotoAssets(assets: Array&lt;FileAsset&gt;, callback: AsyncCallback&lt;void&gt;): void;
Z
zhang-daiyue 已提交
2986

H
huweiqi 已提交
2987
从回收站中恢复图片或者视频,需要先在回收站中预置文件资源。该方法使用callback形式来返回结果。
Z
zhang-daiyue 已提交
2988

H
huweiqi 已提交
2989
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Z
zhang-daiyue 已提交
2990 2991 2992

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
2993
**参数:**
Z
zhang-daiyue 已提交
2994 2995 2996

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
2997 2998
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 回收站中待恢复图片或者视频数组。 |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |
Z
zhang-daiyue 已提交
2999

H
huweiqi 已提交
3000
**示例:**
Z
zhang-daiyue 已提交
3001 3002

```ts
H
huweiqi 已提交
3003 3004
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3005
async function example() {
H
huweiqi 已提交
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026
  try {
    console.info('recoverPhotoAssetsDemoCallback');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.SYSTEM, userFileManager.AlbumSubType.TRASH);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await album.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.recoverPhotoAssets([asset], (err) => {
      if (err === undefined) {
        console.info('album recoverPhotoAssets successfully');
      } else {
        console.error('album recoverPhotoAssets failed with error: ' + err);
      }
    });
  } catch (err) {
    console.error('recoverPhotoAssetsDemoCallback failed with error: ' + err);
  }
Z
zhang-daiyue 已提交
3027 3028
}
```
Z
zengyawen 已提交
3029

H
huweiqi 已提交
3030
### recoverPhotoAssets<sup>10+</sup>
Z
zhang-daiyue 已提交
3031

H
huweiqi 已提交
3032
recoverPhotoAssets(assets: Array&lt;FileAsset&gt;): Promise&lt;void&gt;;
Z
zhang-daiyue 已提交
3033

H
huweiqi 已提交
3034
从回收站中恢复图片或者视频,需要先在回收站中预置文件资源。该方法使用Promise来返回结果。
Z
zhang-daiyue 已提交
3035

H
huweiqi 已提交
3036
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Z
zhang-daiyue 已提交
3037 3038 3039

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3040
**参数:**
Z
zhang-daiyue 已提交
3041 3042 3043

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3044 3045 3046
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 回收站中待恢复图片或者视频数组。 |

**返回值:**
Z
zhang-daiyue 已提交
3047

H
huweiqi 已提交
3048 3049 3050
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
|Promise&lt;void&gt; | Promise对象,返回void。 |
Z
zhang-daiyue 已提交
3051

H
huweiqi 已提交
3052
**示例:**
Z
zhang-daiyue 已提交
3053 3054

```ts
H
huweiqi 已提交
3055 3056
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3057
async function example() {
H
huweiqi 已提交
3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
  try {
    console.info('recoverPhotoAssetsDemoPromise');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.SYSTEM, userFileManager.AlbumSubType.TRASH);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await album.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.recoverPhotoAssets([asset]).then(() => {
      console.info('album recoverPhotoAssets successfully');
    }).catch((err) => {
      console.error('album recoverPhotoAssets failed with error: ' + err);
    });
  } catch (err) {
    console.error('recoverPhotoAssetsDemoPromise failed with error: ' + err);
  }
Z
zhang-daiyue 已提交
3077 3078 3079
}
```

H
huweiqi 已提交
3080
### deletePhotoAssets<sup>10+</sup>
Y
yangbo 已提交
3081

H
huweiqi 已提交
3082
deletePhotoAssets(assets: Array&lt;FileAsset&gt;, callback: AsyncCallback&lt;void&gt;): void;
Y
yangbo 已提交
3083

H
huweiqi 已提交
3084
从回收站中彻底删除图片或者视频,需要先在回收站中预置文件资源。该方法使用callback形式来返回结果。
Y
yangbo 已提交
3085

H
huweiqi 已提交
3086
**注意**:此操作不可逆,执行此操作后文件资源将彻底删除,请谨慎操作。
Y
yangbo 已提交
3087

Z
zhang-daiyue 已提交
3088
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
3089 3090 3091

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3092
**参数:**
Y
yangbo 已提交
3093 3094 3095

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3096 3097
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 回收站中待彻底删除图片或者视频数组。 |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |
Y
yangbo 已提交
3098

H
huweiqi 已提交
3099
**示例:**
Y
yangbo 已提交
3100 3101

```ts
H
huweiqi 已提交
3102 3103
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3104
async function example() {
H
huweiqi 已提交
3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
  try {
    console.info('deletePhotoAssetsDemoCallback');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.SYSTEM, userFileManager.AlbumSubType.TRASH);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await album.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.deletePhotoAssets([asset], (err) => {
      if (err === undefined) {
        console.info('album deletePhotoAssets successfully');
      } else {
        console.error('album deletePhotoAssets failed with error: ' + err);
      }
    });
  } catch (err) {
    console.error('deletePhotoAssetsDemoCallback failed with error: ' + err);
  }
Y
yangbo 已提交
3126 3127 3128
}
```

H
huweiqi 已提交
3129
### deletePhotoAssets<sup>10+</sup>
Y
yangbo 已提交
3130

H
huweiqi 已提交
3131
deletePhotoAssets(assets: Array&lt;FileAsset&gt;): Promise&lt;void&gt;;
Y
yangbo 已提交
3132

H
huweiqi 已提交
3133
从回收站中彻底删除图片或者视频,需要先在回收站中预置文件资源。该方法使用Promise来返回结果。
Y
yangbo 已提交
3134

H
huweiqi 已提交
3135
**注意**:此操作不可逆,执行此操作后文件资源将彻底删除,请谨慎操作。
Y
yangbo 已提交
3136

Z
zhang-daiyue 已提交
3137
**需要权限**:ohos.permission.WRITE_IMAGEVIDEO
Y
yangbo 已提交
3138 3139 3140

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3141 3142 3143 3144 3145 3146
**参数:**

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| assets | Array&lt;[FileAsset](#fileasset)&gt; | 是   | 回收站中待彻底删除图片或者视频数组。 |

Z
zengyawen 已提交
3147
**返回值:**
Y
yangbo 已提交
3148

H
huweiqi 已提交
3149 3150 3151
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
|Promise&lt;void&gt; | Promise对象,返回void。 |
Y
yangbo 已提交
3152

H
huweiqi 已提交
3153
**示例:**
Y
yangbo 已提交
3154 3155

```ts
H
huweiqi 已提交
3156 3157
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3158 3159
async function example() {
  try {
H
huweiqi 已提交
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174
    console.info('deletePhotoAssetsDemoPromise');
    let predicates = new dataSharePredicates.DataSharePredicates();
    let fetchOption = {
      fetchColumns: [],
      predicates: predicates
    };
    let albumFetchResult = await mgr.getAlbums(userFileManager.AlbumType.SYSTEM, userFileManager.AlbumSubType.TRASH);
    let album = await albumFetchResult.getFirstObject();
    let fetchResult = await album.getPhotoAssets(fetchOption);
    let asset = await fetchResult.getFirstObject();
    album.deletePhotoAssets([asset]).then(() => {
      console.info('album deletePhotoAssets successfully');
    }).catch((err) => {
      console.error('album deletePhotoAssets failed with error: ' + err);
    });
Z
zhang-daiyue 已提交
3175
  } catch (err) {
H
huweiqi 已提交
3176
    console.error('deletePhotoAssetsDemoPromise failed with error: ' + err);
Z
zhang-daiyue 已提交
3177
  }
Y
yangbo 已提交
3178 3179 3180
}
```

Z
zhang-daiyue 已提交
3181
## PrivateAlbum
H
huweiqi 已提交
3182 3183

系统相册。
Z
zhang-daiyue 已提交
3184 3185 3186

### 属性

H
huweiqi 已提交
3187
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Z
zhang-daiyue 已提交
3188 3189 3190

| 名称           | 类型    | 可读   | 可写   | 说明      |
| ------------ | ------ | ---- | ---- | ------- |
H
huweiqi 已提交
3191 3192 3193 3194 3195
| albumName | string | 是    | 是    | 相册名称。    |
| albumUri | string | 是    | 否    | 相册Uri。   |
| dateModified | number | 是    | 否    | 修改日期。    |
| count | number | 是    | 否    | 相册中文件数量。 |
| coverUri | string | 是    | 否    | 封面文件Uri。 |
Z
zhang-daiyue 已提交
3196 3197

### getPhotoAssets
Y
yangbo 已提交
3198

Z
zhang-daiyue 已提交
3199
getPhotoAssets(options: FetchOptions, callback: AsyncCallback&lt;FetchResult&lt;FileAsset&gt;&gt;): void;
Y
yangbo 已提交
3200

H
huweiqi 已提交
3201
获取系统相册中的文件。该方法使用callback形式来返回文件。
Y
yangbo 已提交
3202

H
huweiqi 已提交
3203
**需要权限**:ohos.permission.READ_IMAGEVIDEO
Y
yangbo 已提交
3204 3205 3206

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3207
**参数:**
Y
yangbo 已提交
3208

Z
zhang-daiyue 已提交
3209 3210
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3211 3212
| options | [FetchOptions](#fetchoptions) | 是   | 检索选项。 |
| callback | AsyncCallback&lt;[FetchResult](#fetchresult)&lt;[FileAsset](#fileasset)&gt;&gt; | 是   | callback返回图片和视频数据结果集。 |
Y
yangbo 已提交
3213

H
huweiqi 已提交
3214
**示例:**
Y
yangbo 已提交
3215 3216

```ts
H
huweiqi 已提交
3217 3218
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3219
async function example() {
H
huweiqi 已提交
3220
  console.info('privateAlbumGetFileAssetsDemoCallback');
Z
zhang-daiyue 已提交
3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232
  let albumList = await mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH);
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const trashAlbum = await albumList.getFirstObject();
  trashAlbum.getPhotoAssets(fetchOption, (err, fetchResult) => {
    if (fetchResult != undefined) {
      let count = fetchResult.getCount();
      console.info('fetchResult.count = ', count);
    } else {
H
huweiqi 已提交
3233
      console.error('getFileAssets failed, message = ', err);
Z
zhang-daiyue 已提交
3234 3235
    }
  });
Y
yangbo 已提交
3236 3237
}

Z
zhang-daiyue 已提交
3238
```
Z
zengyawen 已提交
3239

Z
zhang-daiyue 已提交
3240
### getPhotoAssets
Y
yangbo 已提交
3241

Z
zhang-daiyue 已提交
3242
getPhotoAssets(options: FetchOptions): Promise&lt;FetchResult&lt;FileAsset&gt;&gt;;
Y
yangbo 已提交
3243

H
huweiqi 已提交
3244
获取系统相册中的文件。该方法使用Promise来返回文件。
Y
yangbo 已提交
3245

H
huweiqi 已提交
3246
**需要权限**:ohos.permission.READ_IMAGEVIDEO
Y
yangbo 已提交
3247 3248 3249

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3250
**参数:**
Y
yangbo 已提交
3251

Z
zhang-daiyue 已提交
3252 3253
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3254
| options | [FetchOptions](#fetchoptions) | 是   | 检索选项。 |
Z
zhang-daiyue 已提交
3255

Z
zengyawen 已提交
3256
**返回值:**
Z
zhang-daiyue 已提交
3257 3258 3259

| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
3260
| Promise:[FetchResult](#fetchresult)&lt;[FileAsset](#fileasset)&gt;| Promise对象,返回图片和视频数据结果集。 |
Y
yangbo 已提交
3261

H
huweiqi 已提交
3262
**示例:**
Y
yangbo 已提交
3263 3264

```ts
H
huweiqi 已提交
3265 3266
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3267
async function example() {
H
huweiqi 已提交
3268
  console.info('privateAlbumGetFileAssetsDemoPromise');
Z
zhang-daiyue 已提交
3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279
  let albumList = await mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH);
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const trashAlbum = await albumList.getFirstObject();
  let fetchResult = await trashAlbum.getPhotoAssets(fetchOption);
  let count = fetchResult.getCount();
  console.info('fetchResult.count = ', count);
}
Y
yangbo 已提交
3280
```
Z
zengyawen 已提交
3281

Z
zhang-daiyue 已提交
3282 3283 3284
### delete

delete(uri: string, callback: AsyncCallback&lt;void&gt;): void;
Y
yangbo 已提交
3285

H
huweiqi 已提交
3286
删除系统相册中的文件。
Y
yangbo 已提交
3287

Z
zhang-daiyue 已提交
3288 3289 3290 3291
**需要权限**:ohos.permission.READ_IMAGEVIDEO 和 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 和 ohos.permission.WRITE_AUDIO

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3292
**参数:**
Z
zhang-daiyue 已提交
3293 3294 3295

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3296 3297
| uri | string | 是   | 相册uri。 |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |
Z
zhang-daiyue 已提交
3298

H
huweiqi 已提交
3299
**示例:**
Z
zhang-daiyue 已提交
3300 3301

```ts
H
huweiqi 已提交
3302 3303
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317
async function example() {
  console.info('privateAlbumDeleteCallback');
  let albumList = await mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH);
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const trashAlbum = await albumList.getFirstObject();
  let fetchResult = await trashAlbum.getPhotoAssets(fetchOption);
  const fileAsset = await fetchResult.getFirstObject();
  let deleteFileUri = fileAsset.uri;
  trashAlbum.delete(deleteFileUri, (err) => {
    if (err != undefined) {
H
huweiqi 已提交
3318
      console.error('trashAlbum.delete failed, message = ', err);
Z
zhang-daiyue 已提交
3319 3320 3321 3322 3323 3324
    } else {
      console.info('trashAlbum.delete successfully');
    }
  });
}
```
Z
zengyawen 已提交
3325

Z
zhang-daiyue 已提交
3326
### delete
Y
yangbo 已提交
3327

Z
zhang-daiyue 已提交
3328
delete(uri: string): Promise&lt;void&gt;;
Y
yangbo 已提交
3329

H
huweiqi 已提交
3330
删除系统相册中的文件。
Y
yangbo 已提交
3331

Z
zhang-daiyue 已提交
3332
**需要权限**:ohos.permission.READ_IMAGEVIDEO 和 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 和 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
3333 3334 3335

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3336
**参数:**
Y
yangbo 已提交
3337

Z
zhang-daiyue 已提交
3338 3339
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3340
| uri | string | 是   | 相册uri。 |
Y
yangbo 已提交
3341

Z
zengyawen 已提交
3342
**返回值:**
Y
yangbo 已提交
3343

Z
zhang-daiyue 已提交
3344 3345
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
3346
| Promise&lt;void&gt;| Promise对象,返回void。 |
Y
yangbo 已提交
3347

H
huweiqi 已提交
3348
**示例:**
Y
yangbo 已提交
3349 3350

```ts
H
huweiqi 已提交
3351 3352
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3353
async function example() {
H
huweiqi 已提交
3354
  console.info('privateAlbumDeleteDemoPromise');
Z
zhang-daiyue 已提交
3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367
  let albumList = await mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH);
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const trashAlbum = await albumList.getFirstObject();
  let fetchResult = await trashAlbum.getPhotoAssets(fetchOption);
  const fileAsset = await fetchResult.getFirstObject();
  let deleteFileUri = fileAsset.uri;
  trashAlbum.delete(deleteFileUri).then(() => {
    console.info('trashAlbum.delete successfully');
  }).catch((err) => {
H
huweiqi 已提交
3368
    console.error('trashAlbum.delete failed, message = ', err);
Z
zhang-daiyue 已提交
3369 3370
  });
}   
Y
yangbo 已提交
3371 3372
```

Z
zhang-daiyue 已提交
3373
### recover
Y
yangbo 已提交
3374

Z
zhang-daiyue 已提交
3375
recover(uri: string, callback: AsyncCallback&lt;void&gt;): void;
李云帆 已提交
3376

H
huweiqi 已提交
3377
恢复系统相册中的文件。
Y
yangbo 已提交
3378

H
huweiqi 已提交
3379
**需要权限**:ohos.permission.READ_IMAGEVIDEO 和 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 和 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
3380 3381 3382

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3383
**参数:**
Z
zhang-daiyue 已提交
3384 3385 3386

| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3387 3388
| uri | string | 是   | 相册uri。 |
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回void。 |
Y
yangbo 已提交
3389

H
huweiqi 已提交
3390
**示例:**
Y
yangbo 已提交
3391 3392

```ts
H
huweiqi 已提交
3393 3394
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408
async function example() {
  console.info('privateAlbumRecoverDemoCallback');
  let albumList = await mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH);
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const trashAlbum = await albumList.getFirstObject();
  let fetchResult = await trashAlbum.getPhotoAssets(fetchOption);
  const fileAsset = await fetchResult.getFirstObject();
  let recoverFileUri = fileAsset.uri;
  trashAlbum.recover(recoverFileUri, (err) => {
    if (err != undefined) {
H
huweiqi 已提交
3409
      console.error('trashAlbum.recover failed, message = ', err);
Z
zhang-daiyue 已提交
3410 3411
    } else {
      console.info('trashAlbum.recover successfully');
Y
yangbo 已提交
3412
    }
Z
zhang-daiyue 已提交
3413
  });
Y
yangbo 已提交
3414 3415
}
```
Z
zengyawen 已提交
3416

Z
zhang-daiyue 已提交
3417
### recover
Y
yangbo 已提交
3418

Z
zhang-daiyue 已提交
3419
recover(uri: string): Promise&lt;void&gt;;
Y
yangbo 已提交
3420

H
huweiqi 已提交
3421
恢复系统相册中的文件。
李云帆 已提交
3422

H
huweiqi 已提交
3423
**需要权限**:ohos.permission.READ_IMAGEVIDEO 和 ohos.permission.WRITE_IMAGEVIDEO 或 ohos.permission.READ_AUDIO 和 ohos.permission.WRITE_AUDIO
Y
yangbo 已提交
3424 3425 3426

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

H
huweiqi 已提交
3427
**参数:**
Y
yangbo 已提交
3428

Z
zhang-daiyue 已提交
3429 3430
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
3431
| uri | string | 是   | 相册uri。 |
Y
yangbo 已提交
3432

Z
zengyawen 已提交
3433
**返回值:**
Y
yangbo 已提交
3434

Z
zhang-daiyue 已提交
3435 3436
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
3437
| Promise&lt;void&gt;| Promise对象,返回void。 |
Y
yangbo 已提交
3438

H
huweiqi 已提交
3439
**示例:**
Y
yangbo 已提交
3440 3441

```ts
H
huweiqi 已提交
3442 3443
import dataSharePredicates from '@ohos.data.dataSharePredicates';

Z
zhang-daiyue 已提交
3444
async function example() {
H
huweiqi 已提交
3445
  console.info('privateAlbumRecoverDemoPromise');
Z
zhang-daiyue 已提交
3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458
  let albumList = await mgr.getPrivateAlbum(userFileManager.PrivateAlbumType.TYPE_TRASH);
  let predicates = new dataSharePredicates.DataSharePredicates();
  let fetchOption = {
    fetchColumns: [],
    predicates: predicates
  };
  const trashAlbum = await albumList.getFirstObject();
  let fetchResult = await trashAlbum.getPhotoAssets(fetchOption);
  const fileAsset = await fetchResult.getFirstObject();
  let recoverFileUri = fileAsset.uri;
  trashAlbum.recover(recoverFileUri).then(() => {
    console.info('trashAlbum.recover successfully');
  }).catch((err) => {
H
huweiqi 已提交
3459
    console.error('trashAlbum.recover failed, message = ', err);
Z
zhang-daiyue 已提交
3460
  });
Y
yangbo 已提交
3461 3462 3463
}
```

Z
zhang-daiyue 已提交
3464 3465 3466 3467
## MemberType

成员类型。

H
huweiqi 已提交
3468
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
zyjhandsome's avatar
zyjhandsome 已提交
3469

H
huweiqi 已提交
3470 3471
| 名称  |  类型 |  可读  |  可写  |  说明  |
| ----- |  ---- |  ---- |  ---- |  ---- |
H
huweiqi 已提交
3472 3473 3474
| number |  number | 是 | 是 | number类型。 |
| string |  string | 是 | 是 | string类型。|
| boolean |  boolean | 是 | 是 | boolean类型。 |
Z
zhang-daiyue 已提交
3475 3476 3477 3478 3479

## ChangeEvent

变更监听的媒体文件类型。

H
huweiqi 已提交
3480
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Z
zhang-daiyue 已提交
3481

H
huweiqi 已提交
3482
| 名称  |  类型 |  可读  |  可写  |  说明 |
H
huweiqi 已提交
3483
| ----- |  ---- |  ---- |  ---- |  ---- |
H
huweiqi 已提交
3484 3485 3486 3487 3488 3489
| deviceChange |  string | 是 | 是 |  设备。 |
| albumChange |  string | 是 | 是 |  相册。 |
| imageChange |  string | 是 | 是 |  图片。 |
| audioChange |  string | 是 | 是 |  音频。 |
| videoChange |  string | 是 | 是 |  视频。 |
| remoteFileChange |  string | 是 | 是 |  远程文件。 |
Z
zhang-daiyue 已提交
3490

Y
yangbo 已提交
3491 3492 3493 3494
## PeerInfo

注册设备的信息。

H
huweiqi 已提交
3495
**系统能力**:SystemCapability.FileManagement.UserFileManager.DistributedCore
Y
yangbo 已提交
3496 3497 3498

| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
H
huweiqi 已提交
3499 3500 3501
| deviceName | string                     | 是   | 否   | 注册设备的名称。   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID。 |
| isOnline   | boolean                    | 是   | 否   | 是否在线。         |
Y
yangbo 已提交
3502

Z
zhang-daiyue 已提交
3503 3504 3505
## FileType

枚举,媒体文件类型。
Y
yangbo 已提交
3506

H
huweiqi 已提交
3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

| 名称  |  值 |  说明 |
| ----- |  ---- |  ---- |
| IMAGE |  1 |  图片。 |
| VIDEO |  2 |  视频。 |
| AUDIO |  3 |  音频。 |

## PhotoSubType<sup>10+</sup>

枚举,不同[FileAsset](#fileasset)的类型。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

| 名称  |  值 |  说明 |
| ----- |  ---- |  ---- |
| DEFAULT |  0 |  默认照片类型。 |
| SCREENSHOT |  1 |  截屏录屏文件类型。 |
| CAMERA |  2 |  相机拍摄的照片和视频类型。 |

## PositionType<sup>10+</sup>

枚举,文件位置,表示文件在本地或云端。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

| 名称  |  值 |  说明 |
| ----- |  ---- |  ---- |
| LOCAL |  1 |  文件只存在于本端设备。 |
| CLOUD |  2 |  文件只存在于云端。 |
| BOTH |  3 |  文件在本地和云中都存在。 |

## AlbumType<sup>10+</sup>

枚举,相册类型,表示是用户相册还是系统预置相册。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

| 名称  |  值 |  说明 |
| ----- |  ---- |  ---- |
| USER |  0 |  用户相册。 |
| SYSTEM |  1024 |  系统预置相册。 |

## AlbumSubType<sup>10+</sup>

枚举,相册子类型,表示具体的相册类型。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
3555

H
huweiqi 已提交
3556 3557
| 名称  |  值 |  说明 |
| ----- |  ---- |  ---- |
H
huweiqi 已提交
3558 3559 3560 3561 3562 3563 3564 3565
| USER_GENERIC |  1 |  用户相册。 |
| FAVORITE |  1025 |  收藏夹。 |
| VIDEO |  1026 |  视频相册。 |
| HIDDEN |  1027 |  隐藏相册。 |
| TRASH |  1028 |  回收站。 |
| SCREENSHOT |  1029 |  截屏和录屏相册。 |
| CAMERA |  1030 |  相机拍摄的照片和视频相册。 |
| ANY |  2147483647 |  任意相册。 |
Y
yangbo 已提交
3566

Z
zhang-daiyue 已提交
3567
## PrivateAlbumType
Y
yangbo 已提交
3568

Z
zhang-daiyue 已提交
3569
枚举,系统相册类型。
Y
yangbo 已提交
3570

H
huweiqi 已提交
3571
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
3572

H
huweiqi 已提交
3573 3574
| 名称    |  值 |   说明   |
| -----   |  ----  |   ----  |
H
huweiqi 已提交
3575 3576
| TYPE_FAVORITE |  0 |  收藏夹相册。 |
| TYPE_TRASH |  1 |  回收站相册。 |
Z
zhang-daiyue 已提交
3577

Y
yangbo 已提交
3578 3579 3580 3581
## AudioKey

枚举,音频文件关键信息。

H
huweiqi 已提交
3582
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
3583

H
huweiqi 已提交
3584
| 名称          |   值              | 说明                                                       |
Y
yangbo 已提交
3585
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
3586 3587 3588 3589 3590 3591 3592 3593 3594
| URI           | uri                 | 文件uri。                                                   |
| DISPLAY_NAME  | display_name        | 显示名字。                                                   |
| DATE_ADDED    | date_added          | 添加日期(添加文件时间距1970年1月1日的秒数值)。             |
| DATE_MODIFIED | date_modified       | 修改日期(修改文件时间距1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)。 |
| TITLE         | title               | 文件标题。                                                   |
| ARTIST        | artist              | 作者。                                                   |
| AUDIOALBUM    | audio_album         | 专辑。                                                   |
| DURATION      | duration            | 持续时间(单位:毫秒)。                                    |
| FAVORITE      | favorite            | 收藏。                                                   |
Y
yangbo 已提交
3595 3596 3597 3598 3599

## ImageVideoKey

枚举,图片和视频文件关键信息。

H
huweiqi 已提交
3600
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
3601

H
huweiqi 已提交
3602
| 名称          | 值              | 说明                                                       |
Y
yangbo 已提交
3603
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
| URI           | uri                 | 文件uri。                                                   |
| FILE_TYPE     | file_type           | 媒体文件类型。                                              |
| DISPLAY_NAME  | display_name        | 显示名字。                                                   |
| DATE_ADDED    | date_added          | 添加日期(添加文件时间距1970年1月1日的秒数值)。             |
| DATE_MODIFIED | date_modified       | 修改日期(修改文件时间距1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)。 |
| TITLE         | title               | 文件标题。                                                   |
| DURATION      | duration            | 持续时间(单位:毫秒)。                                    |
| WIDTH         | width               | 图片宽度(单位:像素)。                                    |
| HEIGHT        | height              | 图片高度(单位:像素)。                                      |
| DATE_TAKEN    | date_taken          | 拍摄日期(文件拍照时间距1970年1月1日的秒数值)。                |
| ORIENTATION   | orientation         | 图片文件的方向。                                             |
| FAVORITE      | favorite            | 收藏。                                                    |
| POSITION<sup>10+</sup>  | position            | 文件位置类型。                               |
| DATE_TRASHED<sup>10+</sup>  | date_trashed  | 删除日期(删除文件时间距1970年1月1日的秒数值)。                 |
| HIDDEN<sup>10+</sup>  | hidden            | 文件的隐藏状态。                               |
Y
yangbo 已提交
3619 3620 3621 3622 3623

## AlbumKey

枚举,相册关键信息。

H
huweiqi 已提交
3624
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
3625

H
huweiqi 已提交
3626
| 名称          | 值              | 说明                                                       |
Y
yangbo 已提交
3627
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642
| URI           | uri                 | 相册uri。                                                   |
| FILE_TYPE     | file_type           | 媒体文件类型。                                              |
| ALBUM_NAME    | album_name          | 相册名字。                                                   |
| DATE_ADDED    | date_added          | 添加日期(添加文件时间距1970年1月1日的秒数值)。             |
| DATE_MODIFIED | date_modified       | 修改日期(修改文件时间距1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)。 |

## PhotoCreateOptions<sup>10+</sup>

图片或视频的创建选项。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

| 名称                   | 类型                | 必填 | 说明                                              |
| ---------------------- | ------------------- | ---- | ------------------------------------------------ |
| subType           | [PhotoSubType](#photosubtype10) | 否  | 图片或者视频的子类型。  |
Y
yangbo 已提交
3643

Z
zhang-daiyue 已提交
3644
## FetchOptions
Y
yangbo 已提交
3645 3646 3647

检索条件。

H
huweiqi 已提交
3648
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
3649

H
huweiqi 已提交
3650 3651
| 名称                   | 类型                | 可读 | 可写 | 说明                                              |
| ---------------------- | ------------------- | ---- |---- | ------------------------------------------------ |
H
huweiqi 已提交
3652 3653
| fetchColumns           | Array&lt;string&gt; | 是   | 是   | 检索条件,指定列名查询,如果该参数为空时默认查询uri、name、fileType(具体字段名称以检索对象定义为准)。示例:<br />fetchColumns: ['uri', 'title']。 |
| predicates           | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md) | 是   | 是   | 谓词查询,显示过滤条件。 |
Y
yangbo 已提交
3654

Z
zhang-daiyue 已提交
3655
## AlbumFetchOptions
Y
yangbo 已提交
3656

Z
zhang-daiyue 已提交
3657
相册检索条件。
李云帆 已提交
3658

H
huweiqi 已提交
3659
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
Y
yangbo 已提交
3660

H
huweiqi 已提交
3661 3662
| 名称                   | 类型                | 可读 | 可写 | 说明                                              |
| ---------------------- | ------------------- | ---- |---- | ------------------------------------------------ |
H
huweiqi 已提交
3663
| predicates           | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md) | 是   | 是   | 谓词查询,显示过滤条件。 |
Y
yangbo 已提交
3664

C
caochuan 已提交
3665 3666 3667 3668 3669 3670 3671 3672
## ChangeData<sup>10+</sup>

监听器回调函数的值。

**系统能力**:SystemCapability.FileManagement.UserFileManager.Core

| 名称    | 类型                        | 可读 | 可写 | 说明                                                         |
| ------- | --------------------------- | ---- | ---- | ------------------------------------------------------------ |
3673 3674 3675
| type    | [NotifyType](#notifytype10) | 是   | 否   | ChangeData的通知类型。                                       |
| uris    | Array&lt;string&gt;         | 是   | 否   | 相同[NotifyType](#notifytype10)的所有uri,可以是FileAsset或Album。 |
| subUris | Array&lt;string&gt;         | 是   | 否   | 相册中变动文件的uri数组。                                    |
C
caochuan 已提交
3676 3677 3678

## NotifyType<sup>10+</sup>

3679
枚举,通知事件的类型。
C
caochuan 已提交
3680

3681
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
C
caochuan 已提交
3682

3683 3684 3685 3686 3687 3688 3689
| 名称                      | 值   | 说明                             |
| ------------------------- | ---- | -------------------------------- |
| NOTIFY_ADD                | 0    | 添加文件集或相册通知的类型。     |
| NOTIFY_UPDATE             | 1    | 文件集或相册的更新通知类型。     |
| NOTIFY_REMOVE             | 2    | 删除文件集或相册的通知类型。     |
| NOTIFY_ALBUM_ADD_ASSET    | 3    | 在相册中添加的文件集的通知类型。 |
| NOTIFY_ALBUM_REMOVE_ASSET | 4    | 在相册中删除的文件集的通知类型。 |
C
caochuan 已提交
3690 3691 3692 3693 3694

## DefaultChangeUri<sup>10+</sup>

枚举,DefaultChangeUri子类型。

3695
**系统能力**:SystemCapability.FileManagement.UserFileManager.Core
C
caochuan 已提交
3696

3697 3698 3699 3700 3701 3702
| 名称              | 值                      | 说明                                                         |
| ----------------- | ----------------------- | ------------------------------------------------------------ |
| DEFAULT_PHOTO_URI | file://media/Photo      | 默认PhotoAsset的Uri,与forSubUri{true}一起使用,将接收所有PhotoAsset的更改通知。 |
| DEFAULT_ALBUM_URI | file://media/PhotoAlbum | 默认相册的Uri,与forSubUri{true}一起使用,将接收所有相册的更改通知。 |
| DEFAULT_AUDIO_URI | file://media/Audio      | 默认AudioAsset的Uri,与forSubUri{true}一起使用,将接收所有AudioAsset的更改通知。 |