js-apis-medialibrary.md 91.1 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
303 304 305 306 307 308
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
media.getPublicDirectory(DIR_CAMERA).then(function(dicResult){
    console.info("getPublicDirectory successfully:"+ JSON.stringify(dicResult));
}).catch(function(err){
    console.info("getPublicDirectory failed with error:"+ err);
});
P
panqiangbiao 已提交
309 310
```

Z
zengyawen 已提交
311 312 313 314 315 316 317 318
### deleteAsset<sup>8+</sup>

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

删除媒体文件资源

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

319
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

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

**参数:**

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

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

**示例:**

```js
async function example() {
338 339
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    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

删除媒体文件资源

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

367
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381

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

**参数:**

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

**示例:**

```js
async function example() {
382 383
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    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 已提交
406
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
407

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

P
panqiangbiao 已提交
410
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
411 412 413 414 415

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

**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
434
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
435

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

P
panqiangbiao 已提交
438
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
439 440 441 442 443

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

**参数:**

Z
zengyawen 已提交
444 445 446
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
447 448 449

**返回值:**

Z
zengyawen 已提交
450 451 452
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
453 454 455

**示例:**

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

Z
zengyawen 已提交
468
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
469

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

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

P
panqiangbiao 已提交
474
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
475

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

P
panqiangbiao 已提交
478 479
**参数**

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

**示例:**

487
```js
P
panqiangbiao 已提交
488 489 490 491
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
492
media.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
493 494 495 496 497 498 499
    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 已提交
500
})
P
panqiangbiao 已提交
501 502
```

Z
zengyawen 已提交
503
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
504

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

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

P
panqiangbiao 已提交
509
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
510

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

P
panqiangbiao 已提交
513 514
**参数:**

Z
zengyawen 已提交
515 516 517
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
518 519 520

**返回值:**

Z
zengyawen 已提交
521 522 523
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
524 525 526

**示例:**

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

P
panqiangbiao 已提交
539
### release<sup>8+</sup>
P
panqiangbiao 已提交
540

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

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

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

P
panqiangbiao 已提交
548 549
**参数:**

H
HelloCrease 已提交
550 551 552
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调表示成功还是失败 |
P
panqiangbiao 已提交
553 554 555

**示例:**

556
```js
P
panqiangbiao 已提交
557
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
558
media.release((err) => {
P
panqiangbiao 已提交
559
    // do something
P
panqiangbiao 已提交
560
});
P
panqiangbiao 已提交
561 562
```

P
panqiangbiao 已提交
563
### release<sup>8+</sup>
P
panqiangbiao 已提交
564

P
panqiangbiao 已提交
565
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
566

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

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

P
panqiangbiao 已提交
572 573
**返回值:**

H
HelloCrease 已提交
574 575
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
576
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
577 578 579

**示例:**

580
```js
P
panqiangbiao 已提交
581
media.release()
P
panqiangbiao 已提交
582 583
```

Z
update  
zengyawen 已提交
584
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
585 586 587 588 589

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

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

Z
update  
zengyawen 已提交
590
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
591 592 593 594 595

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

**参数:**

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

**示例:**

603
```js
P
panqiangbiao 已提交
604
let option = {
潘强标 已提交
605 606 607
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
608 609 610 611 612 613 614 615 616
};
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.
});
617
```
P
panqiangbiao 已提交
618 619


Z
update  
zengyawen 已提交
620
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
621 622 623 624 625

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

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

Z
update  
zengyawen 已提交
626
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
627 628 629 630 631

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

**参数:**

H
HelloCrease 已提交
632 633 634
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
635 636 637

**返回值:**

H
HelloCrease 已提交
638 639
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
640 641 642 643
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

644
```js
P
panqiangbiao 已提交
645
let option = {
潘强标 已提交
646 647 648
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
649 650 651 652 653 654 655
};
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.");
});
656
```
P
panqiangbiao 已提交
657 658


Z
update  
zengyawen 已提交
659
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
660 661 662

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

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

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

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

**参数:**

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

**示例:**

679
```js
P
panqiangbiao 已提交
680
let images = [
Z
zhang-daiyue 已提交
681 682
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
683
];
H
HelloCrease 已提交
684
/* 网络图片使用方式
P
panqiangbiao 已提交
685 686 687 688
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
689
*/
P
panqiangbiao 已提交
690 691 692 693 694 695 696 697
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.");
});
698
```
P
panqiangbiao 已提交
699 700


Z
update  
zengyawen 已提交
701
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
702 703 704

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

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

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

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

**参数:**

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

**示例:**

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


Z
update  
zengyawen 已提交
741
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
742 743 744

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

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

766
```js
P
panqiangbiao 已提交
767
let images = [
Z
zhang-daiyue 已提交
768 769
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
770
];
H
HelloCrease 已提交
771
/* 网络图片使用方式
P
panqiangbiao 已提交
772 773 774 775
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
776
*/
P
panqiangbiao 已提交
777 778 779 780 781 782
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.");
});
783
```
P
panqiangbiao 已提交
784 785


Z
update  
zengyawen 已提交
786
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
787 788 789 790 791

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

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

792
> **说明**: <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 已提交
793 794 795 796 797

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

**参数:**

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

**示例:**

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


Z
update  
zengyawen 已提交
821
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
822 823 824 825 826

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

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

827
> **说明**: <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 已提交
828 829 830 831 832

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

**参数:**

H
HelloCrease 已提交
833 834
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
Z
zengyawen 已提交
835
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
836 837 838

**返回值:**

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

**示例:**

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

Z
zengyawen 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
```
### 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>
897

