js-apis-medialibrary.md 89.7 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 38 39
var media = mediaLibrary.getMediaLibrary(this.context);
```

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

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

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

getMediaLibrary(): MediaLibrary

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

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

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

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

**返回值:**

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

**示例:**

```js
var media = mediaLibrary.getMediaLibrary();
```

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

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


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

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

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

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

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

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

**示例:**

93
```js
P
panqiangbiao 已提交
94 95 96 97 98 99
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
潘强标 已提交
100
media.getFileAssets(imagesfetchOp, (error, fetchFileResult) => {
P
panqiangbiao 已提交
101
    if (fetchFileResult != undefined) {
P
panqiangbiao 已提交
102
        console.info('mediaLibraryTest : ASSET_CALLBACK fetchFileResult success');
P
panqiangbiao 已提交
103 104
        fetchFileResult.getAllObject((err, fileAssetList) => {
            if (fileAssetList != undefined) {
Z
zhang-daiyue 已提交
105 106 107
                fileAssetList.forEach(function(getAllObjectInfo){
                    console.info("getAllObjectInfo.displayName :" + getAllObjectInfo.displayName);
                });
P
panqiangbiao 已提交
108
            }
P
panqiangbiao 已提交
109 110
    	});
    }
P
panqiangbiao 已提交
111
});
P
panqiangbiao 已提交
112
```
Z
zengyawen 已提交
113
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
114

P
panqiangbiao 已提交
115
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
116 117 118

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

P
panqiangbiao 已提交
119
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
120

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

P
panqiangbiao 已提交
123 124
**参数:**

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

**返回值**

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

**示例:**

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

P
panqiangbiao 已提交
151
### on<sup>8+</sup>
P
panqiangbiao 已提交
152

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

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

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

P
panqiangbiao 已提交
159 160
**参数:**

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

**示例:**

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

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

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

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

P
panqiangbiao 已提交
181 182
**参数:**

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

**示例:**

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

B
bmeangel 已提交
196
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
197

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

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

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

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

P
panqiangbiao 已提交
206 207
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
233
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
234

P
panqiangbiao 已提交
235
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
236 237 238

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

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

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

P
panqiangbiao 已提交
243 244
**参数:**

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

**返回值**

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

**示例:**

259
```js
260 261 262 263 264 265
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 已提交
266 267
```

Z
zengyawen 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
### 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() {
    let fileKeyObj = mediaLibrary.FileKey
    let fileType = mediaLibrary.MediaType.FILE
    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() {
    let fileKeyObj = mediaLibrary.FileKey
    let fileType = mediaLibrary.MediaType.FILE
    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 已提交
363
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
364

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

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

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

**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
391
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
392

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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

Z
zengyawen 已提交
425
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
426

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

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

P
panqiangbiao 已提交
431
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
432

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

P
panqiangbiao 已提交
435 436
**参数**

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

**示例:**

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

Z
zengyawen 已提交
460
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
461

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

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

P
panqiangbiao 已提交
466
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
467

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

P
panqiangbiao 已提交
470 471
**参数:**

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

**返回值:**

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

**示例:**

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

P
panqiangbiao 已提交
496
### release<sup>8+</sup>
P
panqiangbiao 已提交
497

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

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

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

P
panqiangbiao 已提交
505 506
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
520
### release<sup>8+</sup>
P
panqiangbiao 已提交
521

P
panqiangbiao 已提交
522
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
523

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

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

P
panqiangbiao 已提交
529 530
**返回值:**

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

**示例:**

537
```js
P
panqiangbiao 已提交
538
media.release()
P
panqiangbiao 已提交
539 540
```

Z
update  
zengyawen 已提交
541
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
542 543 544 545 546

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

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

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

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

**参数:**

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

**示例:**

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


Z
update  
zengyawen 已提交
577
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
578 579 580 581 582

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

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


Z
update  
zengyawen 已提交
616
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
617 618 619 620 621

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

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

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

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

**参数:**

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

**示例:**

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


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

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

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

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

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

**参数:**

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

**示例:**

677
```js
P
panqiangbiao 已提交
678
let images = [
潘强标 已提交
679 680
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
P
panqiangbiao 已提交
681
];
H
HelloCrease 已提交
682
/* 网络图片使用方式
P
panqiangbiao 已提交
683 684 685 686
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
687
*/
P
panqiangbiao 已提交
688 689 690 691 692 693 694
mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
695
```
P
panqiangbiao 已提交
696 697


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

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

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

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

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

**参数:**

H
HelloCrease 已提交
710 711 712 713
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
| images | Array&lt;string&gt; | 是    | 预览的图片URI("https://","dataability://")列表。 |
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
714 715 716

**返回值:**

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

**示例:**

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


Z
update  
zengyawen 已提交
743
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
744 745 746 747 748

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

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

749
> **说明**: <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 已提交
750 751 752 753 754

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

**参数:**

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

**示例:**

762
```js
P
panqiangbiao 已提交
763
let option = {
Z
zhang-daiyue 已提交
764
    type : "media",
P
panqiangbiao 已提交
765 766 767 768 769 770 771 772 773 774
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
    if (err) {
        console.log("An error occurred when selecting media resources.");
        return;
    }
    console.log("Media resources selected.");
    // Obtain the media selection value.
});
775
```
P
panqiangbiao 已提交
776 777


Z
update  
zengyawen 已提交
778
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
779 780 781 782 783

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

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

784
> **说明**: <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 已提交
785 786 787 788 789

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

**参数:**

H
HelloCrease 已提交
790 791
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
Z
zengyawen 已提交
792
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
793 794 795

**返回值:**

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

**示例:**

802
```js
P
panqiangbiao 已提交
803
let option = {
Z
zhang-daiyue 已提交
804
    type : "media",
P
panqiangbiao 已提交
805 806 807 808 809 810 811 812 813
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
    console.log("Media resources selected.");
    // Obtain the media selection value.
}).catch((err) => {
    console.log("An error occurred when selecting media resources.");
});

