js-apis-medialibrary.md 91.3 KB
Newer Older
Z
zengyawen 已提交
1 2
# 媒体库管理

P
panqiangbiao 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
P
panqiangbiao 已提交
5

Z
zengyawen 已提交
6
## 导入模块
7
```js
8
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
P
panqiangbiao 已提交
9 10
```

Z
zengyawen 已提交
11
## mediaLibrary.getMediaLibrary<sup>8+</sup>
P
panqiangbiao 已提交
12

P
panqiangbiao 已提交
13
getMediaLibrary(context: Context): MediaLibrary
P
panqiangbiao 已提交
14

Z
zengyawen 已提交
15
获取媒体库的实例,用于访问和修改用户等个人媒体数据信息(如音频、视频、图片、文档等)。
P
panqiangbiao 已提交
16

W
wangqinxiao 已提交
17 18
此接口仅可在Stage模型下使用。

P
panqiangbiao 已提交
19
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
20

P
panqiangbiao 已提交
21
**参数:** 
P
panqiangbiao 已提交
22

Z
zengyawen 已提交
23 24 25
| 参数名  | 类型    | 必填 | 说明                       |
| ------- | ------- | ---- | -------------------------- |
| context | Context | 是   | 传入Ability实例的Context。 |
P
panqiangbiao 已提交
26 27 28

**返回值:**