Z
zengyawen 已提交
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
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>
972

Z
zengyawen 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
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)
        }
    })
}
1005
```
P
panqiangbiao 已提交
1006

Z
zengyawen 已提交
1007
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
1008 1009 1010

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

Z
zengyawen 已提交
1011 1012 1013
### 属性

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

Z
zengyawen 已提交
1015 1016 1017
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| id                        | number                   | 是   | 否   | 文件资源编号                                           |
Z
zhang-daiyue 已提交
1018
| uri                       | string                   | 是   | 否   | 文件资源uri(如:datashare:///media/image/2)         |
Z
zengyawen 已提交
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
| 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  单位:度) |
潘强标 已提交
1034
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)                                   |
Z
zengyawen 已提交
1035 1036 1037
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
1038 1039 1040


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

P
panqiangbiao 已提交
1042
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1043 1044 1045

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

P
panqiangbiao 已提交
1046
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1047

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

P
panqiangbiao 已提交
1050 1051
**参数:**

H
HelloCrease 已提交
1052 1053 1054
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1055 1056 1057

**示例:**

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

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

P
panqiangbiao 已提交
1078
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1079 1080 1081

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

P
panqiangbiao 已提交
1082
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1083

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

P
panqiangbiao 已提交
1086 1087
**返回值:**

H
HelloCrease 已提交
1088 1089
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1090
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1091 1092 1093

**示例:**

1094
```js
P
panqiangbiao 已提交
1095
async function example() {
Z
zhang-daiyue 已提交
1096
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1097 1098 1099 1100
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1101 1102
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1103 1104 1105 1106 1107 1108 1109 1110 1111
    };
    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 已提交