Z
zengyawen 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
```
### 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>
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>
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)
        }
    })
}
960
```
P
panqiangbiao 已提交
961

Z
zengyawen 已提交
962
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
963 964 965

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

Z
zengyawen 已提交
966 967 968
### 属性

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

Z
zengyawen 已提交
970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| 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  单位:度) |
潘强标 已提交
989
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)                                   |
Z
zengyawen 已提交
990 991 992
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
993 994 995


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

P
panqiangbiao 已提交
997
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
998 999 1000

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

P
panqiangbiao 已提交
1001
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1002

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

P
panqiangbiao 已提交
1005 1006
**参数:**

H
HelloCrease 已提交
1007 1008 1009
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1010 1011 1012

**示例:**

1013
```js
P
panqiangbiao 已提交
1014
async function example() {
Z
zhang-daiyue 已提交
1015
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1016 1017 1018 1019
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1020 1021
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1022 1023 1024 1025 1026 1027 1028
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
1029 1030
```

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

P
panqiangbiao 已提交
1033
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1034 1035 1036

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

P
panqiangbiao 已提交
1037
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1038

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

P
panqiangbiao 已提交
1041 1042
**返回值:**

H
HelloCrease 已提交
1043 1044
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1045
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1046 1047 1048

**示例:**

1049
```js
P
panqiangbiao 已提交
1050
async function example() {
Z
zhang-daiyue 已提交
1051
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1052 1053 1054 1055
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1056 1057
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066
    };
    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 已提交
1067 1068
```

P
panqiangbiao 已提交
1069
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1070

P
panqiangbiao 已提交
1071
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1072 1073 1074

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

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

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

P
panqiangbiao 已提交
1079 1080
**参数:**

H
HelloCrease 已提交
1081 1082 1083
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1084 1085 1086

**示例:**

1087
```js
P
panqiangbiao 已提交
1088
async function example() {
Z
zhang-daiyue 已提交
1089
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1090 1091 1092 1093
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1094 1095
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1096 1097 1098
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1099
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1100 1101
    asset.commitModify(() => {
        console.info('commitModify success');   
P
panqiangbiao 已提交
1102
    });
P
panqiangbiao 已提交
1103
}
P
panqiangbiao 已提交
1104 1105
```

P
panqiangbiao 已提交
1106
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1107

