js-apis-medialibrary.md 90.2 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;
P
panqiangbiao 已提交
97 98 99 100
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
潘强标 已提交
101
media.getFileAssets(imagesfetchOp, (error, fetchFileResult) => {
P
panqiangbiao 已提交
102
    if (fetchFileResult != undefined) {
P
panqiangbiao 已提交
103
        console.info('mediaLibraryTest : ASSET_CALLBACK fetchFileResult success');
104 105 106 107 108
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
            fetchFileResult.getNextObject((err, fileAsset) => {
            if (err) {
                console.error('Failed ');
                return;
P
panqiangbiao 已提交
109
            }
110 111 112
            console.log('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
            })
        } 
P
panqiangbiao 已提交
113
    }
P
panqiangbiao 已提交
114
});
P
panqiangbiao 已提交
115
```
Z
zengyawen 已提交
116
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
117

P
panqiangbiao 已提交
118
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
119 120 121

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

P
panqiangbiao 已提交
122
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
123

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

P
panqiangbiao 已提交
126 127
**参数:**

Z
zengyawen 已提交
128 129 130
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 文件检索选项 |
P
panqiangbiao 已提交
131 132 133

**返回值**

Z
zengyawen 已提交
134 135 136
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | 文件数据结果集 |
P
panqiangbiao 已提交
137 138 139

**示例:**

140
```js
141 142
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
143 144 145 146
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
潘强标 已提交
147
media.getFileAssets(imagesfetchOp).then(function(fetchFileResult){
Z
zhang-daiyue 已提交
148
    console.info("getFileAssets successfully: image number is "+ fetchFileResult.getCount());
P
panqiangbiao 已提交
149 150 151
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
P
panqiangbiao 已提交
152 153
```

P
panqiangbiao 已提交
154
### on<sup>8+</sup>
P
panqiangbiao 已提交
155

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

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

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

P
panqiangbiao 已提交
162 163
**参数:**

H
HelloCrease 已提交
164 165 166 167
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| 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 已提交
168 169 170

**示例:**

171
```js
Z
zhang-daiyue 已提交
172
media.on('imageChange', () => {
P
panqiangbiao 已提交
173
    // image file had changed, do something
P
panqiangbiao 已提交
174 175
})
```
P
panqiangbiao 已提交
176
### off<sup>8+</sup>
P
panqiangbiao 已提交
177

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

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

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

P
panqiangbiao 已提交
184 185
**参数:**

H
HelloCrease 已提交
186 187 188 189
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| 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 已提交
190 191 192

**示例:**

193
```js
潘强标 已提交
194
media.off('imageChange', () => {
P
panqiangbiao 已提交
195
    // stop listening success
P
panqiangbiao 已提交
196 197 198
})
```

B
bmeangel 已提交
199
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
200

P
panqiangbiao 已提交
201
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
202 203 204

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

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

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

P
panqiangbiao 已提交
209 210
**参数:**

Z
zengyawen 已提交
211 212 213 214 215 216
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型                                                     |
| displayName  | string                                  | 是   | 展示文件名                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
217 218 219

**示例:**

220
```js
P
panqiangbiao 已提交
221 222 223 224 225
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
潘强标 已提交
226
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
P
panqiangbiao 已提交
227 228 229 230 231 232 233
        if (fileAsset != undefined) {
            console.info('createAsset successfully, message = ' + err);
        } else {
            console.info('createAsset failed, message = ' + err);
        }
    });
}
P
panqiangbiao 已提交
234 235
```

P
panqiangbiao 已提交
236
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
237

P
panqiangbiao 已提交
238
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
239 240 241

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

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

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

P
panqiangbiao 已提交
246 247
**参数:**

Z
zengyawen 已提交
248 249 250 251 252
| 参数名       | 类型                     | 必填 | 说明                                                         |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | 是   | 媒体类型                                                     |
| displayName  | string                   | 是   | 展示文件名                                                   |
| relativePath | string                   | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
253 254 255

**返回值**

Z
zengyawen 已提交
256 257 258
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | 媒体数据FileAsset |
P
panqiangbiao 已提交
259 260 261

**示例:**

262
```js
263 264 265 266 267 268
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 已提交
269 270
```

Z
zengyawen 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
### deleteAsset<sup>8+</sup>

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

删除媒体文件资源

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

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

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

**参数:**

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

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

**示例:**

```js
async function example() {
298 299
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    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

删除媒体文件资源

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

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

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

**参数:**

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

**示例:**

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

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

P
panqiangbiao 已提交
370
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
371 372 373 374 375

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

**参数:**

Z
zengyawen 已提交
376 377 378 379
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback 返回公共目录路径 |
P
panqiangbiao 已提交
380 381 382

**示例:**

383
```js
P
panqiangbiao 已提交
384
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
P
panqiangbiao 已提交
385
media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => {
P
panqiangbiao 已提交
386
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
387
        console.info('mediaLibraryTest : getPublicDirectory passed');
P
panqiangbiao 已提交
388
    } else {
P
panqiangbiao 已提交
389
        console.info('mediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
390 391 392 393
    }
});
```

P
panqiangbiao 已提交
394
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
395

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

P
panqiangbiao 已提交
398
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
399 400 401 402 403

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

**参数:**

Z
zengyawen 已提交
404 405 406
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
407 408 409

**返回值:**

Z
zengyawen 已提交
410 411 412
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
413 414 415

**示例:**

416
```js
P
panqiangbiao 已提交
417
async function example() {
P
panqiangbiao 已提交
418 419
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
    const dicResult = await media.getPublicDirectory(DIR_CAMERA);
P
panqiangbiao 已提交
420
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
421 422 423 424
        console.info('MediaLibraryTest : getPublicDirectory');
    } else {
        console.info('MediaLibraryTest : getPublicDirectory failed');
    }
P
panqiangbiao 已提交
425 426 427
}
```

Z
zengyawen 已提交
428
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
429

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

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

P
panqiangbiao 已提交
434
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
435

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

P
panqiangbiao 已提交
438 439
**参数**

Z
zengyawen 已提交
440 441 442 443
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册获取条件                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
444 445 446

**示例:**

447
```js
P
panqiangbiao 已提交
448 449 450 451
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
452
media.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
453 454 455 456 457 458 459
    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 已提交
460
})
P
panqiangbiao 已提交
461 462
```

Z
zengyawen 已提交
463
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
464

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

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

P
panqiangbiao 已提交
469
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
470

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

P
panqiangbiao 已提交
473 474
**参数:**

Z
zengyawen 已提交
475 476 477
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
478 479 480

**返回值:**

Z
zengyawen 已提交
481 482 483
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
484 485 486

**示例:**

487
```js
P
panqiangbiao 已提交
488 489 490 491
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
492
media.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
P
panqiangbiao 已提交
493 494 495 496
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
    console.info("getAlbums failed with error:"+ err);
});
P
panqiangbiao 已提交
497 498
```

P
panqiangbiao 已提交
499
### release<sup>8+</sup>
P
panqiangbiao 已提交
500

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

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

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

P
panqiangbiao 已提交
508 509
**参数:**

H
HelloCrease 已提交
510 511 512
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调表示成功还是失败 |
P
panqiangbiao 已提交
513 514 515

**示例:**

516
```js
P
panqiangbiao 已提交
517
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
518
media.release((err) => {
P
panqiangbiao 已提交
519
    // do something
P
panqiangbiao 已提交
520
});
P
panqiangbiao 已提交
521 522
```

P
panqiangbiao 已提交
523
### release<sup>8+</sup>
P
panqiangbiao 已提交
524

P
panqiangbiao 已提交
525
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
526

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

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

P
panqiangbiao 已提交
532 533
**返回值:**

H
HelloCrease 已提交
534 535
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
536
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
537 538 539

**示例:**

540
```js
P
panqiangbiao 已提交
541
media.release()
P
panqiangbiao 已提交
542 543
```

Z
update  
zengyawen 已提交
544
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
545 546 547 548 549

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

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

Z
update  
zengyawen 已提交
550
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
551 552 553 554 555

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

**参数:**

H
HelloCrease 已提交
556 557 558 559
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
| callback | AsyncCallback&lt;string&gt;           | 是    | 媒体资源保存回调,返回保存成功后得到的URI。 |
P
panqiangbiao 已提交
560 561 562

**示例:**

563
```js
P
panqiangbiao 已提交
564
let option = {
潘强标 已提交
565 566 567
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
568 569 570 571 572 573 574 575 576
};
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.
});
577
```
P
panqiangbiao 已提交
578 579


Z
update  
zengyawen 已提交
580
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
581 582 583 584 585

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

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

Z
update  
zengyawen 已提交
586
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
587 588 589 590 591

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

**参数:**

H
HelloCrease 已提交
592 593 594
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
595 596 597

**返回值:**

H
HelloCrease 已提交
598 599
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
600 601 602 603
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

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


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

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

启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。

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

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

**参数:**

H
HelloCrease 已提交
631 632 633 634 635
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","dataability://")列表。 |
| index    | number                    | 是    | 开始显示的图片序号。                               |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
636 637 638

**示例:**

639
```js
P
panqiangbiao 已提交
640
let images = [
潘强标 已提交
641 642
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
P
panqiangbiao 已提交
643
];
H
HelloCrease 已提交
644
/* 网络图片使用方式
P
panqiangbiao 已提交
645 646 647 648
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
649
*/
P
panqiangbiao 已提交
650 651 652 653 654 655 656 657
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.");
});
658
```
P
panqiangbiao 已提交
659 660


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

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

启动图片预览界面,可以预览列表中首张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。

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

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

**参数:**

H
HelloCrease 已提交
673 674 675 676
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","dataability://")列表。 |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
677 678 679

**示例:**

680
```js
P
panqiangbiao 已提交
681
let images = [
潘强标 已提交
682 683
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
P
panqiangbiao 已提交
684
];
H
HelloCrease 已提交
685
/* 网络图片使用方式
P
panqiangbiao 已提交
686 687 688 689
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
690
*/
P
panqiangbiao 已提交
691 692 693 694 695 696 697
mediaLibrary.getMediaLibrary().startImagePreview(images, (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 705 706

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

启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用Promise方式进行异步回调。

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 715 716
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
| images | Array&lt;string&gt; | 是    | 预览的图片URI("https://","dataability://")列表。 |
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
717 718 719

**返回值:**

H
HelloCrease 已提交
720 721
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
P
panqiangbiao 已提交
722 723 724 725
| Promise&lt;void&gt; | Promise实例,用于异步获取预览结果,失败时返回错误信息。 |

**示例:**

726
```js
P
panqiangbiao 已提交
727
let images = [
潘强标 已提交
728 729
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
P
panqiangbiao 已提交
730
];
H
HelloCrease 已提交
731
/* 网络图片使用方式
P
panqiangbiao 已提交
732 733 734 735
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
736
*/
P
panqiangbiao 已提交
737 738 739 740 741 742
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.");
});
743
```
P
panqiangbiao 已提交
744 745


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

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

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

752
> **说明**: <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 已提交
753 754 755 756 757

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

**参数:**

H
HelloCrease 已提交
758 759
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
Z
zengyawen 已提交
760
| option   | [MediaSelectOption](#mediaselectoptiondeprecated)  | 是    | 媒体选择选项。                              |
H
HelloCrease 已提交
761
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 媒体选择回调,返回选择的媒体URI(dataability://)列表。 |
P
panqiangbiao 已提交
762 763 764

**示例:**

765
```js
766 767 768 769 770
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
771
};
772
mediaLibrary.getMediaLibrary().startMediaSelect(imagesfetchOp, (err, value) => {
P
panqiangbiao 已提交
773 774 775 776 777 778 779
    if (err) {
        console.log("An error occurred when selecting media resources.");
        return;
    }
    console.log("Media resources selected.");
    // Obtain the media selection value.
});
780
```
P
panqiangbiao 已提交
781 782


Z
update  
zengyawen 已提交
783
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
784 785 786 787 788

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

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

789
> **说明**: <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 已提交
790 791 792 793 794

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

**参数:**

H
HelloCrease 已提交
795 796
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
Z
zengyawen 已提交
797
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
798 799 800

**返回值:**

H
HelloCrease 已提交
801 802
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
P
panqiangbiao 已提交
803 804 805 806
| Promise&lt;Array&lt;string&gt;&gt; | Promise实例,用于异步获取选择的媒体URI(dataability://)列表。 |

**示例:**

807
```js
808 809 810 811 812
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
813
};
814
mediaLibrary.getMediaLibrary().startMediaSelect(imagesfetchOp).then((value) => {
P
panqiangbiao 已提交
815 816 817 818 819 820
    console.log("Media resources selected.");
    // Obtain the media selection value.
}).catch((err) => {
    console.log("An error occurred when selecting media resources.");
});