1112 1113
```

P
panqiangbiao 已提交
1114
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1115

P
panqiangbiao 已提交
1116
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1117 1118 1119

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

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

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

P
panqiangbiao 已提交
1124 1125
**参数:**

H
HelloCrease 已提交
1126 1127 1128
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1129 1130 1131

**示例:**

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

P
panqiangbiao 已提交
1151
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1152

P
panqiangbiao 已提交
1153
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1154 1155 1156

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

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

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

P
panqiangbiao 已提交
1161 1162
**返回值:**

H
HelloCrease 已提交
1163 1164
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1165
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1166 1167 1168

**示例:**

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

P
panqiangbiao 已提交
1186
### open<sup>8+</sup>
P
panqiangbiao 已提交
1187

P
panqiangbiao 已提交
1188
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1189 1190 1191

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

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

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

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

P
panqiangbiao 已提交
1198 1199
**参数**

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

**示例:**

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

P
panqiangbiao 已提交
1223
### open<sup>8+</sup>
P
panqiangbiao 已提交
1224

P
panqiangbiao 已提交
1225
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1226 1227 1228

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

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

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

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

P
panqiangbiao 已提交
1235 1236
**参数:**

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

**返回值:**

H
HelloCrease 已提交
1243 1244
| 类型                    | 说明            |
| --------------------- | ------------- |
P
panqiangbiao 已提交
1245
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
1246 1247 1248

**示例:**

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

P
panqiangbiao 已提交
1265
### close<sup>8+</sup>
P
panqiangbiao 已提交
1266

P
panqiangbiao 已提交
1267
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1268 1269 1270

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

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

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

P
panqiangbiao 已提交
1275 1276
**参数:**

H
HelloCrease 已提交
1277 1278 1279 1280
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1281 1282 1283

**示例:**

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

P
panqiangbiao 已提交
1313
### close<sup>8+</sup>
P
panqiangbiao 已提交
1314

P
panqiangbiao 已提交
1315
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1316 1317 1318

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

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

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

P
panqiangbiao 已提交
1323 1324
**参数:**

H
HelloCrease 已提交
1325 1326 1327
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1328 1329 1330

**返回值:**

H
HelloCrease 已提交
1331 1332
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1333
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1334 1335 1336

**示例:**

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

Z
zhang-daiyue 已提交
1356 1357 1358 1359 1360 1361 1362
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1363 1364
    });
}
P
panqiangbiao 已提交
1365 1366
```

P
panqiangbiao 已提交
1367
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1368

P
panqiangbiao 已提交
1369
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1370 1371 1372

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

P
panqiangbiao 已提交
1373
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1374

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

P
panqiangbiao 已提交
1377 1378
**参数:**

H
HelloCrease 已提交
1379 1380 1381
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1382 1383 1384

**示例:**

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

P
panqiangbiao 已提交
1403
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1404

P
panqiangbiao 已提交
1405
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1406 1407 1408

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

P
panqiangbiao 已提交
1409
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1410

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

P
panqiangbiao 已提交
1413 1414
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
1441
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1442

P
panqiangbiao 已提交
1443
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1444 1445 1446

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

P
panqiangbiao 已提交
1447
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1448

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

P
panqiangbiao 已提交
1451 1452
**参数:**

H
HelloCrease 已提交
1453 1454 1455
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1456 1457 1458

**返回值:**

H
HelloCrease 已提交
1459 1460
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1461
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1462 1463 1464

**示例:**

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

P
panqiangbiao 已提交
1488
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1489

P
panqiangbiao 已提交
1490
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1491 1492 1493

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

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

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

P
panqiangbiao 已提交
1498 1499
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
1525
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1526

P
panqiangbiao 已提交
1527
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1528 1529 1530

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

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

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

P
panqiangbiao 已提交
1535 1536
**参数:**

H
HelloCrease 已提交
1537 1538 1539
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1540 1541 1542

**返回值:**

H
HelloCrease 已提交
1543 1544
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1545
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1546 1547 1548

**示例:**

1549
```js
P
panqiangbiao 已提交
1550
async function example() {
1551
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1552 1553 1554 1555
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1556 1557
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566
    };
    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 已提交
1567 1568
```

P
panqiangbiao 已提交
1569
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1570

P
panqiangbiao 已提交
1571
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1572 1573 1574

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

P
panqiangbiao 已提交
1575
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1576

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

P
panqiangbiao 已提交
1579 1580
**参数:**