P
panqiangbiao 已提交
1108
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1109 1110 1111

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

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

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

P
panqiangbiao 已提交
1116 1117
**返回值:**

H
HelloCrease 已提交
1118 1119
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1120
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1121 1122 1123

**示例:**

1124
```js
P
panqiangbiao 已提交
1125
async function example() {
Z
zhang-daiyue 已提交
1126
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1127 1128 1129 1130
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1131 1132
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1133 1134 1135
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1136
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1137 1138
    asset.commitModify();
}
P
panqiangbiao 已提交
1139 1140
```

P
panqiangbiao 已提交
1141
### open<sup>8+</sup>
P
panqiangbiao 已提交
1142

P
panqiangbiao 已提交
1143
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1144 1145 1146

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

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

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

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

P
panqiangbiao 已提交
1153 1154
**参数**

H
HelloCrease 已提交
1155 1156 1157 1158
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件句柄                            |
P
panqiangbiao 已提交
1159 1160 1161

**示例:**

1162
```js
P
panqiangbiao 已提交
1163
async function example() {
P
panqiangbiao 已提交
1164
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1165 1166
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1167
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1168 1169 1170 1171 1172 1173 1174 1175
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
P
panqiangbiao 已提交
1176 1177
```

P
panqiangbiao 已提交
1178
### open<sup>8+</sup>
P
panqiangbiao 已提交
1179

P
panqiangbiao 已提交
1180
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1181 1182 1183

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

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

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

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

P
panqiangbiao 已提交
1190 1191
**参数:**

H
HelloCrease 已提交
1192 1193 1194
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
1195 1196 1197

**返回值:**

H
HelloCrease 已提交
1198 1199
| 类型                    | 说明            |
| --------------------- | ------------- |
P
panqiangbiao 已提交
1200
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
1201 1202 1203

**示例:**

1204
```js
P
panqiangbiao 已提交
1205
async function example() {
P
panqiangbiao 已提交
1206
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1207 1208
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1209
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1210 1211 1212 1213 1214 1215 1216
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
            console.info('File err!' + err);
        });
P
panqiangbiao 已提交
1217
}
P
panqiangbiao 已提交
1218 1219
```

P
panqiangbiao 已提交
1220
### close<sup>8+</sup>
P
panqiangbiao 已提交
1221

P
panqiangbiao 已提交
1222
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1223 1224 1225

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

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

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

P
panqiangbiao 已提交
1230 1231
**参数:**

H
HelloCrease 已提交
1232 1233 1234 1235
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1236 1237 1238

**示例:**

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

P
panqiangbiao 已提交
1268
### close<sup>8+</sup>
P
panqiangbiao 已提交
1269

P
panqiangbiao 已提交
1270
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1271 1272 1273

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

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

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

P
panqiangbiao 已提交
1278 1279
**参数:**

H
HelloCrease 已提交
1280 1281 1282
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1283 1284 1285

**返回值:**

H
HelloCrease 已提交
1286 1287
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1288
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1289 1290 1291

**示例:**

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