H
HelloCrease 已提交
29 30
| 类型                            | 说明    |
| ----------------------------- | :---- |
P
panqiangbiao 已提交
31
| [MediaLibrary](#medialibrary) | 媒体库实例 |
P
panqiangbiao 已提交
32

潘强标 已提交
33 34
**示例:(从API Version 9开始)**

35
```ts
36 37
const context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
潘强标 已提交
38 39 40
```

**示例:(API Version 8)**
P
panqiangbiao 已提交
41

42
```js
P
panqiangbiao 已提交
43
import featureAbility from '@ohos.ability.featureAbility';
P
panqiangbiao 已提交
44

45
let context = featureAbility.getContext();
46
let media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
47
```
Z
zengyawen 已提交
48 49 50 51 52 53
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

获取媒体库的实例,用于访问和修改用户等个人媒体数据信息(如音频、视频、图片、文档等)。

W
wangqinxiao 已提交
54 55
此接口仅可在FA模型下使用。

Z
zengyawen 已提交
56
> **说明**: 从API Version 8开始,该接口不再维护,推荐使用新接口[mediaLibrary.getMediaLibrary<sup>8+</sup>](#medialibrarygetmedialibrary8)。
Z
zengyawen 已提交
57 58 59 60 61 62 63 64 65 66 67 68

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**返回值:**

| 类型                          | 说明       |
| ----------------------------- | :--------- |
| [MediaLibrary](#medialibrary) | 媒体库实例 |

**示例:**

```js
69
let media = mediaLibrary.getMediaLibrary();
Z
zengyawen 已提交
70 71
```

P
panqiangbiao 已提交
72
## MediaLibrary
P
panqiangbiao 已提交
73

Z
zengyawen 已提交
74
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
75 76


P
panqiangbiao 已提交
77
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 
P
panqiangbiao 已提交
78 79 80

获取文件资源,使用callback方式返回异步结果。

P
panqiangbiao 已提交
81
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
82

P
panqiangbiao 已提交
83
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
84

P
panqiangbiao 已提交
85 86
**参数:**

Z
zengyawen 已提交
87 88 89 90
| 参数名   | 类型                                                | 必填 | 说明                              |
| -------- | --------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 文件获取选项                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步获取FetchFileResult之后的回调 |
P
panqiangbiao 已提交
91 92 93

**示例:**

94
```js
95 96
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
Z
zhang-daiyue 已提交
97
let imagesFetchOp = {
P
panqiangbiao 已提交
98 99 100
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
Z
zhang-daiyue 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
    if (fetchFileResult == undefined) {
        console.error('Failed to get fetchFileResult: ' + error);
        return;
    }
    const count = fetchFileResult.getCount();
    if (count < 0) {
        console.error('Failed to get count from fetchFileResult: count: ' + count);
        return;
    }
    if (count == 0) {
        console.info('The count of fetchFileResult is zero');
        return;
    }

    console.info('Get fetchFileResult success, count: ' + count);
    fetchFileResult.getFirstObject((err, fileAsset) => {
        if (fileAsset == undefined) {
            console.error('Failed to get first object: ' + err);
            return;
        }
        console.log('fileAsset.displayName ' + ': ' + fileAsset.displayName);
        for (let i = 1; i < count; i++) {
124
            fetchFileResult.getNextObject((err, fileAsset) => {
Z
zhang-daiyue 已提交
125 126 127 128 129
                if (fileAsset == undefined) {
                    console.error('Failed to get next object: ' + err);
                    return;
                }
                console.log('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
130
            })
Z
zhang-daiyue 已提交
131 132
        }
    });
P
panqiangbiao 已提交
133
});
P
panqiangbiao 已提交
134
```
Z
zengyawen 已提交
135
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
136

P
panqiangbiao 已提交
137
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
138 139 140

获取文件资源,使用Promise方式返回结果。

P
panqiangbiao 已提交
141
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
142

P
panqiangbiao 已提交
143
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
144

P
panqiangbiao 已提交
145 146
**参数:**

Z
zengyawen 已提交
147 148 149
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 文件检索选项 |
P
panqiangbiao 已提交
150 151 152

**返回值**

Z
zengyawen 已提交
153 154 155
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | 文件数据结果集 |
P
panqiangbiao 已提交
156 157 158

**示例:**

159
```js
160 161
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
Z
zhang-daiyue 已提交
162
let imagesFetchOp = {
P
panqiangbiao 已提交
163 164 165
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
Z
zhang-daiyue 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
media.getFileAssets(imagesFetchOp).then(function(fetchFileResult) {
    const count = fetchFileResult.getCount();
    if (count < 0) {
        console.error('Failed to get count from fetchFileResult: count: ' + count);
        return;
    }
    if (count == 0) {
        console.info('The count of fetchFileResult is zero');
        return;
    }
    console.info('Get fetchFileResult success, count: ' + count);
    fetchFileResult.getFirstObject().then(function(fileAsset) {
        console.log('fileAsset.displayName ' + ': ' + fileAsset.displayName);
        for (let i = 1; i < count; i++) {
            fetchFileResult.getNextObject().then(function(fileAsset) {
                console.log('fileAsset.displayName ' + ': ' + fileAsset.displayName);
            }).catch(function(err) {
                console.error('Failed to get next object: ' + err);
            })
        }
    }).catch(function(err) {
        console.error('Failed to get first object: ' + err);
    });
P
panqiangbiao 已提交
189
}).catch(function(err){
Z
zhang-daiyue 已提交
190
    console.error("Failed to get file assets: " + err);
P
panqiangbiao 已提交
191
});
P
panqiangbiao 已提交
192 193
```

P
panqiangbiao 已提交
194
### on<sup>8+</sup>
P
panqiangbiao 已提交
195

P
panqiangbiao 已提交
196
on(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
197

198
打开媒体库变更通知,使用callback方式返回异步结果。
P
panqiangbiao 已提交
199

P
panqiangbiao 已提交
200
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
201

P
panqiangbiao 已提交
202 203
**参数:**

H
HelloCrease 已提交
204 205 206 207
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | 是    | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'fileChange':     &nbsp;文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更 |
| callback | callback&lt;void&gt; | 是    | 回调返回空                                    |
P
panqiangbiao 已提交
208 209 210

**示例:**

211
```js
Z
zhang-daiyue 已提交
212
media.on('imageChange', () => {
P
panqiangbiao 已提交
213
    // image file had changed, do something
P
panqiangbiao 已提交
214 215
})
```
P
panqiangbiao 已提交
216
### off<sup>8+</sup>
P
panqiangbiao 已提交
217

P
panqiangbiao 已提交
218
off(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback?: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
219

220
关闭媒体库变更通知,使用callback方式返回异步结果。
P
panqiangbiao 已提交
221

P
panqiangbiao 已提交
222
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
223

P
panqiangbiao 已提交
224 225
**参数:**

H
HelloCrease 已提交
226 227 228 229
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | 是    | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'fileChange':     &nbsp;文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更 |
| callback | callback&lt;void&gt; | 否    | 回调返回空                                    |
P
panqiangbiao 已提交
230 231 232

**示例:**

233
```js
潘强标 已提交
234
media.off('imageChange', () => {
P
panqiangbiao 已提交
235
    // stop listening success
P
panqiangbiao 已提交
236 237 238
})
```

B
bmeangel 已提交
239
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
240

P
panqiangbiao 已提交
241
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
242 243 244

创建媒体资源,使用callback方式返回结果。

P
panqiangbiao 已提交
245
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
246

P
panqiangbiao 已提交
247
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
248

P
panqiangbiao 已提交
249 250
**参数:**

Z
zengyawen 已提交
251 252 253 254 255 256
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型                                                     |
| displayName  | string                                  | 是   | 展示文件名                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
257 258 259

**示例:**

260
```js
P
panqiangbiao 已提交
261 262 263 264 265
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
潘强标 已提交
266
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
P
panqiangbiao 已提交
267 268 269 270 271 272 273
        if (fileAsset != undefined) {
            console.info('createAsset successfully, message = ' + err);
        } else {
            console.info('createAsset failed, message = ' + err);
        }
    });
}
P
panqiangbiao 已提交
274 275
```

P
panqiangbiao 已提交
276
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
277

P
panqiangbiao 已提交
278
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
279 280 281

创建媒体资源,使用Promise方式返回结果。

P
panqiangbiao 已提交
282
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
283

P
panqiangbiao 已提交
284
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
285

P
panqiangbiao 已提交
286 287
**参数:**

Z
zengyawen 已提交
288 289 290 291 292
| 参数名       | 类型                     | 必填 | 说明                                                         |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | 是   | 媒体类型                                                     |
| displayName  | string                   | 是   | 展示文件名                                                   |
| relativePath | string                   | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
293 294 295

**返回值**

Z
zengyawen 已提交
296 297 298
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | 媒体数据FileAsset |
P
panqiangbiao 已提交
299 300 301

**示例:**

302
```js
H
huweiqi 已提交
303 304 305 306 307 308 309 310 311 312 313
async function example() {
    // 使用Promise方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
    media.createAsset(mediaType, 'imagePromise.jpg', path + 'myPicture/').then((fileAsset) => {
        console.info('createAsset successfully, message = ' + JSON.stringify(fileAsset));
    }).catch((err) => {
        console.info('createAsset failed, message = ' + err);
    });
}
P
panqiangbiao 已提交
314 315
```

Z
zengyawen 已提交
316 317 318 319 320 321 322 323
### deleteAsset<sup>8+</sup>

deleteAsset(uri: string): Promise\<void>

删除媒体文件资源

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

324
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | 是    | 需要删除的媒体文件资源的uri |

**返回值:**
| 类型                  | 说明                   |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise回调返回删除的结果。 |

**示例:**

```js
async function example() {
343 344
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
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
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const context = getContext(this);
    var media = mediaLibrary.getMediaLibrary(context);
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
        console.error('asset not exist')
        return
    }
    media.deleteAsset(asset.uri).then(() => {
        console.info("deleteAsset successfully");
    }).catch((err) => {
        console.info("deleteAsset failed with error:"+ err);
    });
}
```

### deleteAsset<sup>8+</sup>
deleteAsset(uri: string, callback: AsyncCallback\<void>): void

删除媒体文件资源

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

372
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | 是    | 需要删除的媒体文件资源的uri。 |
|callback |AsyncCallback\<void>| 是  |回调函数,用于获取删除的结果。|

**示例:**

```js
async function example() {
387 388
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const context = getContext(this);
    var media = mediaLibrary.getMediaLibrary(context);
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
        console.error('asset not exist')
        return
    }
    media.deleteAsset(asset.uri, (err) => {
        if (err != undefined) {
            console.info("deleteAsset successfully");
        } else {
            console.info("deleteAsset failed with error:"+ err);
        }
    });
}
```

P
panqiangbiao 已提交
411
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
412

P
panqiangbiao 已提交
413
getPublicDirectory(type: DirectoryType, callback: AsyncCallback&lt;string&gt;): void
P
panqiangbiao 已提交
414

P
panqiangbiao 已提交
415
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
416 417 418 419 420

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

Z
zengyawen 已提交
421 422 423 424
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback 返回公共目录路径 |
P
panqiangbiao 已提交
425 426 427

**示例:**

428
```js
P
panqiangbiao 已提交
429
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
P
panqiangbiao 已提交
430
media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => {
P
panqiangbiao 已提交
431
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
432
        console.info('mediaLibraryTest : getPublicDirectory passed');
P
panqiangbiao 已提交
433
    } else {
P
panqiangbiao 已提交
434
        console.info('mediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
435 436 437 438
    }
});
```

P
panqiangbiao 已提交
439
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
440

P
panqiangbiao 已提交
441
getPublicDirectory(type: DirectoryType): Promise&lt;string&gt;
P
panqiangbiao 已提交
442

P
panqiangbiao 已提交
443
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
444 445 446 447 448

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

Z
zengyawen 已提交
449 450 451
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
452 453 454

**返回值:**

Z
zengyawen 已提交
455 456 457
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
458 459 460

**示例:**

461
```js
P
panqiangbiao 已提交
462
async function example() {
P
panqiangbiao 已提交
463 464
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
    const dicResult = await media.getPublicDirectory(DIR_CAMERA);
P
panqiangbiao 已提交
465
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
466 467 468 469
        console.info('MediaLibraryTest : getPublicDirectory');
    } else {
        console.info('MediaLibraryTest : getPublicDirectory failed');
    }
P
panqiangbiao 已提交
470 471 472
}
```

Z
zengyawen 已提交
473
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
474

P
panqiangbiao 已提交
475
getAlbums(options: MediaFetchOptions, callback: AsyncCallback<Array&lt;Album&gt;>): void
P
panqiangbiao 已提交
476

P
panqiangbiao 已提交
477
获取相册列表,使用callback 方式返回结果。
P
panqiangbiao 已提交
478

P
panqiangbiao 已提交
479
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
480

P
panqiangbiao 已提交
481
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
482

P
panqiangbiao 已提交
483 484
**参数**

Z
zengyawen 已提交
485 486 487 488
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册获取条件                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
489 490 491

**示例:**

492
```js
P
panqiangbiao 已提交
493 494 495 496
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
497
media.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
498 499 500 501 502 503 504
    if (albumList != undefined) {
        const album = albumList[0];
        console.info('album.albumName = ' + album.albumName);
        console.info('album.count = ' + album.count);
     } else {
        console.info('getAlbum fail, message = ' + err);
     }
P
panqiangbiao 已提交
505
})
P
panqiangbiao 已提交
506 507
```

Z
zengyawen 已提交
508
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
509

P
panqiangbiao 已提交
510
getAlbums(options: MediaFetchOptions): Promise<Array&lt;Album&gt;>
P
panqiangbiao 已提交
511

P
panqiangbiao 已提交
512
获取相册列表,使用 promise 方式返回结果。
P
panqiangbiao 已提交
513

P
panqiangbiao 已提交
514
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
515

P
panqiangbiao 已提交
516
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
517

P
panqiangbiao 已提交
518 519
**参数:**

Z
zengyawen 已提交
520 521 522
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
523 524 525

**返回值:**

Z
zengyawen 已提交
526 527 528
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
529 530 531

**示例:**

532
```js
P
panqiangbiao 已提交
533 534 535 536
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
537
media.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
P
panqiangbiao 已提交
538 539 540 541
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
    console.info("getAlbums failed with error:"+ err);
});
P
panqiangbiao 已提交
542 543
```

P
panqiangbiao 已提交
544
### release<sup>8+</sup>
P
panqiangbiao 已提交
545

P
panqiangbiao 已提交
546
release(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
547

P
panqiangbiao 已提交
548 549
释放MediaLibrary实例。
当后续不需要使用MediaLibrary实例中的方法时调用。
P
panqiangbiao 已提交
550

P
panqiangbiao 已提交
551
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
552

P
panqiangbiao 已提交
553 554
**参数:**

H
HelloCrease 已提交
555 556 557
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调表示成功还是失败 |
P
panqiangbiao 已提交
558 559 560

**示例:**

561
```js
P
panqiangbiao 已提交
562
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
563
media.release((err) => {
P
panqiangbiao 已提交
564
    // do something
P
panqiangbiao 已提交
565
});
P
panqiangbiao 已提交
566 567
```

P
panqiangbiao 已提交
568
### release<sup>8+</sup>
P
panqiangbiao 已提交
569

P
panqiangbiao 已提交
570
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
571

P
panqiangbiao 已提交
572 573
释放MediaLibrary实例。
当后续不需要使用MediaLibrary实例中的方法时调用。
P
panqiangbiao 已提交
574

P
panqiangbiao 已提交
575
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
576

P
panqiangbiao 已提交
577 578
**返回值:**

H
HelloCrease 已提交
579 580
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
581
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
582 583 584

**示例:**

585
```js
P
panqiangbiao 已提交
586
media.release()
P
panqiangbiao 已提交
587 588
```

Z
update  
zengyawen 已提交
589
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
590 591 592 593 594

storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback&lt;string&gt;): void

保存媒体资源,以异步方法获取保存成功的URI,使用callback形式返回结果。

Z
update  
zengyawen 已提交
595
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
596 597 598 599 600

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

H
HelloCrease 已提交
601 602 603 604
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
| callback | AsyncCallback&lt;string&gt;           | 是    | 媒体资源保存回调,返回保存成功后得到的URI。 |
P
panqiangbiao 已提交
605 606 607

**示例:**

608
```js
P
panqiangbiao 已提交
609
let option = {
潘强标 已提交
610 611 612
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
613 614 615 616 617 618 619 620 621
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (err, value) => {
    if (err) {
        console.log("An error occurred when storing media resources.");
        return;
    }
    console.log("Media resources stored. ");
    // Obtain the URI that stores media resources.
});
622
```
P
panqiangbiao 已提交
623 624


Z
update  
zengyawen 已提交
625
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
626 627 628 629 630

storeMediaAsset(option: MediaAssetOption): Promise&lt;string&gt;

保存媒体资源,以异步方法获取保存成功的URI,使用Promise形式返回结果。

Z
update  
zengyawen 已提交
631
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
632 633 634 635 636

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

H
HelloCrease 已提交
637 638 639
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
640 641 642

**返回值:**

H
HelloCrease 已提交
643 644
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
645 646 647 648
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

649
```js
P
panqiangbiao 已提交
650
let option = {
潘强标 已提交
651 652 653
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
654 655 656 657 658 659 660
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
    console.log("Media resources stored.");
    // Obtain the URI that stores media resources.
}).catch((err) => {
    console.log("An error occurred when storing media resources.");
});
661
```
P
panqiangbiao 已提交
662 663


Z
update  
zengyawen 已提交
664
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
665 666 667

startImagePreview(images: Array&lt;string&gt;, index: number, callback: AsyncCallback&lt;void&gt;): void

Z
zhang-daiyue 已提交
668
启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(datashare://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。
P
panqiangbiao 已提交
669

670
> **说明**: <br/>从API Version 9开始废弃。建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
671 672 673 674 675

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

H
HelloCrease 已提交
676 677
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
678
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
679 680
| index    | number                    | 是    | 开始显示的图片序号。                               |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
681 682 683

**示例:**

684
```js
P
panqiangbiao 已提交
685
let images = [
Z
zhang-daiyue 已提交
686 687
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
688
];
H
HelloCrease 已提交
689
/* 网络图片使用方式
P
panqiangbiao 已提交
690 691 692 693
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
694
*/
P
panqiangbiao 已提交
695 696 697 698 699 700 701 702
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
703
```
P
panqiangbiao 已提交
704 705


Z
update  
zengyawen 已提交
706
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
707 708 709

startImagePreview(images: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void

Z
zhang-daiyue 已提交
710
启动图片预览界面,可以预览列表中首张本地图片(datashare://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。
P
panqiangbiao 已提交
711

712
> **说明**: <br/>从API Version 9开始废弃。建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
713 714 715 716 717

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

H
HelloCrease 已提交
718 719
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
720
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
721
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
722 723 724

**示例:**

725
```js
P
panqiangbiao 已提交
726
let images = [
Z
zhang-daiyue 已提交
727 728
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
729
];
H
HelloCrease 已提交
730
/* 网络图片使用方式
P
panqiangbiao 已提交
731 732 733 734
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
735
*/
P
panqiangbiao 已提交
736 737 738 739 740 741 742
mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
743
```
P
panqiangbiao 已提交
744 745


Z
update  
zengyawen 已提交
746
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
747 748 749

startImagePreview(images: Array&lt;string&gt;, index?: number): Promise&lt;void&gt;

Z
zhang-daiyue 已提交
750
启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(datashare://),也可以预览列表中的所有网络图片(https://)。使用Promise方式进行异步回调。
P
panqiangbiao 已提交
751

752
> **说明**: <br/>从API Version 9开始废弃。建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
753 754 755 756 757

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

H
HelloCrease 已提交
758 759
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
760
| images | Array&lt;string&gt; | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
761
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
762 763 764

**返回值:**

H
HelloCrease 已提交
765 766
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
P
panqiangbiao 已提交
767 768 769 770
| Promise&lt;void&gt; | Promise实例,用于异步获取预览结果,失败时返回错误信息。 |

**示例:**

771
```js
P
panqiangbiao 已提交
772
let images = [
Z
zhang-daiyue 已提交
773 774
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
775
];
H
HelloCrease 已提交
776
/* 网络图片使用方式
P
panqiangbiao 已提交
777 778 779 780
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
781
*/
P
panqiangbiao 已提交
782 783 784 785 786 787
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
    console.log("Succeeded in previewing the images.");
}).catch((err) => {
    console.log("An error occurred when previewing the images.");
});
788
```
P
panqiangbiao 已提交
789 790


Z
update  
zengyawen 已提交
791
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
792 793 794 795 796

startMediaSelect(option: MediaSelectOption, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void

启动媒体选择界面,以异步方法获取选择的媒体URI列表,使用callback形式返回结果。

797
> **说明**: <br/>从API Version 9开始废弃。建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
798 799 800 801 802

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

H
HelloCrease 已提交
803 804
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
Z
zengyawen 已提交
805
| option   | [MediaSelectOption](#mediaselectoptiondeprecated)  | 是    | 媒体选择选项。                              |
Z
zhang-daiyue 已提交
806
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 媒体选择回调,返回选择的媒体URI(datashare://)列表。 |
P
panqiangbiao 已提交
807 808 809

**示例:**

810
```js
Z
zhang-daiyue 已提交
811 812 813
let option : mediaLibrary.MediaSelectOption = {
    type : "media",
    count : 2
P
panqiangbiao 已提交
814
};
Z
zhang-daiyue 已提交
815
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
P
panqiangbiao 已提交
816 817 818 819 820 821 822
    if (err) {
        console.log("An error occurred when selecting media resources.");
        return;
    }
    console.log("Media resources selected.");
    // Obtain the media selection value.
});
823
```
P
panqiangbiao 已提交
824 825


Z
update  
zengyawen 已提交
826
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
827 828 829 830 831

startMediaSelect(option: MediaSelectOption): Promise&lt;Array&lt;string&gt;&gt;

启动媒体选择界面,以异步方法获取选择的媒体URI列表,使用Promise形式返回结果。

832
> **说明**: <br/>从API Version 9开始废弃。建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
833 834 835 836 837

**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

H
HelloCrease 已提交
838 839
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
Z
zengyawen 已提交
840
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
841 842 843

**返回值:**

H
HelloCrease 已提交
844 845
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
Z
zhang-daiyue 已提交
846
| Promise&lt;Array&lt;string&gt;&gt; | Promise实例,用于异步获取选择的媒体URI(datashare://)列表。 |
P
panqiangbiao 已提交
847 848 849

**示例:**

850
```js
Z
zhang-daiyue 已提交
851 852 853
let option : mediaLibrary.MediaSelectOption = {
    type : "media",
    count : 2
P
panqiangbiao 已提交
854
};
Z
zhang-daiyue 已提交
855
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
P
panqiangbiao 已提交
856 857 858 859 860 861
    console.log("Media resources selected.");
    // Obtain the media selection value.
}).catch((err) => {
    console.log("An error occurred when selecting media resources.");
});

Z
zengyawen 已提交
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
```
### getActivePeers<sup>8+</sup>

getActivePeers(): Promise\<Array\<PeerInfo>>;

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

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

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

**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
|  Promise\<Array\<PeerInfo>> | 返回获取的所有在线对端设备的PeerInfo |

**示例:**

```js
async function example() {
    const context = getContext(this);
    var media = mediaLibrary.getMediaLibrary(context);
    media.getActivePeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
            console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed info is undefined!')
        }
    }).catch((err) => {
        console.info("get distributed info failed with error:" + err);
    });
}
```

### getActivePeers<sup>8+</sup>
902

Z
zengyawen 已提交
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
getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
| callback: AsyncCallback\<Array\<PeerInfo>> | 返回获取的所有在线对端设备的PeerInfo |

**示例:**

```js
async function example() {
    const context = getContext(this);
    var media = mediaLibrary.getMediaLibrary(context);
    media.getActivePeers((err, devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
                console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed fail, message = ' + err)
        }
    })
}
```


### getAllPeers<sup>8+</sup>

getAllPeers(): Promise\<Array\<PeerInfo>>;

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

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

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

**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
|  Promise\<Array\<PeerInfo>> | 返回获取的所有对端设备的PeerInfo |

**示例:**

```js
async function example() {
    const context = getContext(this);
    var media = mediaLibrary.getMediaLibrary(context);
    media.getAllPeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
                console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed info is undefined!')
        }
    }).catch((err) => {
        console.info("get distributed info failed with error:" + err);
    });
}
```

### getAllPeers<sup>8+</sup>
977

Z
zengyawen 已提交
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
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
| callback: AsyncCallback\<Array\<PeerInfo>> | 返回获取的所有对端设备的PeerInfo |

**示例:**

```js
async function example() {
    const context = getContext(this);
    var media = mediaLibrary.getMediaLibrary(context);
    media.getAllPeers((err, devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
            console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed fail, message = ' + err)
        }
    })
}
1010
```
P
panqiangbiao 已提交
1011

Z
zengyawen 已提交
1012
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
1013 1014 1015

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

Z
zengyawen 已提交
1016 1017 1018
### 属性

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1019

Z
zengyawen 已提交
1020 1021 1022
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| id                        | number                   | 是   | 否   | 文件资源编号                                           |
Z
zhang-daiyue 已提交
1023
| uri                       | string                   | 是   | 否   | 文件资源uri(如:datashare:///media/image/2)         |
Z
zengyawen 已提交
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
| mimeType                  | string                   | 是   | 否   | 文件扩展属性                                           |
| mediaType<sup>8+</sup>    | [MediaType](#mediatype8) | 是   | 否   | 媒体类型                                               |
| displayName               | string                   | 是   | 是   | 显示文件名,包含后缀名                                 |
| title                     | string                   | 是   | 是   | 文件标题                                               |
| relativePath<sup>8+</sup> | string                   | 是   | 是   | 相对公共目录路径                                       |
| parent<sup>8+</sup>       | number                   | 是   | 否   | 父目录id                                               |
| size                      | number                   | 是   | 否   | 文件大小(单位:字节)                                 |
| dateAdded                 | number                   | 是   | 否   | 添加日期(添加文件时间到1970年1月1日的秒数值)         |
| dateModified              | number                   | 是   | 否   | 修改日期(修改文件时间到1970年1月1日的秒数值)         |
| dateTaken                 | number                   | 是   | 否   | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)         |
| artist<sup>8+</sup>       | string                   | 是   | 否   | 作者                                                   |
| audioAlbum<sup>8+</sup>   | string                   | 是   | 否   | 专辑                                                   |
| width                     | number                   | 是   | 否   | 图片宽度(单位:像素)                                 |
| height                    | number                   | 是   | 否   | 图片高度(单位:像素)                                 |
| orientation               | number                   | 是   | 是   | 图片显示方向(顺时针旋转角度,如0,90,180  单位:度) |
潘强标 已提交
1039
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)                                   |
Z
zengyawen 已提交
1040 1041 1042
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
1043 1044 1045


### isDirectory<sup>8+</sup>
P
panqiangbiao 已提交
1046

P
panqiangbiao 已提交
1047
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1048 1049 1050

判断fileAsset是否为目录,使用callback方式返回异步结果。

P
panqiangbiao 已提交
1051
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1052

P
panqiangbiao 已提交
1053
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1054

P
panqiangbiao 已提交
1055 1056
**参数:**

H
HelloCrease 已提交
1057 1058 1059
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1060 1061 1062

**示例:**

1063
```js
P
panqiangbiao 已提交
1064
async function example() {
Z
zhang-daiyue 已提交
1065
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1066 1067 1068 1069
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1070 1071
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1072 1073 1074 1075 1076 1077 1078
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
1079 1080
```

P
panqiangbiao 已提交
1081
### isDirectory<sup>8+</sup>
P
panqiangbiao 已提交
1082

P
panqiangbiao 已提交
1083
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1084 1085 1086

判断fileAsset是否为目录,使用Promise方式返回异步结果。

P
panqiangbiao 已提交
1087
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1088

P
panqiangbiao 已提交
1089
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1090

P
panqiangbiao 已提交
1091 1092
**返回值:**

H
HelloCrease 已提交
1093 1094
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1095
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1096 1097 1098

**示例:**

1099
```js
P
panqiangbiao 已提交
1100
async function example() {
Z
zhang-daiyue 已提交
1101
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1102 1103 1104 1105
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1106 1107
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory().then(function(isDirectory){
        console.info("isDirectory result:"+ isDirectory);
    }).catch(function(err){
        console.info("isDirectory failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1117 1118
```

P
panqiangbiao 已提交
1119
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1120

P
panqiangbiao 已提交
1121
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1122 1123 1124

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

P
panqiangbiao 已提交
1125
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1126

P
panqiangbiao 已提交
1127
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1128

P
panqiangbiao 已提交
1129 1130
**参数:**

H
HelloCrease 已提交
1131 1132 1133
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1134 1135 1136

**示例:**

1137
```js
P
panqiangbiao 已提交
1138
async function example() {
Z
zhang-daiyue 已提交
1139
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1140 1141 1142 1143
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1144 1145
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1146 1147 1148
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1149
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1150 1151
    asset.commitModify(() => {
        console.info('commitModify success');   
P
panqiangbiao 已提交
1152
    });
P
panqiangbiao 已提交
1153
}
P
panqiangbiao 已提交
1154 1155
```

P
panqiangbiao 已提交
1156
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1157

P
panqiangbiao 已提交
1158
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1159 1160 1161

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

P
panqiangbiao 已提交
1162
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1163

P
panqiangbiao 已提交
1164
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1165

P
panqiangbiao 已提交
1166 1167
**返回值:**

H
HelloCrease 已提交
1168 1169
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1170
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1171 1172 1173

**示例:**

1174
```js
P
panqiangbiao 已提交
1175
async function example() {
Z
zhang-daiyue 已提交
1176
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1177 1178 1179 1180
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1181 1182
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1183 1184 1185
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1186
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1187 1188
    asset.commitModify();
}
P
panqiangbiao 已提交
1189 1190
```

P
panqiangbiao 已提交
1191
### open<sup>8+</sup>
P
panqiangbiao 已提交
1192

P
panqiangbiao 已提交
1193
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1194 1195 1196

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

潘强标 已提交
1197 1198
**注意**:当前写操作是互斥的操作,写操作完成后需要调用close进行释放

Z
zhang-daiyue 已提交
1199
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1200

P
panqiangbiao 已提交
1201
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1202

P
panqiangbiao 已提交
1203 1204
**参数**

H
HelloCrease 已提交
1205 1206 1207 1208
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件句柄                            |
P
panqiangbiao 已提交
1209 1210 1211

**示例:**

1212
```js
P
panqiangbiao 已提交
1213
async function example() {
P
panqiangbiao 已提交
1214
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1215 1216
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1217
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1218 1219 1220 1221 1222 1223 1224 1225
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
P
panqiangbiao 已提交
1226 1227
```

P
panqiangbiao 已提交
1228
### open<sup>8+</sup>
P
panqiangbiao 已提交
1229

P
panqiangbiao 已提交
1230
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1231 1232 1233

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

潘强标 已提交
1234 1235
**注意**:当前写操作是互斥的操作,写操作完成后需要调用close进行释放

Z
zhang-daiyue 已提交
1236
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1237

P
panqiangbiao 已提交
1238
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1239

P
panqiangbiao 已提交
1240 1241
**参数:**

H
HelloCrease 已提交
1242 1243 1244
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
1245 1246 1247

**返回值:**

H
HelloCrease 已提交
1248 1249
| 类型                    | 说明            |
| --------------------- | ------------- |
P
panqiangbiao 已提交
1250
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
1251 1252 1253

**示例:**

1254
```js
P
panqiangbiao 已提交
1255
async function example() {
P
panqiangbiao 已提交
1256
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1257 1258
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1259
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1260 1261 1262 1263 1264 1265 1266
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
            console.info('File err!' + err);
        });
P
panqiangbiao 已提交
1267
}
P
panqiangbiao 已提交
1268 1269
```

P
panqiangbiao 已提交
1270
### close<sup>8+</sup>
P
panqiangbiao 已提交
1271

P
panqiangbiao 已提交
1272
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1273 1274 1275

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

Z
zhang-daiyue 已提交
1276
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1277

P
panqiangbiao 已提交
1278
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1279

P
panqiangbiao 已提交
1280 1281
**参数:**

H
HelloCrease 已提交
1282 1283 1284 1285
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1286 1287 1288

**示例:**

1289
```js
P
panqiangbiao 已提交
1290
async function example() {
Z
zhang-daiyue 已提交
1291
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1292 1293 1294 1295
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1296 1297
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1298 1299 1300
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1301 1302 1303 1304
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd, (closeErr) => {
            if (closeErr != undefined) {
1305
                console.info('mediaLibraryTest : close : FAIL ' + closeErr);
Z
zhang-daiyue 已提交
1306 1307 1308 1309 1310 1311 1312 1313
                console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1314 1315
    });
}
P
panqiangbiao 已提交
1316 1317
```

P
panqiangbiao 已提交
1318
### close<sup>8+</sup>
P
panqiangbiao 已提交
1319

P
panqiangbiao 已提交
1320
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1321 1322 1323

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

Z
zhang-daiyue 已提交
1324
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1325

P
panqiangbiao 已提交
1326
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1327

P
panqiangbiao 已提交
1328 1329
**参数:**

H
HelloCrease 已提交
1330 1331 1332
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1333 1334 1335

**返回值:**

H
HelloCrease 已提交
1336 1337
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1338
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1339 1340 1341

**示例:**

1342
```js
P
panqiangbiao 已提交
1343
async function example() {
Z
zhang-daiyue 已提交
1344
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1345 1346 1347 1348
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1349 1350
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1351 1352 1353
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1354 1355 1356 1357
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd).then((closeErr) => {
            if (closeErr != undefined) {
1358
                console.info('mediaLibraryTest : close : FAIL ' + closeErr);
Z
zhang-daiyue 已提交
1359
                console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
1360

Z
zhang-daiyue 已提交
1361 1362 1363 1364 1365 1366 1367
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1368 1369
    });
}
P
panqiangbiao 已提交
1370 1371
```

P
panqiangbiao 已提交
1372
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1373

P
panqiangbiao 已提交
1374
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1375 1376 1377

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

P
panqiangbiao 已提交
1378
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1379

P
panqiangbiao 已提交
1380
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1381

P
panqiangbiao 已提交
1382 1383
**参数:**

H
HelloCrease 已提交
1384 1385 1386
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1387 1388 1389

**示例:**

1390
```js
P
panqiangbiao 已提交
1391
async function example() {
Z
zhang-daiyue 已提交
1392
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1393 1394 1395 1396
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1397 1398
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1399 1400 1401 1402
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
Z
zengyawen 已提交
1403
        console.info('mediaLibraryTest : getThumbnail Successful '+ pixelmap);
P
panqiangbiao 已提交
1404 1405
    });
}
P
panqiangbiao 已提交
1406 1407
```

P
panqiangbiao 已提交
1408
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1409

P
panqiangbiao 已提交
1410
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1411 1412 1413

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

P
panqiangbiao 已提交
1414
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1415

P
panqiangbiao 已提交
1416
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1417

P
panqiangbiao 已提交
1418 1419
**参数:**

H
HelloCrease 已提交
1420 1421 1422 1423
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1424 1425 1426

**示例:**

1427
```js
P
panqiangbiao 已提交
1428
async function example() {
1429
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1430 1431 1432 1433
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1434 1435
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1436
    };
Z
zhang-daiyue 已提交
1437
    let size = { width: 720, height: 720 };
P
panqiangbiao 已提交
1438 1439 1440
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
Z
zengyawen 已提交
1441
        console.info('mediaLibraryTest : getThumbnail Successful '+ pixelmap);
P
panqiangbiao 已提交
1442 1443
    });
}
P
panqiangbiao 已提交
1444 1445
```

P
panqiangbiao 已提交
1446
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1447

P
panqiangbiao 已提交
1448
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1449 1450 1451

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

P
panqiangbiao 已提交
1452
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1453

P
panqiangbiao 已提交
1454
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1455

P
panqiangbiao 已提交
1456 1457
**参数:**

H
HelloCrease 已提交
1458 1459 1460
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1461 1462 1463

**返回值:**

H
HelloCrease 已提交
1464 1465
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1466
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1467 1468 1469

**示例:**

1470
```js
P
panqiangbiao 已提交
1471
async function example() {
1472
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1473 1474
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
潘强标 已提交
1475 1476 1477 1478
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1479
    };
Z
zhang-daiyue 已提交
1480
    let size = { width: 720, height: 720 };
P
panqiangbiao 已提交
1481 1482
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
潘强标 已提交
1483 1484
    asset.getThumbnail(size)
    .then((pixelmap) => {
Z
zengyawen 已提交
1485
        console.info('mediaLibraryTest : getThumbnail Successful '+ pixelmap);
潘强标 已提交
1486 1487 1488
    })
    .catch((err) => {
        console.info('mediaLibraryTest : getThumbnail fail'+ err);
P
panqiangbiao 已提交
1489 1490
    });
}
P
panqiangbiao 已提交
1491 1492
```

P
panqiangbiao 已提交
1493
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1494

P
panqiangbiao 已提交
1495
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1496 1497 1498

将文件设置为收藏文件,使用callback方式返回异步结果。

P
panqiangbiao 已提交
1499
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1500

P
panqiangbiao 已提交
1501
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1502

P
panqiangbiao 已提交
1503 1504
**参数:**

H
HelloCrease 已提交
1505 1506 1507 1508
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1509 1510 1511

**示例:**

1512
```js
P
panqiangbiao 已提交
1513
async function example() {
1514
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1515 1516 1517 1518
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1519 1520
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1521 1522 1523 1524 1525 1526 1527
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
1528 1529
```

P
panqiangbiao 已提交
1530
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1531

P
panqiangbiao 已提交
1532
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1533 1534 1535

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

P
panqiangbiao 已提交
1536
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1537

P
panqiangbiao 已提交
1538
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1539

P
panqiangbiao 已提交
1540 1541
**参数:**

H
HelloCrease 已提交
1542 1543 1544
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1545 1546 1547

**返回值:**

H
HelloCrease 已提交
1548 1549
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1550
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1551 1552 1553

**示例:**

1554
```js
P
panqiangbiao 已提交
1555
async function example() {
1556
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1557 1558 1559 1560
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1561 1562
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true).then(function() {
        console.info("favorite successfully");
    }).catch(function(err){
        console.info("favorite failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1572 1573
```

P
panqiangbiao 已提交
1574
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1575

P
panqiangbiao 已提交
1576
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1577 1578 1579

判断该文件是否为收藏文件,使用callback方式返回异步结果。

P
panqiangbiao 已提交
1580
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1581

P
panqiangbiao 已提交
1582
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1583

P
panqiangbiao 已提交
1584 1585
**参数:**

H
HelloCrease 已提交
1586 1587 1588
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1589 1590 1591

**示例:**

1592
```js
P
panqiangbiao 已提交
1593
async function example() {
1594
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1595 1596 1597 1598
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1599 1600
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite((err, isFavorite) => {
        if (isFavorite) {
            console.info('FileAsset is favorite');
        }else{
            console.info('FileAsset is not favorite');
        }
    });
}
P
panqiangbiao 已提交
1612 1613
```

P
panqiangbiao 已提交
1614
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1615

P
panqiangbiao 已提交
1616
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1617 1618 1619

判断该文件是否为收藏文件,使用promise方式返回异步结果。

P
panqiangbiao 已提交
1620
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1621

P
panqiangbiao 已提交
1622
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1623

P
panqiangbiao 已提交
1624 1625
**返回值:**

H
HelloCrease 已提交
1626 1627
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1628
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1629 1630 1631

**示例:**

1632
```js
P
panqiangbiao 已提交
1633
async function example() {
1634
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1635 1636 1637 1638
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1639 1640
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite().then(function(isFavorite){
        console.info("isFavorite result:"+ isFavorite);
    }).catch(function(err){
        console.info("isFavorite failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1650 1651
```

P
panqiangbiao 已提交
1652
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1653

A
AOL 已提交
1654
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1655 1656 1657

当文件被定位时,将文件放到垃圾文件夹,使用callback方式返回异步结果。

P
panqiangbiao 已提交
1658 1659
放入垃圾文件夹的文件不会被真正删除,可以通过isTrash = false参数恢复成正常文件。

P
panqiangbiao 已提交
1660
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1661

P
panqiangbiao 已提交
1662
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1663

P
panqiangbiao 已提交
1664 1665
**参数:**

H
HelloCrease 已提交
1666 1667 1668 1669
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1670 1671 1672

**示例:**

1673
```js
P
panqiangbiao 已提交
1674
async function example() {
1675
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1676 1677 1678 1679
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1680 1681
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1682 1683 1684 1685 1686
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1687
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1688
    }
P
panqiangbiao 已提交
1689
}
P
panqiangbiao 已提交
1690 1691
```

P
panqiangbiao 已提交
1692
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1693

A
AOL 已提交
1694
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1695 1696 1697

当文件被定位时,将文件放到垃圾文件夹,使用promise方式返回异步结果。

P
panqiangbiao 已提交
1698 1699
放入垃圾文件夹的文件不会被真正删除,可以通过isTrash = false参数恢复成正常文件。

P
panqiangbiao 已提交
1700
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1701

P
panqiangbiao 已提交
1702
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1703

P
panqiangbiao 已提交
1704 1705
**参数:**

H
HelloCrease 已提交
1706 1707 1708
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1709 1710 1711

**返回值:**

H
HelloCrease 已提交
1712 1713
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1714
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1715 1716 1717

**示例:**

1718
```js
P
panqiangbiao 已提交
1719
async function example() {
1720
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1721 1722 1723 1724
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1725 1726
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1727 1728 1729 1730 1731 1732 1733 1734 1735
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true).then(function() {
        console.info("trash successfully");
    }).catch(function(err){
        console.info("trash failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1736 1737
```

P
panqiangbiao 已提交
1738
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1739

P
panqiangbiao 已提交
1740
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1741 1742 1743

当文件被定位,判断文件是否为垃圾文件,使用callback方式返回异步结果。

P
panqiangbiao 已提交
1744
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1745

P
panqiangbiao 已提交
1746
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1747

P
panqiangbiao 已提交
1748 1749
**参数:**

H
HelloCrease 已提交
1750 1751 1752
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1753 1754 1755

**示例:**

1756
```js
P
panqiangbiao 已提交
1757
async function example() {
1758
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1759 1760 1761 1762
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1763 1764
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1765 1766 1767
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1768 1769 1770 1771 1772 1773 1774
    asset.isTrash((err, isTrash) => {
      if (isTrash == undefined) {
        console.error('Failed to get trash state: ' + err);
        return;
      }
      console.info('Get trash state success: ' + isTrash);
    });
P
panqiangbiao 已提交
1775
}
P
panqiangbiao 已提交
1776 1777
```

P
panqiangbiao 已提交
1778
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1779

P
panqiangbiao 已提交
1780
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1781

P
panqiangbiao 已提交
1782
当文件被定位,判断文件是否为垃圾文件,使用promise方式返回异步结果。
P
panqiangbiao 已提交
1783

P
panqiangbiao 已提交
1784
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1785

P
panqiangbiao 已提交
1786
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1787

P
panqiangbiao 已提交
1788 1789
**返回值:**

H
HelloCrease 已提交
1790 1791
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1792
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1793 1794 1795

**示例:**

1796
```js
P
panqiangbiao 已提交
1797
async function example() {
1798
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1799 1800 1801 1802
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1803
      order: fileKeyObj.DATE_ADDED + " DESC",
P
panqiangbiao 已提交
1804 1805 1806 1807
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash().then(function(isTrash){
Z
zhang-daiyue 已提交
1808
      console.info("isTrash result: " + isTrash);
P
panqiangbiao 已提交
1809
    }).catch(function(err){
Z
zhang-daiyue 已提交
1810
      console.error("isTrash failed with error: " + err);
P
panqiangbiao 已提交
1811 1812
    });
}
P
panqiangbiao 已提交
1813 1814
```

Z
zengyawen 已提交
1815
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1816 1817 1818

文件检索结果集。

Z
zengyawen 已提交
1819
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1820

P
panqiangbiao 已提交
1821
getCount(): number
P
panqiangbiao 已提交
1822 1823 1824

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

P
panqiangbiao 已提交
1825
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1826

P
panqiangbiao 已提交
1827 1828
**返回值**

H
HelloCrease 已提交
1829 1830
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1831
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1832 1833 1834

**示例**

1835
```js
P
panqiangbiao 已提交
1836
async function example() {
1837
    let fileKeyObj = mediaLibrary.FileKey;
Z
zhang-daiyue 已提交
1838
    let fileType = mediaLibrary.MediaType.FILE;
P
panqiangbiao 已提交
1839 1840 1841
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1842 1843
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1844 1845 1846 1847
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1848 1849
```

Z
zengyawen 已提交
1850
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1851

P
panqiangbiao 已提交
1852
isAfterLast(): boolean
P
panqiangbiao 已提交
1853 1854 1855

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

P
panqiangbiao 已提交
1856
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1857

P
panqiangbiao 已提交
1858 1859
**返回值**

H
HelloCrease 已提交
1860 1861
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1862
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1863 1864 1865

**示例**

1866
```js
P
panqiangbiao 已提交
1867
async function example() {
1868
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1869 1870 1871 1872
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1873 1874
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1875 1876 1877
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1878
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1879 1880 1881 1882
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1883
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1884
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1885 1886
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1887
              fetchFileResult.close();
P
panqiangbiao 已提交
1888

P
panqiangbiao 已提交
1889 1890
            }
    }
P
panqiangbiao 已提交
1891
}
P
panqiangbiao 已提交
1892 1893
```

Z
zengyawen 已提交
1894
### close<sup>7+</sup>
P
panqiangbiao 已提交
1895

P
panqiangbiao 已提交
1896
close(): void
P
panqiangbiao 已提交
1897 1898 1899

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

P
panqiangbiao 已提交
1900
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1901

P
panqiangbiao 已提交
1902 1903
**示例**

1904
```js
P
panqiangbiao 已提交
1905
async function example() {
1906
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1907 1908 1909 1910
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1911 1912
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1913 1914 1915 1916
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1917 1918
```

Z
zengyawen 已提交
1919
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1920

P
panqiangbiao 已提交
1921
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1922 1923 1924

获取文件检索结果中的第一个文件资产。此方法使用回调返回FileAsset。

P
panqiangbiao 已提交
1925
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1926

P
panqiangbiao 已提交
1927 1928
**参数**

Z
zengyawen 已提交
1929 1930 1931
| 参数名   | 类型                                          | 必填 | 说明                                        |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步获取结果集中第一个FileAsset完成后的回调 |
P
panqiangbiao 已提交
1932 1933 1934

**示例**

1935
```js
P
panqiangbiao 已提交
1936
async function example() {
1937
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1938 1939 1940 1941
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1942 1943
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1944 1945
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
1946
    fetchFileResult.getFirstObject((err, fileAsset) => {
P
panqiangbiao 已提交
1947 1948 1949 1950
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
1951
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
1952 1953
    })
}
P
panqiangbiao 已提交
1954 1955
```

Z
zengyawen 已提交
1956
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1957

P
panqiangbiao 已提交
1958
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1959

Z
zengyawen 已提交
1960
获取文件检索结果中的第一个文件资产。此方法使用Promise方式返回FileAsset。
P
panqiangbiao 已提交
1961

P
panqiangbiao 已提交
1962
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1963

P
panqiangbiao 已提交
1964 1965
**返回值**

Z
zengyawen 已提交
1966 1967
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1968
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1969 1970 1971

**示例**

1972
```js
P
panqiangbiao 已提交
1973
async function example() {
1974
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1975 1976 1977 1978
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1979 1980
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1981 1982 1983 1984 1985 1986 1987 1988
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getFirstObject().then(function(fileAsset){
        console.info("getFirstObject successfully:"+ JSON.stringify(fileAsset));
    }).catch(function(err){
        console.info("getFirstObject failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1989 1990
```

Z
zengyawen 已提交
1991
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1992

P
panqiangbiao 已提交
1993
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1994 1995 1996

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

P
panqiangbiao 已提交
1997
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1998

P
panqiangbiao 已提交
1999 2000
**参数**

Z
zengyawen 已提交
2001 2002 2003
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
2004 2005 2006

**示例**

2007
```js
P
panqiangbiao 已提交
2008
async function example() {
2009
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2010 2011 2012 2013
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2014 2015
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2016 2017
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2018
    fetchFileResult.getNextObject((err, fileAsset) => {
P
panqiangbiao 已提交
2019 2020 2021 2022
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2023
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2024 2025
    })
}
P
panqiangbiao 已提交
2026 2027
```

Z
zengyawen 已提交
2028
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2029

P
panqiangbiao 已提交
2030
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2031 2032 2033

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

P
panqiangbiao 已提交
2034
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2035

P
panqiangbiao 已提交
2036 2037
**返回值**

Z
zengyawen 已提交
2038 2039 2040
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2041 2042 2043

**示例**

2044
```js
P
panqiangbiao 已提交
2045
async function example() {
2046
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2047 2048 2049 2050
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2051 2052
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2053 2054 2055
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
2056
    console.info('mediaLibraryTest : count:' + fetchCount);
Z
zhang-daiyue 已提交
2057
    let fileAsset = await fetchFileResult.getNextObject();
P
panqiangbiao 已提交
2058
}
P
panqiangbiao 已提交
2059 2060
```

Z
zengyawen 已提交
2061
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2062

P
panqiangbiao 已提交
2063
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2064 2065 2066

获取文件检索结果中的最后一个文件资产。此方法使用callback回调来返回FileAsset。

P
panqiangbiao 已提交
2067
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2068

P
panqiangbiao 已提交
2069 2070
**参数**

Z
zengyawen 已提交
2071 2072
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
Z
zengyawen 已提交
2073
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2074 2075 2076

**示例**

2077
```js
P
panqiangbiao 已提交
2078
async function example() {
2079
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2080 2081 2082 2083
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2084 2085
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2086 2087
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2088
    fetchFileResult.getLastObject((err, fileAsset) => {
P
panqiangbiao 已提交
2089 2090 2091 2092
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2093
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2094 2095
    })
}
P
panqiangbiao 已提交
2096 2097
```

Z
zengyawen 已提交
2098
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2099

P
panqiangbiao 已提交
2100
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2101 2102 2103

获取文件检索结果中的最后一个文件资产。此方法使用Promise方式来返回FileAsset。

P
panqiangbiao 已提交
2104
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2105

P
panqiangbiao 已提交
2106 2107
**返回值**

Z
zengyawen 已提交
2108 2109 2110
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2111 2112 2113

**示例**

2114
```js
P
panqiangbiao 已提交
2115
async function example() {
2116
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2117 2118 2119 2120
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2121 2122
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2123 2124 2125 2126
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
2127 2128
```

Z
zengyawen 已提交
2129
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2130

P
panqiangbiao 已提交
2131
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2132 2133 2134

获取文件检索结果中具有指定索引的文件资产。此方法使用回调来返回FileAsset。

P
panqiangbiao 已提交
2135
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2136

P
panqiangbiao 已提交
2137 2138
**参数**

Z
zengyawen 已提交
2139
| 参数名       | 类型                                       | 必填   | 说明                 |
H
HelloCrease 已提交
2140 2141
| -------- | ---------------------------------------- | ---- | ------------------ |
| index    | number                                   | 是    | 要获取的文件的索引,从0开始     |
Z
zengyawen 已提交
2142
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是    | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2143 2144 2145

**示例**

2146
```js
P
panqiangbiao 已提交
2147
async function example() {
2148
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2149 2150 2151 2152
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2153 2154
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2155 2156
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2157
    fetchFileResult.getPositionObject(0, (err, fileAsset) => {
P
panqiangbiao 已提交
2158 2159 2160 2161
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2162
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2163 2164
    })
}
P
panqiangbiao 已提交
2165 2166
```

Z
zengyawen 已提交
2167
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2168

P
panqiangbiao 已提交
2169
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2170 2171 2172

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

P
panqiangbiao 已提交
2173
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2174

P
panqiangbiao 已提交
2175 2176
**参数**

Z
zengyawen 已提交
2177
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
2178 2179
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
2180 2181 2182

**返回值**

Z
zengyawen 已提交
2183 2184 2185
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2186 2187 2188

**示例**

2189
```js
P
panqiangbiao 已提交
2190
async function example() {
2191
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2192 2193 2194 2195
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2196 2197
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2198 2199
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2200
    fetchFileResult.getPositionObject(1) .then(function (fileAsset){
2201
        console.log('fileAsset.displayName : ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
2202
    }).catch(function (err) {
2203
        console.info("getFileAssets failed with error:" + err);
Z
zhang-daiyue 已提交
2204
    });
P
panqiangbiao 已提交
2205
}
P
panqiangbiao 已提交
2206 2207
```

Z
zengyawen 已提交
2208
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2209

P
panqiangbiao 已提交
2210
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2211 2212 2213

获取文件检索结果中的所有文件资产。此方法使用Callback回调来返回FileAsset结果集。

P
panqiangbiao 已提交
2214
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2215

P
panqiangbiao 已提交
2216 2217
**参数**

Z
zengyawen 已提交
2218
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
2219
| -------- | ---------------------------------------- | ---- | -------------------- |
Z
zengyawen 已提交
2220
| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
2221 2222 2223

**示例**

2224
```js
P
panqiangbiao 已提交
2225
async function example() {
2226
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2227 2228 2229 2230
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2231 2232
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2233 2234
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2235
    fetchFileResult.getAllObject((err, fileAsset) => {
2236
        if (err) {
P
panqiangbiao 已提交
2237 2238
           console.error('Failed ');
           return;
2239 2240 2241 2242
        }
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
            console.log('fileAsset.displayName : ' + fileAsset[i].displayName);
        } 
P
panqiangbiao 已提交
2243 2244
    })
}
P
panqiangbiao 已提交
2245 2246
```

Z
zengyawen 已提交
2247
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2248

P
panqiangbiao 已提交
2249
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2250 2251 2252

获取文件检索结果中的所有文件资产。此方法使用Promise来返回FileAsset结果集。

P
panqiangbiao 已提交
2253
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2254

P
panqiangbiao 已提交
2255 2256
**返回值**

Z
zengyawen 已提交
2257 2258 2259
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2260 2261 2262

**示例**

2263
```js
P
panqiangbiao 已提交
2264
async function example() {
2265
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2266 2267 2268 2269
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2270 2271
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2272 2273 2274 2275
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
P
panqiangbiao 已提交
2276 2277
```

Z
zengyawen 已提交
2278
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2279 2280 2281

实体相册

Z
zengyawen 已提交
2282
### 属性
P
panqiangbiao 已提交
2283

Z
zengyawen 已提交
2284 2285
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

Z
zengyawen 已提交
2286
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2287
| ------------ | ------ | ---- | ---- | ------- |
Z
zengyawen 已提交
2288 2289 2290
| albumId | number | 是    | 否    | 相册编号    |
| albumName | string | 是    | 是    | 相册名称    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri   |
H
HelloCrease 已提交
2291
| dateModified | number | 是    | 否    | 修改日期    |
Z
zengyawen 已提交
2292 2293 2294
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
2295

P
panqiangbiao 已提交
2296 2297 2298
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2299 2300 2301

更新相册属性修改到数据库中。

P
panqiangbiao 已提交
2302
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
2303

P
panqiangbiao 已提交
2304
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2305

P
panqiangbiao 已提交
2306 2307
**参数**

Z
zengyawen 已提交
2308 2309 2310
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2311 2312 2313

**示例**

2314
```js
P
panqiangbiao 已提交
2315
async function example() {
P
panqiangbiao 已提交
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify((err) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log('Modify successful.');
    })
}
P
panqiangbiao 已提交
2331 2332
```

P
panqiangbiao 已提交
2333
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2334

P
panqiangbiao 已提交
2335
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2336 2337 2338

更新相册属性修改到数据库中。

P
panqiangbiao 已提交
2339
**需要权限**:ohos.permission.READ_MEDIA, ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
2340

P
panqiangbiao 已提交
2341
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2342

P
panqiangbiao 已提交
2343 2344
**返回值**

H
HelloCrease 已提交
2345 2346
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2347 2348 2349 2350
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2351
```js
P
panqiangbiao 已提交
2352
async function example() {
P
panqiangbiao 已提交
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify().then(function() {
        console.info("commitModify successfully");
    }).catch(function(err){
        console.info("commitModify failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2366 2367
```

Z
zengyawen 已提交
2368
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2369

P
panqiangbiao 已提交
2370
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2371 2372 2373

按照检索条件获取相册中的文件。此方法使用Callback回调来返回文件结果集。

P
panqiangbiao 已提交
2374
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2375

P
panqiangbiao 已提交
2376
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2377

P
panqiangbiao 已提交
2378 2379
**参数**

Z
zengyawen 已提交
2380
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2381
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2382 2383
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2384 2385 2386

**示例**

2387
```js
P
panqiangbiao 已提交
2388
async function example() {
P
panqiangbiao 已提交
2389 2390 2391 2392
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2393 2394 2395 2396
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
P
panqiangbiao 已提交
2397 2398 2399 2400 2401 2402
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
P
panqiangbiao 已提交
2403 2404 2405
}
```

Z
zengyawen 已提交
2406
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2407

P
panqiangbiao 已提交
2408
 getFileAssets(options?: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
2409

Z
zengyawen 已提交
2410
按照检索条件获取相册中的文件。此方法使用异步Promise来返回文件结果集。
P
panqiangbiao 已提交
2411

P
panqiangbiao 已提交
2412
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2413

P
panqiangbiao 已提交
2414
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2415 2416 2417

**参数**

Z
zengyawen 已提交
2418
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2419
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2420
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2421 2422 2423

**返回值**

Z
zengyawen 已提交
2424 2425
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2426
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2427 2428 2429

**示例**

2430
```js
P
panqiangbiao 已提交
2431
async function example() {
P
panqiangbiao 已提交
2432 2433 2434 2435
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2436 2437 2438
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
2439
    };
P
panqiangbiao 已提交
2440 2441 2442 2443 2444 2445 2446 2447
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
        console.info("getFileAssets successfully:"+ JSON.stringify(albumFetchFileResult));
    }).catch(function(err){
        console.info("getFileAssets failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2448 2449
```

P
panqiangbiao 已提交
2450
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2451

P
panqiangbiao 已提交
2452
注册设备的信息。
2453 2454

**系统接口**:此接口为系统接口。
P
panqiangbiao 已提交
2455

Z
zhang-daiyue 已提交
2456
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.DistributedCore
Z
zengyawen 已提交
2457

Z
zengyawen 已提交
2458 2459 2460 2461 2462 2463
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2464 2465 2466



Z
zengyawen 已提交
2467
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2468 2469 2470

枚举,媒体类型。

Z
zengyawen 已提交
2471 2472
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

潘强标 已提交
2473 2474 2475 2476 2477 2478
| 名称  |  说明 |
| ----- |  ---- |
| FILE  |  文件 |
| IMAGE |  图片 |
| VIDEO |  视频 |
| AUDIO |  音频 |
P
panqiangbiao 已提交
2479

Z
zengyawen 已提交
2480
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2481 2482 2483

枚举,文件关键信息。

Z
zengyawen 已提交
2484 2485
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

Z
zengyawen 已提交
2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
| 名称          | 默认值              | 说明                                                       |
| ------------- | ------------------- | ---------------------------------------------------------- |
| ID            | file_id             | 文件编号                                                   |
| RELATIVE_PATH | relative_path       | 相对公共目录路径                                           |
| DISPLAY_NAME  | display_name        | 显示名字                                                   |
| PARENT        | parent              | 父目录id                                                   |
| MIME_TYPE     | mime_type           | 文件扩展属性                                               |
| MEDIA_TYPE    | media_type          | 媒体类型                                                   |
| SIZE          | size                | 文件大小(单位:字节)                                     |
| DATE_ADDED    | date_added          | 添加日期(添加文件时间到1970年1月1日的秒数值)             |
| DATE_MODIFIED | date_modified       | 修改日期(修改文件时间到1970年1月1日的秒数值)             |
| DATE_TAKEN    | date_taken          | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)             |
| TITLE         | title               | 文件标题                                                   |
| ARTIST        | artist              | 作者                                                       |
| AUDIOALBUM    | audio_album         | 专辑                                                       |
Z
zhang-daiyue 已提交
2501
| DURATION      | duration            | 持续时间(单位:毫秒)                                       |
Z
zengyawen 已提交
2502 2503
| WIDTH         | width               | 图片宽度(单位:像素)                                     |
| HEIGHT        | height              | 图片高度(单位:像素)                                     |
P
panqiangbiao 已提交
2504
| ORIENTATION   | orientation         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
Z
zengyawen 已提交
2505 2506
| ALBUM_ID      | bucket_id           | 文件所归属的相册编号                                       |
| ALBUM_NAME    | bucket_display_name | 文件所归属相册名称                                         |
P
panqiangbiao 已提交
2507

Z
zengyawen 已提交
2508
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2509 2510 2511

枚举,目录类型。

Z
zengyawen 已提交
2512 2513
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

潘强标 已提交
2514 2515 2516 2517 2518 2519 2520 2521
| 名称          |  说明               |
| ------------- |  ------------------ |
| DIR_CAMERA    |  表示Camera文件路径 |
| DIR_VIDEO     |  表示视频路径       |
| DIR_IMAGE     |  表示图片路径       |
| DIR_AUDIO     |  表示音频路径       |
| DIR_DOCUMENTS |  表示文档路径       |
| DIR_DOWNLOAD  |  表示下载路径       |
P
panqiangbiao 已提交
2522

Z
zengyawen 已提交
2523
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2524 2525

枚举,设备类型。
2526 2527

**系统接口**:此接口为系统接口。
P
panqiangbiao 已提交
2528

Z
zhang-daiyue 已提交
2529
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.DistributedCore
Z
zengyawen 已提交
2530

潘强标 已提交
2531 2532 2533 2534 2535 2536 2537 2538 2539
| 名称         |  说明       |
| ------------ |  ---------- |
| TYPE_UNKNOWN |  未识别设备 |
| TYPE_LAPTOP  |  笔记本电脑 |
| TYPE_PHONE   |  手机       |
| TYPE_TABLET  |  平板电脑   |
| TYPE_WATCH   |  智能手表   |
| TYPE_CAR     |  车载设备   |
| TYPE_TV      |  电视设备   |
P
panqiangbiao 已提交
2540

Z
zengyawen 已提交
2541
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2542 2543 2544

检索条件。

Z
zengyawen 已提交
2545 2546
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

Z
zengyawen 已提交
2547 2548
| 名称                    | 类型                | 可读 | 可写 | 必填 | 说明                                                         |
| ----------------------- | ------------------- | ---- | ---- | ---- | ------------------------------------------------------------ |
2549
| selections              | string              | 是   | 是   | 是   | 检索条件,使用[FileKey](#filekey8)中的枚举值作为检索条件的列名。示例:<br />selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR ' +mediaLibrary.FileKey.MEDIA_TYPE + '= ?', |
Z
zengyawen 已提交
2550
| selectionArgs           | Array&lt;string&gt; | 是   | 是   | 是   | 检索条件的值,对应selections中检索条件列的值。<br />示例:<br />selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
Z
zhang-daiyue 已提交
2551
| order                   | string              | 是   | 是   | 否   | 检索结果排序方式,使用[FileKey](#filekey8)中的枚举值作为检索结果排序的列,可以用升序或降序排列。示例:<br />升序排列:order: mediaLibrary.FileKey.DATE_ADDED + " ASC"<br />降序排列:order: mediaLibrary.FileKey.DATE_ADDED + " DESC" |
Z
zengyawen 已提交
2552 2553 2554
| uri<sup>8+</sup>        | string              | 是   | 是   | 否   | 文件URI                                                      |
| networkId<sup>8+</sup>  | string              | 是   | 是   | 否   | 注册设备网络ID                                               |
| extendArgs<sup>8+</sup> | string              | 是   | 是   | 否   | 扩展的检索参数,目前没有扩展检索参数                         |
P
panqiangbiao 已提交
2555

P
panqiangbiao 已提交
2556
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2557 2558

图片尺寸。
2559 2560

**系统能力:**  以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2561

H
HelloCrease 已提交
2562 2563 2564 2565
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2566

Z
update  
zengyawen 已提交
2567
## MediaAssetOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2568 2569 2570

媒体资源选项。

Z
update  
zengyawen 已提交
2571
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2572 2573 2574 2575

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core


Z
zengyawen 已提交
2576 2577 2578 2579 2580
| 名称         | 类型   | 必填 | 描述                                                         |
| ------------ | ------ | ---- | ------------------------------------------------------------ |
| src          | string | 是   | 本地文件应用沙箱路径。                                       |
| mimeType     | string | 是   | 媒体MIME(Multipurpose&nbsp;Internet&nbsp;Mail&nbsp;Extensions)类型。<br/>包括:'image/\*'、'video/\*'、'audio/\*'、 'file\*'。 |
| relativePath | string | 否   | 自定义媒体资源保存位置,例:Pictures/ 不填则保存到默认路径。 <br/> image类型默认路径Pictures/ <br/> video类型默认路径Videos/ <br/> audio类型默认路径Audios/ <br/> file类型默认路径Documents/ 。 |
P
panqiangbiao 已提交
2581

Z
update  
zengyawen 已提交
2582
## MediaSelectOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2583 2584 2585

媒体资源类型选项。

Z
update  
zengyawen 已提交
2586
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2587 2588 2589

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

H
HelloCrease 已提交
2590 2591
| 名称    | 类型     | 必填   | 描述                   |
| ----- | ------ | ---- | -------------------- |
潘强标 已提交
2592
| type  | string | 是    | 媒体类型,包括:image, video, media,当前仅支持media类型 |
Z
zhang-daiyue 已提交
2593
| count | number | 是    | 媒体选择,count = 1表示单选,count大于1表示多选。            |
Z
zhang-daiyue 已提交
2594

H
huweiqi 已提交
2595