H
HelloCrease 已提交
1581 1582 1583
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1584 1585 1586

**示例:**

1587
```js
P
panqiangbiao 已提交
1588
async function example() {
1589
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1590 1591 1592 1593
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1594 1595
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606
    };
    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 已提交
1607 1608
```

P
panqiangbiao 已提交
1609
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1610

P
panqiangbiao 已提交
1611
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1612 1613 1614

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

P
panqiangbiao 已提交
1615
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1616

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

P
panqiangbiao 已提交
1619 1620
**返回值:**

H
HelloCrease 已提交
1621 1622
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1623
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1624 1625 1626

**示例:**

1627
```js
P
panqiangbiao 已提交
1628
async function example() {
1629
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1630 1631 1632 1633
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1634 1635
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1636 1637 1638 1639 1640 1641 1642 1643 1644
    };
    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 已提交
1645 1646
```

P
panqiangbiao 已提交
1647
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1648

A
AOL 已提交
1649
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1650 1651 1652

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

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

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

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

P
panqiangbiao 已提交
1659 1660
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
1687
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1688

A
AOL 已提交
1689
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1690 1691 1692

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

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

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

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

P
panqiangbiao 已提交
1699 1700
**参数:**

H
HelloCrease 已提交
1701 1702 1703
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1704 1705 1706

**返回值:**

H
HelloCrease 已提交
1707 1708
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1709
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1710 1711 1712

**示例:**

1713
```js
P
panqiangbiao 已提交
1714
async function example() {
1715
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1716 1717 1718 1719
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1720 1721
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730
    };
    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 已提交
1731 1732
```

P
panqiangbiao 已提交
1733
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1734

P
panqiangbiao 已提交
1735
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1736 1737 1738

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

P
panqiangbiao 已提交
1739
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1740

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

P
panqiangbiao 已提交
1743 1744
**参数:**

H
HelloCrease 已提交
1745 1746 1747
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1748 1749 1750

**示例:**

1751
```js
P
panqiangbiao 已提交
1752
async function example() {
1753
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1754 1755 1756 1757
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1758 1759
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1760 1761 1762
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1763 1764 1765 1766 1767 1768 1769
    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 已提交
1770
}
P
panqiangbiao 已提交
1771 1772
```

P
panqiangbiao 已提交
1773
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1774

P
panqiangbiao 已提交
1775
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1776

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

P
panqiangbiao 已提交
1779
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1780

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

P
panqiangbiao 已提交
1783 1784
**返回值:**

H
HelloCrease 已提交
1785 1786
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1787
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1788 1789 1790

**示例:**

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

Z
zengyawen 已提交
1810
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1811 1812 1813

文件检索结果集。

Z
zengyawen 已提交
1814
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1815

P
panqiangbiao 已提交
1816
getCount(): number
P
panqiangbiao 已提交
1817 1818 1819

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

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

P
panqiangbiao 已提交
1822 1823
**返回值**

H
HelloCrease 已提交
1824 1825
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1826
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1827 1828 1829

**示例**

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

Z
zengyawen 已提交
1845
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1846

P
panqiangbiao 已提交
1847
isAfterLast(): boolean
P
panqiangbiao 已提交
1848 1849 1850

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

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

P
panqiangbiao 已提交
1853 1854
**返回值**

H
HelloCrease 已提交
1855 1856
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1857
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1858 1859 1860

**示例**

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

P
panqiangbiao 已提交
1884 1885
            }
    }