Z
zhang-daiyue 已提交
1311 1312 1313 1314 1315 1316 1317
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1318 1319
    });
}
P
panqiangbiao 已提交
1320 1321
```

P
panqiangbiao 已提交
1322
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1323

P
panqiangbiao 已提交
1324
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1325 1326 1327

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

P
panqiangbiao 已提交
1328
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1329

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

P
panqiangbiao 已提交
1332 1333
**参数:**

H
HelloCrease 已提交
1334 1335 1336
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1337 1338 1339

**示例:**

1340
```js
P
panqiangbiao 已提交
1341
async function example() {
Z
zhang-daiyue 已提交
1342
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1343 1344 1345 1346
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1347 1348
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1349 1350 1351 1352
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
P
panqiangbiao 已提交
1353
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
1354 1355
    });
}
P
panqiangbiao 已提交
1356 1357
```

P
panqiangbiao 已提交
1358
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1359

P
panqiangbiao 已提交
1360
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1361 1362 1363

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

P
panqiangbiao 已提交
1364
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1365

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

P
panqiangbiao 已提交
1368 1369
**参数:**

H
HelloCrease 已提交
1370 1371 1372 1373
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1374 1375 1376

**示例:**

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

P
panqiangbiao 已提交
1396
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1397

P
panqiangbiao 已提交
1398
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1399 1400 1401

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

P
panqiangbiao 已提交
1402
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1403

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

P
panqiangbiao 已提交
1406 1407
**参数:**

H
HelloCrease 已提交
1408 1409 1410
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1411 1412 1413

**返回值:**

H
HelloCrease 已提交
1414 1415
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1416
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1417 1418 1419

**示例:**

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

P
panqiangbiao 已提交
1443
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1444

P
panqiangbiao 已提交
1445
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1446 1447 1448

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

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

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

P
panqiangbiao 已提交
1453 1454
**参数:**

H
HelloCrease 已提交
1455 1456 1457 1458
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1459 1460 1461

**示例:**

1462
```js
P
panqiangbiao 已提交
1463
async function example() {
Z
zhang-daiyue 已提交
1464
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1465 1466 1467 1468
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1469 1470
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1471 1472 1473 1474 1475 1476 1477
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
1478 1479
```

P
panqiangbiao 已提交
1480
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1481

P
panqiangbiao 已提交
1482
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1483 1484 1485

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

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

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

P
panqiangbiao 已提交
1490 1491
**参数:**

H
HelloCrease 已提交
1492 1493 1494
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1495 1496 1497

**返回值:**

H
HelloCrease 已提交
1498 1499
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1500
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1501 1502 1503

**示例:**

1504
```js
P
panqiangbiao 已提交
1505
async function example() {
Z
zhang-daiyue 已提交
1506
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1507 1508 1509 1510
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1511 1512
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521
    };
    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 已提交
1522 1523
```

P
panqiangbiao 已提交
1524
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1525

P
panqiangbiao 已提交
1526
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1527 1528 1529

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

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

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

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

H
HelloCrease 已提交
1536 1537 1538
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1539 1540 1541

**示例:**

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

P
panqiangbiao 已提交
1564
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1565

P
panqiangbiao 已提交
1566
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1567 1568 1569

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

P
panqiangbiao 已提交
1570
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1571

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

P
panqiangbiao 已提交
1574 1575
**返回值:**

H
HelloCrease 已提交
1576 1577
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1578
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1579 1580 1581

**示例:**

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

P
panqiangbiao 已提交
1602
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1603

A
AOL 已提交
1604
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1605 1606 1607

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

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

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

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

P
panqiangbiao 已提交
1614 1615
**参数:**

H
HelloCrease 已提交
1616 1617 1618 1619
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1620 1621 1622

**示例:**

1623
```js
P
panqiangbiao 已提交
1624
async function example() {
Z
zhang-daiyue 已提交
1625
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1626 1627 1628 1629
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1630 1631
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1632 1633 1634 1635 1636
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1637
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1638
    }
P
panqiangbiao 已提交
1639
}
P
panqiangbiao 已提交
1640 1641
```

P
panqiangbiao 已提交
1642
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1643

A
AOL 已提交
1644
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1645 1646 1647

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

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

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

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

P
panqiangbiao 已提交
1654 1655
**参数:**

H
HelloCrease 已提交
1656 1657 1658
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1659 1660 1661

**返回值:**

H
HelloCrease 已提交
1662 1663
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1664
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1665 1666 1667

**示例:**

1668
```js
P
panqiangbiao 已提交
1669
async function example() {
Z
zhang-daiyue 已提交
1670
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1671 1672 1673 1674
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1675 1676
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685
    };
    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 已提交