Z
zengyawen 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
```
### 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>
861

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

Z
zengyawen 已提交
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
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)
        }
    })
}
969
```
P
panqiangbiao 已提交
970

Z
zengyawen 已提交
971
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
972 973 974

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

Z
zengyawen 已提交
975 976 977
### 属性

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

Z
zengyawen 已提交
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| id                        | number                   | 是   | 否   | 文件资源编号                                           |
| uri                       | string                   | 是   | 否   | 文件资源uri(如:dataability:///media/image/2)         |
| 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  单位:度) |
潘强标 已提交
998
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)                                   |
Z
zengyawen 已提交
999 1000 1001
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
1002 1003 1004


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

P
panqiangbiao 已提交
1006
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1007 1008 1009

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

P
panqiangbiao 已提交
1010
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1011

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

P
panqiangbiao 已提交
1014 1015
**参数:**

H
HelloCrease 已提交
1016 1017 1018
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1019 1020 1021

**示例:**

1022
```js
P
panqiangbiao 已提交
1023
async function example() {
Z
zhang-daiyue 已提交
1024
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1025 1026 1027 1028
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1029 1030
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1031 1032 1033 1034 1035 1036 1037
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
1038 1039
```

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

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

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

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
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1054
| Promise&lt;boolean&gt; | Promise实例,返回当前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 1074 1075
    };
    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 已提交