P
panqiangbiao 已提交
1886
}
P
panqiangbiao 已提交
1887 1888
```

Z
zengyawen 已提交
1889
### close<sup>7+</sup>
P
panqiangbiao 已提交
1890

P
panqiangbiao 已提交
1891
close(): void
P
panqiangbiao 已提交
1892 1893 1894

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

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

P
panqiangbiao 已提交
1897 1898
**示例**

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

Z
zengyawen 已提交
1914
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1915

P
panqiangbiao 已提交
1916
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1917 1918 1919

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

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

P
panqiangbiao 已提交
1922 1923
**参数**

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

**示例**

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

Z
zengyawen 已提交
1951
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1952

P
panqiangbiao 已提交
1953
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1954

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

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

P
panqiangbiao 已提交
1959 1960
**返回值**

Z
zengyawen 已提交
1961 1962
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1963
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1964 1965 1966

**示例**

1967
```js
P
panqiangbiao 已提交
1968
async function example() {
1969
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1970 1971 1972 1973
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1974 1975
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1976 1977 1978 1979 1980 1981 1982 1983
    };
    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 已提交
1984 1985
```

Z
zengyawen 已提交
1986
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1987

P
panqiangbiao 已提交
1988
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1989 1990 1991

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

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

P
panqiangbiao 已提交
1994 1995
**参数**

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

**示例**

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

Z
zengyawen 已提交
2023
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2024

P
panqiangbiao 已提交
2025
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2026 2027 2028

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

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

P
panqiangbiao 已提交
2031 2032
**返回值**

Z
zengyawen 已提交
2033 2034 2035
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2036 2037 2038

**示例**

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

Z
zengyawen 已提交
2056
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2057

P
panqiangbiao 已提交
2058
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2059 2060 2061

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

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

P
panqiangbiao 已提交
2064 2065
**参数**

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

**示例**

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

Z
zengyawen 已提交
2093
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2094

P
panqiangbiao 已提交
2095
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2096 2097 2098

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

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

P
panqiangbiao 已提交
2101 2102
**返回值**

Z
zengyawen 已提交
2103 2104 2105
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2106 2107 2108

**示例**

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

Z
zengyawen 已提交
2124
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2125

P
panqiangbiao 已提交
2126
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2127 2128 2129

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

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

P
panqiangbiao 已提交
2132 2133
**参数**

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

**示例**

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

Z
zengyawen 已提交
2162
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2163

P
panqiangbiao 已提交
2164
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2165 2166 2167

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

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

P
panqiangbiao 已提交
2170 2171
**参数**

Z
zengyawen 已提交
2172
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
2173 2174
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
2175 2176 2177

**返回值**

Z
zengyawen 已提交
2178 2179 2180
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2181 2182 2183

**示例**

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

Z
zengyawen 已提交
2203
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2204

P
panqiangbiao 已提交
2205
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2206 2207 2208

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

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

P
panqiangbiao 已提交
2211 2212
**参数**

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

**示例**

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

Z
zengyawen 已提交
2242
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2243

P
panqiangbiao 已提交
2244
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2245 2246 2247

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

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

P
panqiangbiao 已提交
2250 2251
**返回值**

Z
zengyawen 已提交
2252 2253 2254
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2255 2256 2257

**示例**

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

Z
zengyawen 已提交
2273
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2274 2275 2276

实体相册

Z
zengyawen 已提交
2277
### 属性
P
panqiangbiao 已提交
2278

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

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

P
panqiangbiao 已提交
2291 2292 2293
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2294 2295 2296

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

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

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

P
panqiangbiao 已提交
2301 2302
**参数**

Z
zengyawen 已提交
2303 2304 2305
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2306 2307 2308

**示例**

2309
```js
P
panqiangbiao 已提交
2310
async function example() {
P
panqiangbiao 已提交
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
    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 已提交
2326 2327
```

P
panqiangbiao 已提交
2328
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2329

P
panqiangbiao 已提交
2330
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2331 2332 2333

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

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

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

P
panqiangbiao 已提交
2338 2339
**返回值**

H
HelloCrease 已提交
2340 2341
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2342 2343 2344 2345
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2346
```js
P
panqiangbiao 已提交
2347
async function example() {
P
panqiangbiao 已提交
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360
    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 已提交
2361 2362
```

Z
zengyawen 已提交
2363
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2364

P
panqiangbiao 已提交
2365
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2366 2367 2368

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

P
panqiangbiao 已提交
2369
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2370

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

P
panqiangbiao 已提交
2373 2374
**参数**

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

**示例**

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

Z
zengyawen 已提交
2401
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2402

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

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

P
panqiangbiao 已提交
2407
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2408

P
panqiangbiao 已提交
2409
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2410 2411 2412

**参数**

Z
zengyawen 已提交
2413
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2414
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2415
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2416 2417 2418

**返回值**

Z
zengyawen 已提交
2419 2420
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2421
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2422 2423 2424

**示例**

2425
```js
P
panqiangbiao 已提交
2426
async function example() {
P
panqiangbiao 已提交
2427 2428 2429 2430
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2431 2432 2433
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
2434
    };
P
panqiangbiao 已提交
2435 2436 2437 2438 2439 2440 2441 2442
    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 已提交
2443 2444
```

P
panqiangbiao 已提交
2445
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2446

P
panqiangbiao 已提交
2447
注册设备的信息。
2448 2449

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

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

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



Z
zengyawen 已提交
2462
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2463 2464 2465

枚举,媒体类型。

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

潘强标 已提交
2468 2469 2470 2471 2472 2473
| 名称  |  说明 |
| ----- |  ---- |
| FILE  |  文件 |
| IMAGE |  图片 |
| VIDEO |  视频 |
| AUDIO |  音频 |
P
panqiangbiao 已提交
2474

Z
zengyawen 已提交
2475
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2476 2477 2478

枚举,文件关键信息。

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

Z
zengyawen 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
| 名称          | 默认值              | 说明                                                       |
| ------------- | ------------------- | ---------------------------------------------------------- |
| 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 已提交
2496
| DURATION      | duration            | 持续时间(单位:毫秒)                                       |
Z
zengyawen 已提交
2497 2498
| WIDTH         | width               | 图片宽度(单位:像素)                                     |
| HEIGHT        | height              | 图片高度(单位:像素)                                     |
P
panqiangbiao 已提交
2499
| ORIENTATION   | orientation         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
Z
zengyawen 已提交
2500 2501
| ALBUM_ID      | bucket_id           | 文件所归属的相册编号                                       |
| ALBUM_NAME    | bucket_display_name | 文件所归属相册名称                                         |
P
panqiangbiao 已提交
2502

Z
zengyawen 已提交
2503
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2504 2505 2506

枚举,目录类型。

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

潘强标 已提交
2509 2510 2511 2512 2513 2514 2515 2516
| 名称          |  说明               |
| ------------- |  ------------------ |
| DIR_CAMERA    |  表示Camera文件路径 |
| DIR_VIDEO     |  表示视频路径       |
| DIR_IMAGE     |  表示图片路径       |
| DIR_AUDIO     |  表示音频路径       |
| DIR_DOCUMENTS |  表示文档路径       |
| DIR_DOWNLOAD  |  表示下载路径       |
P
panqiangbiao 已提交
2517

Z
zengyawen 已提交
2518
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2519 2520

枚举,设备类型。
2521 2522

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

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

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

Z
zengyawen 已提交
2536
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2537 2538 2539

检索条件。

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

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

P
panqiangbiao 已提交
2551
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2552 2553

图片尺寸。
2554 2555

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

H
HelloCrease 已提交
2557 2558 2559 2560
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2561

Z
update  
zengyawen 已提交
2562
## MediaAssetOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2563 2564 2565

媒体资源选项。

Z
update  
zengyawen 已提交
2566
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2567 2568 2569 2570

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


Z
zengyawen 已提交
2571 2572 2573 2574 2575
| 名称         | 类型   | 必填 | 描述                                                         |
| ------------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2576

Z
update  
zengyawen 已提交
2577
## MediaSelectOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2578 2579 2580

媒体资源类型选项。

Z
update  
zengyawen 已提交
2581
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2582 2583 2584

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

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