1686 1687
```

P
panqiangbiao 已提交
1688
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1689

P
panqiangbiao 已提交
1690
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1691 1692 1693

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

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

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

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

H
HelloCrease 已提交
1700 1701 1702
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1703 1704 1705

**示例:**

1706
```js
P
panqiangbiao 已提交
1707
async function example() {
Z
zhang-daiyue 已提交
1708
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1709 1710 1711 1712
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1713 1714
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1715 1716 1717 1718 1719 1720
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash(isTrashCallBack);
    function isTrashCallBack(err, isTrash) {
            if (isTrash == true) {
P
panqiangbiao 已提交
1721
                console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK isTrash = ' + isTrash);
Z
zhang-daiyue 已提交
1722
                asset.trash(true, istrashCallBack);
P
panqiangbiao 已提交
1723 1724

            } else {
P
panqiangbiao 已提交
1725 1726
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');
P
panqiangbiao 已提交
1727 1728

            }
P
panqiangbiao 已提交
1729
    }
P
panqiangbiao 已提交
1730
}
P
panqiangbiao 已提交
1731 1732
```

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

P
panqiangbiao 已提交
1735
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1736

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

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

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

P
panqiangbiao 已提交
1743 1744
**返回值:**

H
HelloCrease 已提交
1745 1746
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1747
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1748 1749 1750

**示例:**

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

Z
zengyawen 已提交
1771
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1772 1773 1774

文件检索结果集。

Z
zengyawen 已提交
1775
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1776

P
panqiangbiao 已提交
1777
getCount(): number
P
panqiangbiao 已提交
1778 1779 1780

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

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

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

H
HelloCrease 已提交
1785 1786
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1787
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1788 1789 1790

**示例**

1791
```js
P
panqiangbiao 已提交
1792
async function example() {
Z
zhang-daiyue 已提交
1793 1794
    let fileKeyObj = mediaLibrary.FileKey
    let fileType = mediaLibrary.MediaType.FILE;
P
panqiangbiao 已提交
1795 1796 1797
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1798 1799
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1800 1801 1802 1803
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1804 1805
```

Z
zengyawen 已提交
1806
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1807

P
panqiangbiao 已提交
1808
isAfterLast(): boolean
P
panqiangbiao 已提交
1809 1810 1811

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

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

P
panqiangbiao 已提交
1814 1815
**返回值**

H
HelloCrease 已提交
1816 1817
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1818
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1819 1820 1821

**示例**

1822
```js
P
panqiangbiao 已提交
1823
async function example() {
Z
zhang-daiyue 已提交
1824
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1825 1826 1827 1828
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1829 1830
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1831 1832 1833
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1834
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1835 1836 1837 1838
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1839
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1840
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1841 1842
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1843
              fetchFileResult.close();
P
panqiangbiao 已提交
1844

P
panqiangbiao 已提交
1845 1846
            }
    }