1076 1077
```

P
panqiangbiao 已提交
1078
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1079

P
panqiangbiao 已提交
1080
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1081 1082 1083

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

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

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

P
panqiangbiao 已提交
1088 1089
**参数:**

H
HelloCrease 已提交
1090 1091 1092
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1093 1094 1095

**示例:**

1096
```js
P
panqiangbiao 已提交
1097
async function example() {
Z
zhang-daiyue 已提交
1098
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1099 1100 1101 1102
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1103 1104
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1105 1106 1107
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1108
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1109 1110
    asset.commitModify(() => {
        console.info('commitModify success');   
P
panqiangbiao 已提交
1111
    });
P
panqiangbiao 已提交
1112
}
P
panqiangbiao 已提交
1113 1114
```

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

P
panqiangbiao 已提交
1117
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1118 1119 1120

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

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

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

P
panqiangbiao 已提交
1125 1126
**返回值:**

H
HelloCrease 已提交
1127 1128
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1129
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1130 1131 1132

**示例:**

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

P
panqiangbiao 已提交
1150
### open<sup>8+</sup>
P
panqiangbiao 已提交
1151

P
panqiangbiao 已提交
1152
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1153 1154 1155

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

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

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

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

P
panqiangbiao 已提交
1162 1163
**参数**

H
HelloCrease 已提交
1164 1165 1166 1167
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件句柄                            |
P
panqiangbiao 已提交
1168 1169 1170

**示例:**

1171
```js
P
panqiangbiao 已提交
1172
async function example() {
P
panqiangbiao 已提交
1173
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1174 1175
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1176
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1177 1178 1179 1180 1181 1182 1183 1184
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
P
panqiangbiao 已提交
1185 1186
```

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

P
panqiangbiao 已提交
1189
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1190 1191 1192

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

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

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

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

P
panqiangbiao 已提交
1199 1200
**参数:**

H
HelloCrease 已提交
1201 1202 1203
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
1204 1205 1206

**返回值:**

H
HelloCrease 已提交
1207 1208
| 类型                    | 说明            |
| --------------------- | ------------- |
P
panqiangbiao 已提交
1209
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
1210 1211 1212

**示例:**

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

P
panqiangbiao 已提交
1229
### close<sup>8+</sup>
P
panqiangbiao 已提交
1230

P
panqiangbiao 已提交
1231
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1232 1233 1234

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

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

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

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

H
HelloCrease 已提交
1241 1242 1243 1244
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1245 1246 1247

**示例:**

1248
```js
P
panqiangbiao 已提交
1249
async function example() {
Z
zhang-daiyue 已提交
1250
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1251 1252 1253 1254
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1255 1256
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1257 1258 1259
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1260 1261 1262 1263
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd, (closeErr) => {
            if (closeErr != undefined) {
1264
                console.info('mediaLibraryTest : close : FAIL ' + closeErr);
Z
zhang-daiyue 已提交
1265 1266 1267 1268 1269 1270 1271 1272
                console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1273 1274
    });
}
P
panqiangbiao 已提交
1275 1276
```

P
panqiangbiao 已提交
1277
### close<sup>8+</sup>
P
panqiangbiao 已提交
1278

P
panqiangbiao 已提交
1279
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1280 1281 1282

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

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

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

P
panqiangbiao 已提交
1287 1288
**参数:**

H
HelloCrease 已提交
1289 1290 1291
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1292 1293 1294

**返回值:**

H
HelloCrease 已提交
1295 1296
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1297
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1298 1299 1300

**示例:**

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

Z
zhang-daiyue 已提交
1320 1321 1322 1323 1324 1325 1326
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1327 1328
    });
}
P
panqiangbiao 已提交
1329 1330
```

P
panqiangbiao 已提交
1331
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1332

P
panqiangbiao 已提交
1333
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1334 1335 1336

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

P
panqiangbiao 已提交
1337
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1338

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

P
panqiangbiao 已提交
1341 1342
**参数:**

H
HelloCrease 已提交
1343 1344 1345
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1346 1347 1348

**示例:**

1349
```js
P
panqiangbiao 已提交
1350
async function example() {
Z
zhang-daiyue 已提交
1351
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1352 1353 1354 1355
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1356 1357
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1358 1359 1360 1361
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
P
panqiangbiao 已提交
1362
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
1363 1364
    });
}
P
panqiangbiao 已提交
1365 1366
```

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

P
panqiangbiao 已提交
1369
getThumbnail(size: Size, 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 1382
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1383 1384 1385

**示例:**

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

P
panqiangbiao 已提交
1405
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1406

P
panqiangbiao 已提交
1407
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1408 1409 1410

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

P
panqiangbiao 已提交
1411
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1412

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

P
panqiangbiao 已提交
1415 1416
**参数:**

H
HelloCrease 已提交
1417 1418 1419
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1420 1421 1422

**返回值:**

H
HelloCrease 已提交
1423 1424
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1425
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1426 1427 1428

**示例:**

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

P
panqiangbiao 已提交
1452
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1453

P
panqiangbiao 已提交
1454
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1455 1456 1457

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

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

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

P
panqiangbiao 已提交
1462 1463
**参数:**

H
HelloCrease 已提交
1464 1465 1466 1467
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1468 1469 1470

**示例:**

1471
```js
P
panqiangbiao 已提交
1472
async function example() {
1473
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1474 1475 1476 1477
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1478 1479
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1480 1481 1482 1483 1484 1485 1486
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
1487 1488
```

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

P
panqiangbiao 已提交
1491
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1492 1493 1494

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

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

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

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

H
HelloCrease 已提交
1501 1502 1503
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1504 1505 1506

**返回值:**

H
HelloCrease 已提交
1507 1508
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1509
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1510 1511 1512

**示例:**

1513
```js
P
panqiangbiao 已提交
1514
async function example() {
1515
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1516 1517 1518 1519
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1520 1521
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530
    };
    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 已提交
1531 1532
```

P
panqiangbiao 已提交
1533
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1534

P
panqiangbiao 已提交
1535
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1536 1537 1538

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

P
panqiangbiao 已提交
1539
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1540

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

P
panqiangbiao 已提交
1543 1544
**参数:**

H
HelloCrease 已提交
1545 1546 1547
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1548 1549 1550

**示例:**

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

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

P
panqiangbiao 已提交
1575
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1576 1577 1578

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

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

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

P
panqiangbiao 已提交
1583 1584
**返回值:**

H
HelloCrease 已提交
1585 1586
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1587
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1588 1589 1590

**示例:**

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

P
panqiangbiao 已提交
1611
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1612

A
AOL 已提交
1613
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1614 1615 1616

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

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

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

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

P
panqiangbiao 已提交
1623 1624
**参数:**

H
HelloCrease 已提交
1625 1626 1627 1628
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1629 1630 1631

**示例:**

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

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

A
AOL 已提交
1653
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1654 1655 1656

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

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

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

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

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

H
HelloCrease 已提交
1665 1666 1667
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1668 1669 1670

**返回值:**

H
HelloCrease 已提交
1671 1672
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1673
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1674 1675 1676

**示例:**

1677
```js
P
panqiangbiao 已提交
1678
async function example() {
1679
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1680 1681 1682 1683
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1684 1685
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694
    };
    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 已提交
1695 1696
```

P
panqiangbiao 已提交
1697
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1698

P
panqiangbiao 已提交
1699
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1700 1701 1702

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

P
panqiangbiao 已提交
1703
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1704

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

P
panqiangbiao 已提交
1707 1708
**参数:**

H
HelloCrease 已提交
1709 1710 1711
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1712 1713 1714

**示例:**

1715
```js
P
panqiangbiao 已提交
1716
async function example() {
1717
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1718 1719 1720 1721
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1722 1723
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1724 1725 1726 1727 1728 1729
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash(isTrashCallBack);
    function isTrashCallBack(err, isTrash) {
            if (isTrash == true) {
P
panqiangbiao 已提交
1730
                console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK isTrash = ' + isTrash);
1731
                asset.trash(true, isTrashCallBack);
P
panqiangbiao 已提交
1732 1733

            } else {
P
panqiangbiao 已提交
1734 1735
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');
P
panqiangbiao 已提交
1736 1737

            }
P
panqiangbiao 已提交
1738
    }
P
panqiangbiao 已提交
1739
}
P
panqiangbiao 已提交
1740 1741
```

P
panqiangbiao 已提交
1742
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1743

P
panqiangbiao 已提交
1744
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1745

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

P
panqiangbiao 已提交
1748
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1749

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

P
panqiangbiao 已提交
1752 1753
**返回值:**

H
HelloCrease 已提交
1754 1755
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1756
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1757 1758 1759

**示例:**

1760
```js
P
panqiangbiao 已提交
1761
async function example() {
1762
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1763 1764 1765 1766
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1767 1768
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash().then(function(isTrash){
        console.info("isTrash result:"+ isTrash);
    }).catch(function(err){
        console.info("isTrash failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1778 1779
```

Z
zengyawen 已提交
1780
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1781 1782 1783

文件检索结果集。

Z
zengyawen 已提交
1784
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1785

P
panqiangbiao 已提交
1786
getCount(): number
P
panqiangbiao 已提交
1787 1788 1789

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

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

P
panqiangbiao 已提交
1792 1793
**返回值**

H
HelloCrease 已提交
1794 1795
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1796
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1797 1798 1799

**示例**

1800
```js
P
panqiangbiao 已提交
1801
async function example() {
1802
    let fileKeyObj = mediaLibrary.FileKey;
Z
zhang-daiyue 已提交
1803
    let fileType = mediaLibrary.MediaType.FILE;
P
panqiangbiao 已提交
1804 1805 1806
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1807 1808
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1809 1810 1811 1812
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1813 1814
```

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

P
panqiangbiao 已提交
1817
isAfterLast(): boolean
P
panqiangbiao 已提交
1818 1819 1820

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

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

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

H
HelloCrease 已提交
1825 1826
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1827
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1828 1829 1830

**示例**

1831
```js
P
panqiangbiao 已提交
1832
async function example() {
1833
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1834 1835 1836 1837
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1838 1839
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1840 1841 1842
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1843
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1844 1845 1846 1847
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1848
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1849
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1850 1851
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1852
              fetchFileResult.close();
P
panqiangbiao 已提交
1853

P
panqiangbiao 已提交
1854 1855
            }
    }
P
panqiangbiao 已提交
1856
}
P
panqiangbiao 已提交
1857 1858
```

Z
zengyawen 已提交
1859
### close<sup>7+</sup>
P
panqiangbiao 已提交
1860

P
panqiangbiao 已提交
1861
close(): void
P
panqiangbiao 已提交
1862 1863 1864

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

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

P
panqiangbiao 已提交
1867 1868
**示例**

1869
```js
P
panqiangbiao 已提交
1870
async function example() {
1871
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1872 1873 1874 1875
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1876 1877
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1878 1879 1880 1881
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1882 1883
```

Z
zengyawen 已提交
1884
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1885

P
panqiangbiao 已提交
1886
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1887 1888 1889

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

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

P
panqiangbiao 已提交
1892 1893
**参数**

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

**示例**

1900
```js
P
panqiangbiao 已提交
1901
async function example() {
1902
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1903 1904 1905 1906
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1907 1908
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1909 1910
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
1911
    fetchFileResult.getFirstObject((err, fileAsset) => {
P
panqiangbiao 已提交
1912 1913 1914 1915
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
1916
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
1917 1918
    })
}
P
panqiangbiao 已提交
1919 1920
```

Z
zengyawen 已提交
1921
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1922

P
panqiangbiao 已提交
1923
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1924

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

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

P
panqiangbiao 已提交
1929 1930
**返回值**

Z
zengyawen 已提交
1931 1932
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1933
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1934 1935 1936

**示例**

1937
```js
P
panqiangbiao 已提交
1938
async function example() {
1939
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1940 1941 1942 1943
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1944 1945
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1946 1947 1948 1949 1950 1951 1952 1953
    };
    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 已提交
1954 1955
```

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

P
panqiangbiao 已提交
1958
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1959 1960 1961

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

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

P
panqiangbiao 已提交
1964 1965
**参数**

Z
zengyawen 已提交
1966 1967 1968
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1969 1970 1971

**示例**

1972
```js
P
panqiangbiao 已提交
1973
async function example() {
1974
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1975 1976 1977 1978
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1979 1980
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1981 1982
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
1983
    fetchFileResult.getNextObject((err, fileAsset) => {
P
panqiangbiao 已提交
1984 1985 1986 1987
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
1988
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
1989 1990
    })
}
P
panqiangbiao 已提交
1991 1992
```

Z
zengyawen 已提交
1993
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1994

P
panqiangbiao 已提交
1995
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1996 1997 1998

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

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

P
panqiangbiao 已提交
2001 2002
**返回值**

Z
zengyawen 已提交
2003 2004 2005
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2006 2007 2008

**示例**

2009
```js
P
panqiangbiao 已提交
2010
async function example() {
2011
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2012 2013 2014 2015
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2016 2017
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2018 2019 2020
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
2021
    console.info('mediaLibraryTest : count:' + fetchCount);
Z
zhang-daiyue 已提交
2022
    let fileAsset = await fetchFileResult.getNextObject();
P
panqiangbiao 已提交
2023
}
P
panqiangbiao 已提交
2024 2025
```

Z
zengyawen 已提交
2026
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2027

P
panqiangbiao 已提交
2028
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2029 2030 2031

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

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

P
panqiangbiao 已提交
2034 2035
**参数**

Z
zengyawen 已提交
2036 2037
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
Z
zengyawen 已提交
2038
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2039 2040 2041

**示例**

2042
```js
P
panqiangbiao 已提交
2043
async function example() {
2044
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2045 2046 2047 2048
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2049 2050
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2051 2052
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2053
    fetchFileResult.getLastObject((err, fileAsset) => {
P
panqiangbiao 已提交
2054 2055 2056 2057
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2058
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2059 2060
    })
}
P
panqiangbiao 已提交
2061 2062
```

Z
zengyawen 已提交
2063
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2064

P
panqiangbiao 已提交
2065
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2066 2067 2068

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

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

P
panqiangbiao 已提交
2071 2072
**返回值**

Z
zengyawen 已提交
2073 2074 2075
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2076 2077 2078

**示例**

2079
```js
P
panqiangbiao 已提交
2080
async function example() {
2081
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2082 2083 2084 2085
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2086 2087
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2088 2089 2090 2091
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
2092 2093
```

Z
zengyawen 已提交
2094
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2095

P
panqiangbiao 已提交
2096
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2097 2098 2099

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

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

P
panqiangbiao 已提交
2102 2103
**参数**

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

**示例**

2111
```js
P
panqiangbiao 已提交
2112
async function example() {
2113
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2114 2115 2116 2117
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2118 2119
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2120 2121
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2122
    fetchFileResult.getPositionObject(0, (err, fileAsset) => {
P
panqiangbiao 已提交
2123 2124 2125 2126
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2127
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2128 2129
    })
}
P
panqiangbiao 已提交
2130 2131
```

Z
zengyawen 已提交
2132
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2133

P
panqiangbiao 已提交
2134
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2135 2136 2137

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

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

P
panqiangbiao 已提交
2140 2141
**参数**

Z
zengyawen 已提交
2142
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
2143 2144
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
2145 2146 2147

**返回值**

Z
zengyawen 已提交
2148 2149 2150
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2151 2152 2153

**示例**

2154
```js
P
panqiangbiao 已提交
2155
async function example() {
2156
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2157 2158 2159 2160
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2161 2162
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2163 2164
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2165
    fetchFileResult.getPositionObject(1) .then(function (fileAsset){
2166
        console.log('fileAsset.displayName : ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
2167
    }).catch(function (err) {
2168
        console.info("getFileAssets failed with error:" + err);
Z
zhang-daiyue 已提交
2169
    });
P
panqiangbiao 已提交
2170
}
P
panqiangbiao 已提交
2171 2172
```

Z
zengyawen 已提交
2173
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2174

P
panqiangbiao 已提交
2175
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2176 2177 2178

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

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

P
panqiangbiao 已提交
2181 2182
**参数**

Z
zengyawen 已提交
2183
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
2184
| -------- | ---------------------------------------- | ---- | -------------------- |
Z
zengyawen 已提交
2185
| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
2186 2187 2188

**示例**

2189
```js
P
panqiangbiao 已提交
2190
async function example() {
2191
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2192 2193 2194 2195
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2196 2197
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2198 2199
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2200
    fetchFileResult.getAllObject((err, fileAsset) => {
2201
        if (err) {
P
panqiangbiao 已提交
2202 2203
           console.error('Failed ');
           return;
2204 2205 2206 2207
        }
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
            console.log('fileAsset.displayName : ' + fileAsset[i].displayName);
        } 
P
panqiangbiao 已提交
2208 2209
    })
}
P
panqiangbiao 已提交
2210 2211
```

Z
zengyawen 已提交
2212
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2213

P
panqiangbiao 已提交
2214
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2215 2216 2217

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

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

P
panqiangbiao 已提交
2220 2221
**返回值**

Z
zengyawen 已提交
2222 2223 2224
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2225 2226 2227

**示例**

2228
```js
P
panqiangbiao 已提交
2229
async function example() {
2230
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2231 2232 2233 2234
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2235 2236
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2237 2238 2239 2240
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
P
panqiangbiao 已提交
2241 2242
```

Z
zengyawen 已提交
2243
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2244 2245 2246

实体相册

Z
zengyawen 已提交
2247
### 属性
P
panqiangbiao 已提交
2248

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

Z
zengyawen 已提交
2251
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2252
| ------------ | ------ | ---- | ---- | ------- |
Z
zengyawen 已提交
2253 2254 2255
| albumId | number | 是    | 否    | 相册编号    |
| albumName | string | 是    | 是    | 相册名称    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri   |
H
HelloCrease 已提交
2256
| dateModified | number | 是    | 否    | 修改日期    |
Z
zengyawen 已提交
2257 2258 2259
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
2260

P
panqiangbiao 已提交
2261 2262 2263
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2264 2265 2266

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

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

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

P
panqiangbiao 已提交
2271 2272
**参数**

Z
zengyawen 已提交
2273 2274 2275
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2276 2277 2278

**示例**

2279
```js
P
panqiangbiao 已提交
2280
async function example() {
P
panqiangbiao 已提交
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
    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 已提交
2296 2297
```

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

P
panqiangbiao 已提交
2300
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2301 2302 2303

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

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

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

P
panqiangbiao 已提交
2308 2309
**返回值**

H
HelloCrease 已提交
2310 2311
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2312 2313 2314 2315
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2316
```js
P
panqiangbiao 已提交
2317
async function example() {
P
panqiangbiao 已提交
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify().then(function() {
        console.info("commitModify successfully");
    }).catch(function(err){
        console.info("commitModify failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2331 2332
```

Z
zengyawen 已提交
2333
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2334

P
panqiangbiao 已提交
2335
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2336 2337 2338

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

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

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

P
panqiangbiao 已提交
2343 2344
**参数**

Z
zengyawen 已提交
2345
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2346
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2347 2348
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2349 2350 2351

**示例**

2352
```js
P
panqiangbiao 已提交
2353
async function example() {
P
panqiangbiao 已提交
2354 2355 2356 2357
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2358 2359 2360 2361
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
P
panqiangbiao 已提交
2362 2363 2364 2365 2366 2367
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
P
panqiangbiao 已提交
2368 2369 2370
}
```

Z
zengyawen 已提交
2371
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2372

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

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

P
panqiangbiao 已提交
2377
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2378

P
panqiangbiao 已提交
2379
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2380 2381 2382

**参数**

Z
zengyawen 已提交
2383
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2384
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2385
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2386 2387 2388

**返回值**

Z
zengyawen 已提交
2389 2390
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2391
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2392 2393 2394

**示例**

2395
```js
P
panqiangbiao 已提交
2396
async function example() {
P
panqiangbiao 已提交
2397 2398 2399 2400
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2401 2402 2403
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
2404
    };
P
panqiangbiao 已提交
2405 2406 2407 2408 2409 2410 2411 2412
    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 已提交
2413 2414
```

P
panqiangbiao 已提交
2415
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2416

P
panqiangbiao 已提交
2417
注册设备的信息。
2418 2419

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

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

Z
zengyawen 已提交
2423 2424 2425 2426 2427 2428
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2429 2430 2431



Z
zengyawen 已提交
2432
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2433 2434 2435

枚举,媒体类型。

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

潘强标 已提交
2438 2439 2440 2441 2442 2443
| 名称  |  说明 |
| ----- |  ---- |
| FILE  |  文件 |
| IMAGE |  图片 |
| VIDEO |  视频 |
| AUDIO |  音频 |
P
panqiangbiao 已提交
2444

Z
zengyawen 已提交
2445
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2446 2447 2448

枚举,文件关键信息。

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

Z
zengyawen 已提交
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
| 名称          | 默认值              | 说明                                                       |
| ------------- | ------------------- | ---------------------------------------------------------- |
| 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 已提交
2466
| DURATION      | duration            | 持续时间(单位:毫秒)                                       |
Z
zengyawen 已提交
2467 2468
| WIDTH         | width               | 图片宽度(单位:像素)                                     |
| HEIGHT        | height              | 图片高度(单位:像素)                                     |
P
panqiangbiao 已提交
2469
| ORIENTATION   | orientation         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
Z
zengyawen 已提交
2470 2471
| ALBUM_ID      | bucket_id           | 文件所归属的相册编号                                       |
| ALBUM_NAME    | bucket_display_name | 文件所归属相册名称                                         |
P
panqiangbiao 已提交
2472

Z
zengyawen 已提交
2473
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2474 2475 2476

枚举,目录类型。

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

潘强标 已提交
2479 2480 2481 2482 2483 2484 2485 2486
| 名称          |  说明               |
| ------------- |  ------------------ |
| DIR_CAMERA    |  表示Camera文件路径 |
| DIR_VIDEO     |  表示视频路径       |
| DIR_IMAGE     |  表示图片路径       |
| DIR_AUDIO     |  表示音频路径       |
| DIR_DOCUMENTS |  表示文档路径       |
| DIR_DOWNLOAD  |  表示下载路径       |
P
panqiangbiao 已提交
2487

Z
zengyawen 已提交
2488
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2489 2490

枚举,设备类型。
2491 2492

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

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

潘强标 已提交
2496 2497 2498 2499 2500 2501 2502 2503 2504
| 名称         |  说明       |
| ------------ |  ---------- |
| TYPE_UNKNOWN |  未识别设备 |
| TYPE_LAPTOP  |  笔记本电脑 |
| TYPE_PHONE   |  手机       |
| TYPE_TABLET  |  平板电脑   |
| TYPE_WATCH   |  智能手表   |
| TYPE_CAR     |  车载设备   |
| TYPE_TV      |  电视设备   |
P
panqiangbiao 已提交
2505

Z
zengyawen 已提交
2506
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2507 2508 2509

检索条件。

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

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

P
panqiangbiao 已提交
2521
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2522 2523

图片尺寸。
2524 2525

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

H
HelloCrease 已提交
2527 2528 2529 2530
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2531

Z
update  
zengyawen 已提交
2532
## MediaAssetOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2533 2534 2535

媒体资源选项。

Z
update  
zengyawen 已提交
2536
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2537 2538 2539 2540

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


Z
zengyawen 已提交
2541 2542 2543 2544 2545
| 名称         | 类型   | 必填 | 描述                                                         |
| ------------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2546

Z
update  
zengyawen 已提交
2547
## MediaSelectOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2548 2549 2550

媒体资源类型选项。

Z
update  
zengyawen 已提交
2551
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2552 2553 2554

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

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

2560