P
panqiangbiao 已提交
1847
}
P
panqiangbiao 已提交
1848 1849
```

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

P
panqiangbiao 已提交
1852
close(): void
P
panqiangbiao 已提交
1853 1854 1855

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

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

P
panqiangbiao 已提交
1858 1859
**示例**

1860
```js
P
panqiangbiao 已提交
1861
async function example() {
Z
zhang-daiyue 已提交
1862
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1863 1864 1865 1866
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1867 1868
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1869 1870 1871 1872
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1873 1874
```

Z
zengyawen 已提交
1875
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1876

P
panqiangbiao 已提交
1877
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1878 1879 1880

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

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

P
panqiangbiao 已提交
1883 1884
**参数**

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

**示例**

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

Z
zengyawen 已提交
1912
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1913

P
panqiangbiao 已提交
1914
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1915

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

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

P
panqiangbiao 已提交
1920 1921
**返回值**

Z
zengyawen 已提交
1922 1923
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1924
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1925 1926 1927

**示例**

1928
```js
P
panqiangbiao 已提交
1929
async function example() {
Z
zhang-daiyue 已提交
1930
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1931 1932 1933 1934
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1935 1936
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1937 1938 1939 1940 1941 1942 1943 1944
    };
    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 已提交
1945 1946
```

Z
zengyawen 已提交
1947
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1948

P
panqiangbiao 已提交
1949
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1950 1951 1952

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

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

P
panqiangbiao 已提交
1955 1956
**参数**

Z
zengyawen 已提交
1957 1958 1959
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1960 1961 1962

**示例**

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

Z
zengyawen 已提交
1984
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1985

P
panqiangbiao 已提交
1986
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1987 1988 1989

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

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

P
panqiangbiao 已提交
1992 1993
**返回值**

Z
zengyawen 已提交
1994 1995 1996
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1997 1998 1999

**示例**

2000
```js
P
panqiangbiao 已提交
2001
async function example() {
Z
zhang-daiyue 已提交
2002
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
2003 2004 2005 2006
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2007 2008
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2009 2010 2011
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
2012
    console.info('mediaLibraryTest : count:' + fetchCount);
Z
zhang-daiyue 已提交
2013
    let fileAsset = await fetchFileResult.getNextObject();
P
panqiangbiao 已提交
2014
}
P
panqiangbiao 已提交
2015 2016
```

Z
zengyawen 已提交
2017
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2018

P
panqiangbiao 已提交
2019
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2020 2021 2022

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

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

P
panqiangbiao 已提交
2025 2026
**参数**

Z
zengyawen 已提交
2027 2028
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
Z
zengyawen 已提交
2029
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2030 2031 2032

**示例**

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

Z
zengyawen 已提交
2054
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2055

P
panqiangbiao 已提交
2056
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2057 2058 2059

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

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

P
panqiangbiao 已提交
2062 2063
**返回值**

Z
zengyawen 已提交
2064 2065 2066
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2067 2068 2069

**示例**

2070
```js
P
panqiangbiao 已提交
2071
async function example() {
Z
zhang-daiyue 已提交
2072
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
2073 2074 2075 2076
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2077 2078
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2079 2080 2081 2082
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
2083 2084
```

Z
zengyawen 已提交
2085
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2086

P
panqiangbiao 已提交
2087
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2088 2089 2090

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

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

P
panqiangbiao 已提交
2093 2094
**参数**

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

**示例**

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

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

P
panqiangbiao 已提交
2125
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2126 2127 2128

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

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

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

Z
zengyawen 已提交
2133
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
2134 2135
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
2136 2137 2138

**返回值**

Z
zengyawen 已提交
2139 2140 2141
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2142 2143 2144

**示例**

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

Z
zengyawen 已提交
2164
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2165

P
panqiangbiao 已提交
2166
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2167 2168 2169

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

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

P
panqiangbiao 已提交
2172 2173
**参数**

Z
zengyawen 已提交
2174
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
2175
| -------- | ---------------------------------------- | ---- | -------------------- |
Z
zengyawen 已提交
2176
| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
2177 2178 2179

**示例**

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

Z
zengyawen 已提交
2201
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2202

P
panqiangbiao 已提交
2203
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2204 2205 2206

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

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

P
panqiangbiao 已提交
2209 2210
**返回值**

Z
zengyawen 已提交
2211 2212 2213
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2214 2215 2216

**示例**

2217
```js
P
panqiangbiao 已提交
2218
async function example() {
Z
zhang-daiyue 已提交
2219
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
2220 2221 2222 2223
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2224 2225
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2226 2227 2228 2229
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
P
panqiangbiao 已提交
2230 2231
```

Z
zengyawen 已提交
2232
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2233 2234 2235

实体相册

Z
zengyawen 已提交
2236
### 属性
P
panqiangbiao 已提交
2237

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

Z
zengyawen 已提交
2240
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2241
| ------------ | ------ | ---- | ---- | ------- |
Z
zengyawen 已提交
2242 2243 2244
| albumId | number | 是    | 否    | 相册编号    |
| albumName | string | 是    | 是    | 相册名称    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri   |
H
HelloCrease 已提交
2245
| dateModified | number | 是    | 否    | 修改日期    |
Z
zengyawen 已提交
2246 2247 2248
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
2249

P
panqiangbiao 已提交
2250 2251 2252
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2253 2254 2255

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

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

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

P
panqiangbiao 已提交
2260 2261
**参数**

Z
zengyawen 已提交
2262 2263 2264
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2265 2266 2267

**示例**

2268
```js
P
panqiangbiao 已提交
2269
async function example() {
P
panqiangbiao 已提交
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
    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 已提交
2285 2286
```

P
panqiangbiao 已提交
2287
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2288

P
panqiangbiao 已提交
2289
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2290 2291 2292

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

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

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

P
panqiangbiao 已提交
2297 2298
**返回值**

H
HelloCrease 已提交
2299 2300
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2301 2302 2303 2304
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2305
```js
P
panqiangbiao 已提交
2306
async function example() {
P
panqiangbiao 已提交
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
    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 已提交
2320 2321
```

Z
zengyawen 已提交
2322
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2323

P
panqiangbiao 已提交
2324
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2325 2326 2327

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

P
panqiangbiao 已提交
2328
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2329

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

P
panqiangbiao 已提交
2332 2333
**参数**

Z
zengyawen 已提交
2334
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2335
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2336 2337
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2338 2339 2340

**示例**

2341
```js
P
panqiangbiao 已提交
2342
async function example() {
P
panqiangbiao 已提交
2343 2344 2345 2346
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2347 2348 2349 2350
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
P
panqiangbiao 已提交
2351 2352 2353 2354 2355 2356
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
P
panqiangbiao 已提交
2357 2358 2359
}
```

Z
zengyawen 已提交
2360
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2361

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

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

P
panqiangbiao 已提交
2366
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2367

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

**参数**

Z
zengyawen 已提交
2372
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2373
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2374
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2375 2376 2377

**返回值**

Z
zengyawen 已提交
2378 2379
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2380
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2381 2382 2383

**示例**

2384
```js
P
panqiangbiao 已提交
2385
async function example() {
P
panqiangbiao 已提交
2386 2387 2388 2389
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2390 2391 2392 2393
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
P
panqiangbiao 已提交
2394 2395 2396 2397 2398 2399 2400 2401
    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 已提交
2402 2403
```

P
panqiangbiao 已提交
2404
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2405

P
panqiangbiao 已提交
2406
注册设备的信息。
2407 2408

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

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

Z
zengyawen 已提交
2412 2413 2414 2415 2416 2417
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2418 2419 2420



Z
zengyawen 已提交
2421
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2422 2423 2424

枚举,媒体类型。

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

潘强标 已提交
2427 2428 2429 2430 2431 2432
| 名称  |  说明 |
| ----- |  ---- |
| FILE  |  文件 |
| IMAGE |  图片 |
| VIDEO |  视频 |
| AUDIO |  音频 |
P
panqiangbiao 已提交
2433

Z
zengyawen 已提交
2434
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2435 2436 2437

枚举,文件关键信息。

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

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

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

枚举,目录类型。

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

潘强标 已提交
2468 2469 2470 2471 2472 2473 2474 2475
| 名称          |  说明               |
| ------------- |  ------------------ |
| DIR_CAMERA    |  表示Camera文件路径 |
| DIR_VIDEO     |  表示视频路径       |
| DIR_IMAGE     |  表示图片路径       |
| DIR_AUDIO     |  表示音频路径       |
| DIR_DOCUMENTS |  表示文档路径       |
| DIR_DOWNLOAD  |  表示下载路径       |
P
panqiangbiao 已提交
2476

Z
zengyawen 已提交
2477
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2478 2479

枚举,设备类型。
2480 2481

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

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

潘强标 已提交
2485 2486 2487 2488 2489 2490 2491 2492 2493
| 名称         |  说明       |
| ------------ |  ---------- |
| TYPE_UNKNOWN |  未识别设备 |
| TYPE_LAPTOP  |  笔记本电脑 |
| TYPE_PHONE   |  手机       |
| TYPE_TABLET  |  平板电脑   |
| TYPE_WATCH   |  智能手表   |
| TYPE_CAR     |  车载设备   |
| TYPE_TV      |  电视设备   |
P
panqiangbiao 已提交
2494

Z
zengyawen 已提交
2495
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2496 2497 2498

检索条件。

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

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

P
panqiangbiao 已提交
2510
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2511 2512

图片尺寸。
2513 2514

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

H
HelloCrease 已提交
2516 2517 2518 2519
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2520

Z
update  
zengyawen 已提交
2521
## MediaAssetOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2522 2523 2524

媒体资源选项。

Z
update  
zengyawen 已提交
2525
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2526 2527 2528 2529

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


Z
zengyawen 已提交
2530 2531 2532 2533 2534
| 名称         | 类型   | 必填 | 描述                                                         |
| ------------ | ------ | ---- | ------------------------------------------------------------ |
| 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 已提交
2535

Z
update  
zengyawen 已提交
2536
## MediaSelectOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2537 2538 2539

媒体资源类型选项。

Z
update  
zengyawen 已提交
2540
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2541 2542 2543

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

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

2549