js-apis-medialibrary.md 98.1 KB
Newer Older
Z
zengyawen 已提交
1
# @ohos.multimedia.medialibrary (媒体库管理)
Z
zengyawen 已提交
2

zyjhandsome's avatar
zyjhandsome 已提交
3
> **说明:**
H
huweiqi 已提交
4 5 6 7
> - 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
> - 从API Version 9开始废弃。保留至API Version 13版本。
> - 部分功能变更为系统接口,仅供系统应用使用,请使用[@ohos.filemanagement.userFileManager](js-apis-userFileManager.md)相应接口替代。
> - 媒体资源选择和保存功能仍开放给普通应用,请使用[@ohos.file.picker](js-apis-file-picker.md)相应接口替代。
P
panqiangbiao 已提交
8

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

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

P
panqiangbiao 已提交
16
getMediaLibrary(context: Context): MediaLibrary
P
panqiangbiao 已提交
17

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

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

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

P
panqiangbiao 已提交
24
**参数:** 
P
panqiangbiao 已提交
25

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

**返回值:**

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

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

38
```ts
H
huweiqi 已提交
39
// 获取mediaLibrary实例,后续用到此实例均采用此处获取的实例
40 41
const context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
潘强标 已提交
42 43 44
```

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

46
```js
P
panqiangbiao 已提交
47
import featureAbility from '@ohos.ability.featureAbility';
P
panqiangbiao 已提交
48

49
let context = featureAbility.getContext();
50
let media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
51
```
52

Z
zengyawen 已提交
53 54 55 56 57 58
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

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

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

Z
zengyawen 已提交
61 62 63 64 65 66 67 68 69 70 71
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**返回值:**

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

**示例:**

```js
72
let media = mediaLibrary.getMediaLibrary();
Z
zengyawen 已提交
73 74
```

P
panqiangbiao 已提交
75
## MediaLibrary
P
panqiangbiao 已提交
76

Z
zengyawen 已提交
77
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
78 79


P
panqiangbiao 已提交
80
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 
P
panqiangbiao 已提交
81 82 83

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

P
panqiangbiao 已提交
84
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
85

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

P
panqiangbiao 已提交
88 89
**参数:**

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

**示例:**

97
```js
H
huweiqi 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
async function example() {
    let fileKeyObj = mediaLibrary.FileKey;
    let imageType = mediaLibrary.MediaType.IMAGE;
    // 创建文件获取选项,此处参数为获取image类型的文件资源
    let imagesFetchOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
    };
    // 获取文件资源,使用callback方式返回异步结果
    media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
        // 判断获取的文件资源的检索结果集是否为undefined,若为undefined则接口调用失败
        if (fetchFileResult == undefined) {
            console.error('get fetchFileResult failed with error: ' + error);
Z
zhang-daiyue 已提交
111 112
            return;
        }
H
huweiqi 已提交
113 114 115 116 117 118
        // 获取文件检索结果集中的总数
        const count = fetchFileResult.getCount();
        // 判断结果集中的数量是否小于0,小于0时表示接口调用失败
        if (count < 0) {
            console.error('get count from fetchFileResult failed, count: ' + count);
            return;
Z
zhang-daiyue 已提交
119
        }
H
huweiqi 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        // 判断结果集中的数量是否等于0,等于0时表示接口调用成功,但是检索结果集为空,请检查文件获取选项参数配置是否有误和设备中是否存在相应文件
        if (count == 0) {
            console.info('The count of fetchFileResult is zero');
            return;
        }
        console.info('Get fetchFileResult successfully, count: ' + count);
        // 获取文件检索结果集中的第一个资源,使用callback方式返回异步结果
        fetchFileResult.getFirstObject((error, fileAsset) => {
            // 检查获取的第一个资源是否为undefined,若为undefined则接口调用失败
            if (fileAsset == undefined) {
                console.error('get first object failed with error: ' + error);
                return;
            }
            console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
            // 调用 getNextObject 接口获取下一个资源,直到最后一个
            for (let i = 1; i < count; i++) {
H
huweiqi 已提交
136 137
                let fileAsset = await fetchFileResult.getNextObject();
                console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
H
huweiqi 已提交
138
            }
H
huweiqi 已提交
139 140
            // 释放FetchFileResult实例并使其失效。无法调用其他方法
            fetchFileResult.close();
H
huweiqi 已提交
141
        });
Z
zhang-daiyue 已提交
142
    });
H
huweiqi 已提交
143
}
P
panqiangbiao 已提交
144
```
H
huweiqi 已提交
145

Z
zengyawen 已提交
146
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
147

P
panqiangbiao 已提交
148
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
149 150 151

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

P
panqiangbiao 已提交
152
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
153

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

P
panqiangbiao 已提交
156 157
**参数:**

Z
zengyawen 已提交
158 159 160
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 文件检索选项 |
P
panqiangbiao 已提交
161 162 163

**返回值**

Z
zengyawen 已提交
164 165 166
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | 文件数据结果集 |
P
panqiangbiao 已提交
167 168 169

**示例:**

170
```js
H
huweiqi 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
async function example() {
    let fileKeyObj = mediaLibrary.FileKey;
    let imageType = mediaLibrary.MediaType.IMAGE;
    // 创建文件获取选项,此处参数为获取image类型的文件资源
    let imagesFetchOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
    };
    // 获取文件资源,使用Promise方式返回结果
    media.getFileAssets(imagesFetchOp).then((fetchFileResult) => {
        // 获取文件检索结果集中的总数
        const count = fetchFileResult.getCount();
        // 判断结果集中的数量是否小于0,小于0时表示接口调用失败
        if (count < 0) {
            console.error('get count from fetchFileResult failed, count: ' + count);
            return;
Z
zhang-daiyue 已提交
187
        }
H
huweiqi 已提交
188 189 190 191 192 193 194 195 196 197 198
        // 判断结果集中的数量是否等于0,等于0时表示接口调用成功,但是检索结果集为空,请检查文件获取选项参数配置是否有误和设备中是否存在相应文件
        if (count == 0) {
            console.info('The count of fetchFileResult is zero');
            return;
        }
        console.info('Get fetchFileResult successfully, count: ' + count);
        // 获取文件检索结果集中的第一个资源,使用Promise方式返回异步结果
        fetchFileResult.getFirstObject().then((fileAsset) => {
            console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
            // 调用 getNextObject 接口获取下一个资源,直到最后一个
            for (let i = 1; i < count; i++) {
H
huweiqi 已提交
199 200
                let fileAsset = await fetchFileResult.getNextObject();
                console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
H
huweiqi 已提交
201
            }
H
huweiqi 已提交
202 203
            // 释放FetchFileResult实例并使其失效。无法调用其他方法
            fetchFileResult.close();
H
huweiqi 已提交
204 205 206 207 208 209 210
        }).catch((error) => {
            // 调用getFirstObject接口失败
            console.error('get first object failed with error: ' + error);
        });
    }).catch((error) => {
        // 调用getFileAssets接口失败
        console.error('get file assets failed with error: ' + error);
Z
zhang-daiyue 已提交
211
    });
H
huweiqi 已提交
212
}
P
panqiangbiao 已提交
213 214
```

P
panqiangbiao 已提交
215
### on<sup>8+</sup>
P
panqiangbiao 已提交
216

217
on(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
218

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

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

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

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

**示例:**

232
```js
Z
zhang-daiyue 已提交
233
media.on('imageChange', () => {
P
panqiangbiao 已提交
234
    // image file had changed, do something
P
panqiangbiao 已提交
235 236
})
```
P
panqiangbiao 已提交
237
### off<sup>8+</sup>
P
panqiangbiao 已提交
238

239
off(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback?: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
240

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

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

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

H
HelloCrease 已提交
247 248
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
249
| type     | 'deviceChange'&#124;<br/>'albumChange'&#124;<br/>'imageChange'&#124;<br/>'audioChange'&#124;<br/>'videoChange'&#124;<br/>'fileChange'&#124;<br/>'remoteFileChange'               | 是    | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'fileChange':     &nbsp;文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更 |
H
huweiqi 已提交
250
| callback | Callback&lt;void&gt; | 否    | 回调返回空                                    |
P
panqiangbiao 已提交
251 252 253

**示例:**

254
```js
潘强标 已提交
255
media.off('imageChange', () => {
H
huweiqi 已提交
256
    // stop listening successfully
P
panqiangbiao 已提交
257 258 259
})
```

B
bmeangel 已提交
260
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
261

P
panqiangbiao 已提交
262
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
263 264 265

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

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

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

P
panqiangbiao 已提交
270 271
**参数:**

Z
zengyawen 已提交
272 273 274 275 276 277
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型                                                     |
| displayName  | string                                  | 是   | 展示文件名                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
278 279 280

**示例:**

281
```js
P
panqiangbiao 已提交
282 283 284 285 286
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
H
huweiqi 已提交
287
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (error, fileAsset) => {
P
panqiangbiao 已提交
288
        if (fileAsset != undefined) {
H
huweiqi 已提交
289
            console.info('createAsset successfully, message');
P
panqiangbiao 已提交
290
        } else {
H
huweiqi 已提交
291
            console.error('createAsset failed with error: ' + error);
P
panqiangbiao 已提交
292 293 294
        }
    });
}
P
panqiangbiao 已提交
295 296
```

P
panqiangbiao 已提交
297
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
298

P
panqiangbiao 已提交
299
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
300 301 302

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

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

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

P
panqiangbiao 已提交
307 308
**参数:**

Z
zengyawen 已提交
309 310 311 312 313
| 参数名       | 类型                     | 必填 | 说明                                                         |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | 是   | 媒体类型                                                     |
| displayName  | string                   | 是   | 展示文件名                                                   |
| relativePath | string                   | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
314 315 316

**返回值**

Z
zengyawen 已提交
317 318 319
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | 媒体数据FileAsset |
P
panqiangbiao 已提交
320 321 322

**示例:**

323
```js
H
huweiqi 已提交
324 325 326 327 328 329 330
async function example() {
    // 使用Promise方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
    media.createAsset(mediaType, 'imagePromise.jpg', path + 'myPicture/').then((fileAsset) => {
        console.info('createAsset successfully, message = ' + JSON.stringify(fileAsset));
H
huweiqi 已提交
331 332
    }).catch((error) => {
        console.error('createAsset failed with error: ' + error);
H
huweiqi 已提交
333 334
    });
}
P
panqiangbiao 已提交
335 336
```

Z
zengyawen 已提交
337 338 339 340 341 342 343 344
### deleteAsset<sup>8+</sup>

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

删除媒体文件资源

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

345
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363

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

**参数:**

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

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

**示例:**

```js
async function example() {
364 365
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
366 367 368 369 370 371 372
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
H
huweiqi 已提交
373 374
        console.error('asset not exist');
        return;
Z
zengyawen 已提交
375 376
    }
    media.deleteAsset(asset.uri).then(() => {
H
huweiqi 已提交
377 378 379
        console.info('deleteAsset successfully');
    }).catch((error) => {
        console.error('deleteAsset failed with error: ' + error);
Z
zengyawen 已提交
380
    });
H
huweiqi 已提交
381
    fetchFileResult.close();
Z
zengyawen 已提交
382 383 384 385 386 387 388 389 390 391
}
```

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

删除媒体文件资源

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

392
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406

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

**参数:**

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

**示例:**

```js
async function example() {
407 408
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
409 410 411 412 413 414 415
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
H
huweiqi 已提交
416 417
        console.error('asset not exist');
        return;
Z
zengyawen 已提交
418
    }
H
huweiqi 已提交
419 420 421
    media.deleteAsset(asset.uri, (error) => {
        if (error != undefined) {
            console.error('deleteAsset failed with error: ' + error);
Z
zengyawen 已提交
422
        } else {
H
huweiqi 已提交
423
            console.info('deleteAsset successfully');
Z
zengyawen 已提交
424 425
        }
    });
H
huweiqi 已提交
426
    fetchFileResult.close();
Z
zengyawen 已提交
427 428 429
}
```

P
panqiangbiao 已提交
430
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
431

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

P
panqiangbiao 已提交
434
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
435 436 437 438 439

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

**参数:**

Z
zengyawen 已提交
440 441 442 443
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback 返回公共目录路径 |
P
panqiangbiao 已提交
444 445 446

**示例:**

447
```js
P
panqiangbiao 已提交
448
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
H
huweiqi 已提交
449
media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => {
P
panqiangbiao 已提交
450
    if (dicResult == 'Camera/') {
H
huweiqi 已提交
451
        console.info('getPublicDirectory DIR_CAMERA successfully');
P
panqiangbiao 已提交
452
    } else {
H
huweiqi 已提交
453
        console.error('getPublicDirectory DIR_CAMERA failed with error: ' + error);
P
panqiangbiao 已提交
454 455 456 457
    }
});
```

P
panqiangbiao 已提交
458
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
459

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

P
panqiangbiao 已提交
462
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
463 464 465 466 467

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

**参数:**

Z
zengyawen 已提交
468 469 470
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
471 472 473

**返回值:**

Z
zengyawen 已提交
474 475 476
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
477 478 479

**示例:**

480
```js
P
panqiangbiao 已提交
481
async function example() {
P
panqiangbiao 已提交
482
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
H
huweiqi 已提交
483 484 485 486 487 488 489 490 491
    media.getPublicDirectory(DIR_CAMERA).then((dicResult) => {
        if (dicResult == 'Camera/') {
            console.info('getPublicDirectory DIR_CAMERA successfully');
        } else {
            console.error('getPublicDirectory DIR_CAMERA failed');
        }
    }).catch((error) => {
        console.error('getPublicDirectory failed with error: ' + error);
    });
P
panqiangbiao 已提交
492 493 494
}
```

Z
zengyawen 已提交
495
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
496

H
huweiqi 已提交
497
getAlbums(options: MediaFetchOptions, callback: AsyncCallback&lt;Array&lt;Album&gt;&gt;): void
P
panqiangbiao 已提交
498

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

P
panqiangbiao 已提交
501
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
502

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

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

Z
zengyawen 已提交
507 508 509 510
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册获取条件                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
511 512 513

**示例:**

514
```js
H
huweiqi 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => {
        if (albumList != undefined) {
            console.info('getAlbums successfully: ' + JSON.stringify(albumList));
        } else {
            console.error('getAlbums failed with error: ' + error);
        }
    })
}
P
panqiangbiao 已提交
528 529
```

Z
zengyawen 已提交
530
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
531

H
huweiqi 已提交
532
getAlbums(options: MediaFetchOptions): Promise&lt;Array&lt;Album&gt;&gt;
P
panqiangbiao 已提交
533

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

P
panqiangbiao 已提交
536
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
537

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

P
panqiangbiao 已提交
540 541
**参数:**

Z
zengyawen 已提交
542 543 544
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
545 546 547

**返回值:**

Z
zengyawen 已提交
548 549 550
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
551 552 553

**示例:**

554
```js
H
huweiqi 已提交
555 556 557 558 559 560 561 562 563 564 565
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
        console.info('getAlbums successfully: ' + JSON.stringify(albumList));
    }).catch((error) => {
        console.error('getAlbums failed with error: ' + error);
    });
}
P
panqiangbiao 已提交
566 567
```

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

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

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

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

P
panqiangbiao 已提交
577 578
**参数:**

H
HelloCrease 已提交
579 580
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
581
| callback | AsyncCallback&lt;void&gt; | 是    | 无返回值 |
P
panqiangbiao 已提交
582 583 584

**示例:**

585
```js
H
huweiqi 已提交
586
media.release(() => {
P
panqiangbiao 已提交
587
    // do something
P
panqiangbiao 已提交
588
});
P
panqiangbiao 已提交
589 590
```

P
panqiangbiao 已提交
591
### release<sup>8+</sup>
P
panqiangbiao 已提交
592

P
panqiangbiao 已提交
593
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
594

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

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

P
panqiangbiao 已提交
600 601
**返回值:**

H
HelloCrease 已提交
602 603
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
604
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
605 606 607

**示例:**

608
```js
P
panqiangbiao 已提交
609
media.release()
P
panqiangbiao 已提交
610 611
```

H
huweiqi 已提交
612
### storeMediaAsset
P
panqiangbiao 已提交
613 614 615 616 617

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

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

H
huweiqi 已提交
618
> **说明**:此接口为API Version 6开始支持,只支持FA模型使用。
P
panqiangbiao 已提交
619 620 621 622 623

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

**参数:**

H
HelloCrease 已提交
624 625 626 627
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
| callback | AsyncCallback&lt;string&gt;           | 是    | 媒体资源保存回调,返回保存成功后得到的URI。 |
P
panqiangbiao 已提交
628 629 630

**示例:**

631
```js
P
panqiangbiao 已提交
632
let option = {
H
huweiqi 已提交
633 634 635
    src : '/data/storage/el2/base/haps/entry/image.png',
    mimeType : 'image/*',
    relativePath : 'Pictures/'
P
panqiangbiao 已提交
636
};
H
huweiqi 已提交
637 638 639
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (error, value) => {
    if (error) {
        console.error('storeMediaAsset failed with error: ' + error);
P
panqiangbiao 已提交
640 641
        return;
    }
H
huweiqi 已提交
642
    console.info('Media resources stored. ');
P
panqiangbiao 已提交
643 644
    // Obtain the URI that stores media resources.
});
645
```
P
panqiangbiao 已提交
646 647


H
huweiqi 已提交
648
### storeMediaAsset
P
panqiangbiao 已提交
649 650 651 652 653

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

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

H
huweiqi 已提交
654
> **说明**:此接口为API Version 6开始支持,只支持FA模型使用。
P
panqiangbiao 已提交
655 656 657 658 659

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

**参数:**

H
HelloCrease 已提交
660 661 662
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
663 664 665

**返回值:**

H
HelloCrease 已提交
666 667
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
668 669 670 671
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

672
```js
P
panqiangbiao 已提交
673
let option = {
H
huweiqi 已提交
674 675 676
    src : '/data/storage/el2/base/haps/entry/image.png',
    mimeType : 'image/*',
    relativePath : 'Pictures/'
P
panqiangbiao 已提交
677 678
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
H
huweiqi 已提交
679
    console.info('Media resources stored.');
P
panqiangbiao 已提交
680
    // Obtain the URI that stores media resources.
H
huweiqi 已提交
681 682
}).catch((error) => {
    console.error('storeMediaAsset failed with error: ' + error);
P
panqiangbiao 已提交
683
});
684
```
P
panqiangbiao 已提交
685 686


H
huweiqi 已提交
687
### startImagePreview
P
panqiangbiao 已提交
688 689 690

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

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

H
huweiqi 已提交
693 694 695
> **说明**: 
> 此接口为API Version 6开始支持,只支持FA模型使用。
> 建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
696 697 698 699 700

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

**参数:**

H
HelloCrease 已提交
701 702
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
703
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI('https://','datashare://')列表。 |
H
HelloCrease 已提交
704 705
| index    | number                    | 是    | 开始显示的图片序号。                               |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
706 707 708

**示例:**

709
```js
P
panqiangbiao 已提交
710
let images = [
H
huweiqi 已提交
711 712
    'datashare:///media/xxxx/2',
    'datashare:///media/xxxx/3'
P
panqiangbiao 已提交
713
];
H
HelloCrease 已提交
714
/* 网络图片使用方式
P
panqiangbiao 已提交
715
let images = [
H
huweiqi 已提交
716 717
    'https://media.xxxx.com/image1.jpg',
    'https://media.xxxx.com/image2.jpg'
P
panqiangbiao 已提交
718
];
H
HelloCrease 已提交
719
*/
P
panqiangbiao 已提交
720
let index = 1;
H
huweiqi 已提交
721 722 723
mediaLibrary.getMediaLibrary().startImagePreview(images, index, (error) => {
    if (error) {
        console.error('startImagePreview failed with error: ' + error);
P
panqiangbiao 已提交
724 725
        return;
    }
H
huweiqi 已提交
726
    console.info('Succeeded in previewing the images.');
P
panqiangbiao 已提交
727
});
728
```
P
panqiangbiao 已提交
729 730


H
huweiqi 已提交
731
### startImagePreview
P
panqiangbiao 已提交
732 733 734

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

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

H
huweiqi 已提交
737 738 739
> **说明**: 
> 此接口为API Version 6开始支持,只支持FA模型使用。
> 建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
740 741 742 743 744

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

**参数:**

H
HelloCrease 已提交
745 746
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
747
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI('https://','datashare://')列表。 |
H
HelloCrease 已提交
748
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
749 750 751

**示例:**

752
```js
P
panqiangbiao 已提交
753
let images = [
H
huweiqi 已提交
754 755
    'datashare:///media/xxxx/2',
    'datashare:///media/xxxx/3'
P
panqiangbiao 已提交
756
];
H
HelloCrease 已提交
757
/* 网络图片使用方式
P
panqiangbiao 已提交
758
let images = [
H
huweiqi 已提交
759 760
    'https://media.xxxx.com/image1.jpg',
    'https://media.xxxx.com/image2.jpg'
P
panqiangbiao 已提交
761
];
H
HelloCrease 已提交
762
*/
H
huweiqi 已提交
763 764 765
mediaLibrary.getMediaLibrary().startImagePreview(images, (error) => {
    if (error) {
        console.error('startImagePreview failed with error: ' + error);
P
panqiangbiao 已提交
766 767
        return;
    }
H
huweiqi 已提交
768
    console.info('Succeeded in previewing the images.');
P
panqiangbiao 已提交
769
});
770
```
P
panqiangbiao 已提交
771 772


H
huweiqi 已提交
773
### startImagePreview
P
panqiangbiao 已提交
774 775 776

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

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

H
huweiqi 已提交
779 780 781
> **说明**: 
> 此接口为API Version 6开始支持,只支持FA模型使用。
> 建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
782 783 784 785 786

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

**参数:**

H
HelloCrease 已提交
787 788
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
789
| images | Array&lt;string&gt; | 是    | 预览的图片URI('https://','datashare://')列表。 |
H
HelloCrease 已提交
790
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
791 792 793

**返回值:**

H
HelloCrease 已提交
794 795
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
P
panqiangbiao 已提交
796 797 798 799
| Promise&lt;void&gt; | Promise实例,用于异步获取预览结果,失败时返回错误信息。 |

**示例:**

800
```js
P
panqiangbiao 已提交
801
let images = [
H
huweiqi 已提交
802 803
    'datashare:///media/xxxx/2',
    'datashare:///media/xxxx/3'
P
panqiangbiao 已提交
804
];
H
HelloCrease 已提交
805
/* 网络图片使用方式
P
panqiangbiao 已提交
806
let images = [
H
huweiqi 已提交
807 808
    'https://media.xxxx.com/image1.jpg',
    'https://media.xxxx.com/image2.jpg'
P
panqiangbiao 已提交
809
];
H
HelloCrease 已提交
810
*/
P
panqiangbiao 已提交
811 812
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
H
huweiqi 已提交
813 814 815
    console.info('Succeeded in previewing the images.');
}).catch((error) => {
    console.error('startImagePreview failed with error: ' + error);
P
panqiangbiao 已提交
816
});
817
```
P
panqiangbiao 已提交
818 819


H
huweiqi 已提交
820
### startMediaSelect
P
panqiangbiao 已提交
821 822 823 824 825

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

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

H
huweiqi 已提交
826 827 828
> **说明**: 
> 此接口为API Version 6开始支持,只支持FA模型使用。
> 建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[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 已提交
829 830 831 832 833

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

**参数:**

H
HelloCrease 已提交
834 835
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
H
huweiqi 已提交
836
| option   | [MediaSelectOption](#mediaselectoption)  | 是    | 媒体选择选项。                              |
Z
zhang-daiyue 已提交
837
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 媒体选择回调,返回选择的媒体URI(datashare://)列表。 |
P
panqiangbiao 已提交
838 839 840

**示例:**

841
```js
Z
zhang-daiyue 已提交
842
let option : mediaLibrary.MediaSelectOption = {
H
huweiqi 已提交
843
    type : 'media',
Z
zhang-daiyue 已提交
844
    count : 2
P
panqiangbiao 已提交
845
};
H
huweiqi 已提交
846 847 848
mediaLibrary.getMediaLibrary().startMediaSelect(option, (error, value) => {
    if (error) {
        console.error('startMediaSelect failed with error: ' + error);
P
panqiangbiao 已提交
849 850
        return;
    }
H
huweiqi 已提交
851
    console.info('Media resources selected.');
P
panqiangbiao 已提交
852 853
    // Obtain the media selection value.
});
854
```
P
panqiangbiao 已提交
855 856


H
huweiqi 已提交
857
### startMediaSelect
P
panqiangbiao 已提交
858 859 860 861 862

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

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

H
huweiqi 已提交
863 864 865
> **说明**: 
> 此接口为API Version 6开始支持,只支持FA模型使用。
> 建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[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 已提交
866 867 868 869 870

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

**参数:**

H
HelloCrease 已提交
871 872
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
H
huweiqi 已提交
873
| option | [MediaSelectOption](#mediaselectoption) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
874 875 876

**返回值:**

H
HelloCrease 已提交
877 878
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
Z
zhang-daiyue 已提交
879
| Promise&lt;Array&lt;string&gt;&gt; | Promise实例,用于异步获取选择的媒体URI(datashare://)列表。 |
P
panqiangbiao 已提交
880 881 882

**示例:**

883
```js
Z
zhang-daiyue 已提交
884
let option : mediaLibrary.MediaSelectOption = {
H
huweiqi 已提交
885
    type : 'media',
Z
zhang-daiyue 已提交
886
    count : 2
P
panqiangbiao 已提交
887
};
Z
zhang-daiyue 已提交
888
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
H
huweiqi 已提交
889
    console.info('Media resources selected.');
P
panqiangbiao 已提交
890
    // Obtain the media selection value.
H
huweiqi 已提交
891 892
}).catch((error) => {
    console.error('startMediaSelect failed with error: ' + error);
P
panqiangbiao 已提交
893 894
});

Z
zengyawen 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
```
### getActivePeers<sup>8+</sup>

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

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
912
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有在线对端设备的PeerInfo |
Z
zengyawen 已提交
913 914 915 916 917 918 919

**示例:**

```js
async function example() {
    media.getActivePeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
H
huweiqi 已提交
920
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
Z
zengyawen 已提交
921
        } else {
H
huweiqi 已提交
922
            console.info('get distributed info is undefined!');
Z
zengyawen 已提交
923
        }
H
huweiqi 已提交
924 925
    }).catch((error) => {
        console.error('get distributed info failed with error: ' + error);
Z
zengyawen 已提交
926 927 928 929 930
    });
}
```

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

Z
zengyawen 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945
getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
946
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有在线对端设备的PeerInfo |
Z
zengyawen 已提交
947 948 949 950 951

**示例:**

```js
async function example() {
H
huweiqi 已提交
952
    media.getActivePeers((error, devicesInfo) => {
Z
zengyawen 已提交
953
        if (devicesInfo != undefined) {
H
huweiqi 已提交
954
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
Z
zengyawen 已提交
955
        } else {
H
huweiqi 已提交
956
            console.error('get distributed failed with error: ' + error);
Z
zengyawen 已提交
957
        }
H
huweiqi 已提交
958
    });
Z
zengyawen 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
}
```


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

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

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
979
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有对端设备的PeerInfo |
Z
zengyawen 已提交
980 981 982 983 984 985 986

**示例:**

```js
async function example() {
    media.getAllPeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
H
huweiqi 已提交
987
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
Z
zengyawen 已提交
988
        } else {
H
huweiqi 已提交
989
            console.info('get distributed info is undefined!');
Z
zengyawen 已提交
990
        }
H
huweiqi 已提交
991 992
    }).catch((error) => {
        console.error('get distributed info failed with error: ' + error);
Z
zengyawen 已提交
993 994 995 996 997
    });
}
```

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

Z
zengyawen 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
1013
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有对端设备的PeerInfo |
Z
zengyawen 已提交
1014 1015 1016 1017 1018

**示例:**

```js
async function example() {
H
huweiqi 已提交
1019
    media.getAllPeers((error, devicesInfo) => {
Z
zengyawen 已提交
1020
        if (devicesInfo != undefined) {
H
huweiqi 已提交
1021
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
Z
zengyawen 已提交
1022
        } else {
H
huweiqi 已提交
1023
            console.error('get distributed failed with error: ' + error);
Z
zengyawen 已提交
1024
        }
H
huweiqi 已提交
1025
    });
Z
zengyawen 已提交
1026
}
1027
```
P
panqiangbiao 已提交
1028

Z
zengyawen 已提交
1029
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
1030 1031 1032

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

zyjhandsome's avatar
zyjhandsome 已提交
1033
> **说明:**
H
huweiqi 已提交
1034 1035 1036
> 1. title字段默认为去掉后缀的文件名,音频和视频文件会尝试解析文件内容,部分设备写入后在触发扫描时会被还原。
> 2. orientation字段部分设备可能不支持修改,建议使用image组件的[ModifyImageProperty](js-apis-image.md#modifyimageproperty9)接口。

Z
zengyawen 已提交
1037 1038 1039
### 属性

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

Z
zengyawen 已提交
1041 1042 1043
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| id                        | number                   | 是   | 否   | 文件资源编号                                           |
Z
zhang-daiyue 已提交
1044
| uri                       | string                   | 是   | 否   | 文件资源uri(如:datashare:///media/image/2)         |
Z
zengyawen 已提交
1045 1046 1047 1048 1049 1050 1051 1052
| 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日的秒数值)         |
H
huweiqi 已提交
1053
| dateModified              | number                   | 是   | 否   | 修改日期(修改文件时间到1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)|
Z
zengyawen 已提交
1054 1055 1056 1057 1058 1059
| dateTaken                 | number                   | 是   | 否   | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)         |
| artist<sup>8+</sup>       | string                   | 是   | 否   | 作者                                                   |
| audioAlbum<sup>8+</sup>   | string                   | 是   | 否   | 专辑                                                   |
| width                     | number                   | 是   | 否   | 图片宽度(单位:像素)                                 |
| height                    | number                   | 是   | 否   | 图片高度(单位:像素)                                 |
| orientation               | number                   | 是   | 是   | 图片显示方向(顺时针旋转角度,如0,90,180  单位:度) |
潘强标 已提交
1060
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)                                   |
Z
zengyawen 已提交
1061 1062 1063
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
1064 1065 1066


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

P
panqiangbiao 已提交
1068
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1069 1070 1071

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

P
panqiangbiao 已提交
1072
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1073

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

P
panqiangbiao 已提交
1076 1077
**参数:**

H
HelloCrease 已提交
1078 1079 1080
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1081 1082 1083

**示例:**

1084
```js
P
panqiangbiao 已提交
1085
async function example() {
H
huweiqi 已提交
1086
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1087 1088
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1089 1090 1091
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1092 1093 1094
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1095 1096 1097 1098 1099 1100
    asset.isDirectory((error, isDirectory) => {
        if (error) {
            console.error('isDirectory failed with error: ' + error);
        } else {
            console.info('isDirectory result:' + isDirectory);
        }
P
panqiangbiao 已提交
1101
    });
H
huweiqi 已提交
1102
    fetchFileResult.close();
P
panqiangbiao 已提交
1103
}
P
panqiangbiao 已提交
1104 1105
```

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

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

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

P
panqiangbiao 已提交
1112
**需要权限**:ohos.permission.READ_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;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1121 1122 1123

**示例:**

1124
```js
P
panqiangbiao 已提交
1125
async function example() {
H
huweiqi 已提交
1126
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1127 1128
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1129 1130 1131
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1132 1133 1134
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1135 1136 1137 1138
    asset.isDirectory().then((isDirectory) => {
        console.info('isDirectory result:' + isDirectory);
    }).catch((error) => {
        console.error('isDirectory failed with error: ' + error);
P
panqiangbiao 已提交
1139
    });
H
huweiqi 已提交
1140
    fetchFileResult.close();
P
panqiangbiao 已提交
1141
}
P
panqiangbiao 已提交
1142 1143
```

P
panqiangbiao 已提交
1144
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1145

P
panqiangbiao 已提交
1146
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1147 1148 1149

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

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

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

P
panqiangbiao 已提交
1154 1155
**参数:**

H
HelloCrease 已提交
1156 1157 1158
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1159 1160 1161

**示例:**

1162
```js
P
panqiangbiao 已提交
1163
async function example() {
H
huweiqi 已提交
1164
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1165 1166
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1167 1168 1169
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1170 1171 1172
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1173
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1174
    asset.commitModify(() => {
H
huweiqi 已提交
1175
        console.info('commitModify successfully');   
P
panqiangbiao 已提交
1176
    });
H
huweiqi 已提交
1177
    fetchFileResult.close();
P
panqiangbiao 已提交
1178
}
P
panqiangbiao 已提交
1179 1180
```

P
panqiangbiao 已提交
1181
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1182

P
panqiangbiao 已提交
1183
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1184 1185 1186

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

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

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

P
panqiangbiao 已提交
1191 1192
**返回值:**

H
HelloCrease 已提交
1193 1194
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1195
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1196 1197 1198

**示例:**

1199
```js
P
panqiangbiao 已提交
1200
async function example() {
H
huweiqi 已提交
1201
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1202 1203
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1204 1205 1206
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1207 1208 1209
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1210
    asset.title = 'newtitle';
H
huweiqi 已提交
1211 1212
    await asset.commitModify();
    fetchFileResult.close();
P
panqiangbiao 已提交
1213
}
P
panqiangbiao 已提交
1214 1215
```

P
panqiangbiao 已提交
1216
### open<sup>8+</sup>
P
panqiangbiao 已提交
1217

P
panqiangbiao 已提交
1218
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1219 1220 1221

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

H
huweiqi 已提交
1222
**注意**:以 'w' 模式打开文件时,返回的fd无法进行读取。但由于不同文件系统实现上的差异,部分用户态文件系统在 'w' 模式打开时会允许用fd读取。若有针对fd的读写行为,建议使用 'rw' 模式打开文件。当前写操作是互斥的操作,写操作完成后需要调用close进行释放。
潘强标 已提交
1223

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

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

P
panqiangbiao 已提交
1228 1229
**参数**

H
HelloCrease 已提交
1230 1231 1232
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
1233
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件描述符                            |
P
panqiangbiao 已提交
1234 1235 1236

**示例:**

1237
```js
P
panqiangbiao 已提交
1238
async function example() {
P
panqiangbiao 已提交
1239
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1240 1241
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
H
huweiqi 已提交
1242 1243 1244 1245 1246 1247 1248
    const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
    asset.open('rw', (error, fd) => {
        if (fd > 0) {
            asset.close(fd);
        } else {
            console.error('File Open failed with error: ' + error);
        }
P
panqiangbiao 已提交
1249 1250
    });
}
P
panqiangbiao 已提交
1251 1252
```

P
panqiangbiao 已提交
1253
### open<sup>8+</sup>
P
panqiangbiao 已提交
1254

P
panqiangbiao 已提交
1255
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1256 1257 1258

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

H
huweiqi 已提交
1259
**注意**:以 'w' 模式打开文件时,返回的fd无法进行读取。但由于不同文件系统实现上的差异,部分用户态文件系统在 'w' 模式打开时会允许用fd读取。若有针对fd的读写行为,建议使用 'rw' 模式打开文件。当前写操作是互斥的操作,写操作完成后需要调用close进行释放。
潘强标 已提交
1260

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

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

P
panqiangbiao 已提交
1265 1266
**参数:**

H
HelloCrease 已提交
1267 1268 1269
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
1270 1271 1272

**返回值:**

H
HelloCrease 已提交
1273 1274
| 类型                    | 说明            |
| --------------------- | ------------- |
1275
| Promise&lt;number&gt; | Promise返回文件描述符 |
P
panqiangbiao 已提交
1276 1277 1278

**示例:**

1279
```js
P
panqiangbiao 已提交
1280
async function example() {
P
panqiangbiao 已提交
1281
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1282 1283
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
H
huweiqi 已提交
1284 1285 1286 1287 1288 1289
    const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
    asset.open('rw').then((fd) => {
        console.info('File open fd: ' + fd);
    }).catch((error) => {
        console.error('File open failed with error: ' + error);
    });
P
panqiangbiao 已提交
1290
}
P
panqiangbiao 已提交
1291 1292
```

P
panqiangbiao 已提交
1293
### close<sup>8+</sup>
P
panqiangbiao 已提交
1294

P
panqiangbiao 已提交
1295
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1296 1297 1298

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

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

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

P
panqiangbiao 已提交
1303 1304
**参数:**

H
HelloCrease 已提交
1305 1306 1307 1308
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1309 1310 1311

**示例:**

1312
```js
P
panqiangbiao 已提交
1313
async function example() {
H
huweiqi 已提交
1314
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1315 1316
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1317 1318 1319
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1320 1321 1322
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1323
    asset.open('rw').then((fd) => {
H
huweiqi 已提交
1324 1325 1326 1327
        console.info('File open fd: ' + fd);
        asset.close(fd, (error) => {
            if (error) {
                console.error('asset.close failed with error: ' + error);
Z
zhang-daiyue 已提交
1328
            } else {
H
huweiqi 已提交
1329
                console.info('asset.close successfully');
Z
zhang-daiyue 已提交
1330 1331
            }
        });
H
huweiqi 已提交
1332 1333
    }).catch((error) => {
        console.error('File open failed with error: ' + error);
P
panqiangbiao 已提交
1334
    });
H
huweiqi 已提交
1335
    fetchFileResult.close();
P
panqiangbiao 已提交
1336
}
P
panqiangbiao 已提交
1337 1338
```

P
panqiangbiao 已提交
1339
### close<sup>8+</sup>
P
panqiangbiao 已提交
1340

P
panqiangbiao 已提交
1341
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1342 1343 1344

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

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

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

P
panqiangbiao 已提交
1349 1350
**参数:**

H
HelloCrease 已提交
1351 1352 1353
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1354 1355 1356

**返回值:**

H
HelloCrease 已提交
1357 1358
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1359
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1360 1361 1362

**示例:**

1363
```js
P
panqiangbiao 已提交
1364
async function example() {
H
huweiqi 已提交
1365
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1366 1367
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1368 1369 1370
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1371 1372 1373
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1374 1375
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
H
huweiqi 已提交
1376 1377 1378 1379
        asset.close(fd).then(() => {
            console.info('asset.close successfully');
        }).catch((closeErr) => {
            console.error('asset.close fail, closeErr: ' + closeErr);
Z
zhang-daiyue 已提交
1380
        });
H
huweiqi 已提交
1381 1382
    }).catch((error) => {
        console.error('open File failed with error: ' + error);
P
panqiangbiao 已提交
1383
    });
H
huweiqi 已提交
1384
    fetchFileResult.close();
P
panqiangbiao 已提交
1385
}
P
panqiangbiao 已提交
1386 1387
```

P
panqiangbiao 已提交
1388
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1389

P
panqiangbiao 已提交
1390
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1391 1392 1393

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

P
panqiangbiao 已提交
1394
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1395

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

P
panqiangbiao 已提交
1398 1399
**参数:**

H
HelloCrease 已提交
1400 1401 1402
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1403 1404 1405

**示例:**

1406
```js
P
panqiangbiao 已提交
1407
async function example() {
H
huweiqi 已提交
1408
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1409 1410
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1411 1412 1413
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1414 1415 1416
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1417 1418 1419 1420 1421 1422
    asset.getThumbnail((error, pixelmap) => {
        if (error) {
            console.error('mediaLibrary getThumbnail failed with error: ' + error);
        } else {
            console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
        }
P
panqiangbiao 已提交
1423
    });
H
huweiqi 已提交
1424
    fetchFileResult.close();
P
panqiangbiao 已提交
1425
}
P
panqiangbiao 已提交
1426 1427
```

P
panqiangbiao 已提交
1428
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1429

P
panqiangbiao 已提交
1430
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1431 1432 1433

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

P
panqiangbiao 已提交
1434
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1435

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

P
panqiangbiao 已提交
1438 1439
**参数:**

H
HelloCrease 已提交
1440 1441 1442 1443
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1444 1445 1446

**示例:**

1447
```js
P
panqiangbiao 已提交
1448
async function example() {
1449
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1450 1451
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1452 1453 1454
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1455
    };
Z
zhang-daiyue 已提交
1456
    let size = { width: 720, height: 720 };
P
panqiangbiao 已提交
1457 1458
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1459 1460 1461 1462 1463 1464
    asset.getThumbnail(size, (error, pixelmap) => {
        if (error) {
            console.error('mediaLibrary getThumbnail failed with error: ' + error);
        } else {
            console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
        }
P
panqiangbiao 已提交
1465
    });
H
huweiqi 已提交
1466
    fetchFileResult.close();
P
panqiangbiao 已提交
1467
}
P
panqiangbiao 已提交
1468 1469
```

P
panqiangbiao 已提交
1470
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1471

P
panqiangbiao 已提交
1472
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1473 1474 1475

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

P
panqiangbiao 已提交
1476
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1477

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

P
panqiangbiao 已提交
1480 1481
**参数:**

H
HelloCrease 已提交
1482 1483 1484
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1485 1486 1487

**返回值:**

H
HelloCrease 已提交
1488 1489
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1490
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1491 1492 1493

**示例:**

1494
```js
P
panqiangbiao 已提交
1495
async function example() {
1496
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1497 1498
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
潘强标 已提交
1499 1500
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
H
huweiqi 已提交
1501
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1502
    };
Z
zhang-daiyue 已提交
1503
    let size = { width: 720, height: 720 };
P
panqiangbiao 已提交
1504 1505
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1506 1507 1508 1509
    asset.getThumbnail(size).then((pixelmap) => {
        console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
    }).catch((error) => {
        console.error('mediaLibrary getThumbnail failed with error: ' + error);
P
panqiangbiao 已提交
1510
    });
H
huweiqi 已提交
1511
    fetchFileResult.close();
P
panqiangbiao 已提交
1512
}
P
panqiangbiao 已提交
1513 1514
```

P
panqiangbiao 已提交
1515
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1516

P
panqiangbiao 已提交
1517
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1518 1519 1520

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

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

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

P
panqiangbiao 已提交
1525 1526
**参数:**

H
HelloCrease 已提交
1527 1528 1529 1530
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1531 1532 1533

**示例:**

1534
```js
P
panqiangbiao 已提交
1535
async function example() {
1536
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1537 1538
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1539 1540 1541
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1542 1543 1544
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1545 1546 1547 1548 1549 1550
    asset.favorite(true,(error) => {
        if (error) {
            console.error('mediaLibrary favorite failed with error: ' + error);
        } else {
            console.info('mediaLibrary favorite Successful');
        }
P
panqiangbiao 已提交
1551
    });
H
huweiqi 已提交
1552
    fetchFileResult.close();
P
panqiangbiao 已提交
1553
}
P
panqiangbiao 已提交
1554 1555
```

P
panqiangbiao 已提交
1556
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1557

P
panqiangbiao 已提交
1558
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1559 1560 1561

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

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

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

P
panqiangbiao 已提交
1566 1567
**参数:**

H
HelloCrease 已提交
1568 1569 1570
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1571 1572 1573

**返回值:**

H
HelloCrease 已提交
1574 1575
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1576
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1577 1578 1579

**示例:**

1580
```js
P
panqiangbiao 已提交
1581
async function example() {
1582
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1583 1584
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1585 1586 1587
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1588 1589 1590
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1591 1592 1593 1594
    asset.favorite(true).then(() => {
        console.info('mediaLibrary favorite Successful');
    }).catch((error) => {
        console.error('mediaLibrary favorite failed with error: ' + error);
P
panqiangbiao 已提交
1595
    });
H
huweiqi 已提交
1596
    fetchFileResult.close();
P
panqiangbiao 已提交
1597
}
P
panqiangbiao 已提交
1598 1599
```

P
panqiangbiao 已提交
1600
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1601

P
panqiangbiao 已提交
1602
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1603 1604 1605

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

P
panqiangbiao 已提交
1606
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1607

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

P
panqiangbiao 已提交
1610 1611
**参数:**

H
HelloCrease 已提交
1612 1613 1614
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1615 1616 1617

**示例:**

1618
```js
P
panqiangbiao 已提交
1619
async function example() {
1620
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1621 1622
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1623 1624 1625
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1626 1627 1628
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1629 1630 1631 1632 1633
    asset.isFavorite((error, isFavorite) => {
        if (error) {
            console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
        } else {
            console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
P
panqiangbiao 已提交
1634 1635
        }
    });
H
huweiqi 已提交
1636
    fetchFileResult.close();
P
panqiangbiao 已提交
1637
}
P
panqiangbiao 已提交
1638 1639
```

P
panqiangbiao 已提交
1640
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1641

P
panqiangbiao 已提交
1642
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1643 1644 1645

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

P
panqiangbiao 已提交
1646
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1647

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

P
panqiangbiao 已提交
1650 1651
**返回值:**

H
HelloCrease 已提交
1652 1653
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1654
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1655 1656 1657

**示例:**

1658
```js
P
panqiangbiao 已提交
1659
async function example() {
1660
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1661 1662
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1663 1664 1665
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1666 1667 1668
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1669 1670 1671 1672
    asset.isFavorite().then((isFavorite) => {
        console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
    }).catch((error) => {
        console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
P
panqiangbiao 已提交
1673
    });
H
huweiqi 已提交
1674
    fetchFileResult.close();
P
panqiangbiao 已提交
1675
}
P
panqiangbiao 已提交
1676 1677
```

P
panqiangbiao 已提交
1678
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1679

A
AOL 已提交
1680
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1681 1682 1683

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

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

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

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

P
panqiangbiao 已提交
1690 1691
**参数:**

H
HelloCrease 已提交
1692 1693 1694 1695
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1696 1697 1698

**示例:**

1699
```js
P
panqiangbiao 已提交
1700
async function example() {
1701
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1702 1703
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1704 1705 1706
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1707 1708 1709
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1710 1711 1712 1713 1714 1715 1716 1717
    asset.trash(true, (error) => {
        if (error) {
            console.error('mediaLibrary trash failed with error: ' + error);
        } else {
            console.info('mediaLibrary trash Successful');
        }
    });
    fetchFileResult.close();
P
panqiangbiao 已提交
1718
}
P
panqiangbiao 已提交
1719 1720
```

P
panqiangbiao 已提交
1721
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1722

A
AOL 已提交
1723
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1724 1725 1726

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

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

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

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

P
panqiangbiao 已提交
1733 1734
**参数:**

H
HelloCrease 已提交
1735 1736 1737
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1738 1739 1740

**返回值:**

H
HelloCrease 已提交
1741 1742
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1743
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1744 1745 1746

**示例:**

1747
```js
P
panqiangbiao 已提交
1748
async function example() {
1749
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1750 1751
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1752 1753 1754
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1755 1756 1757
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1758 1759 1760 1761
    asset.trash(true).then(() => {
        console.info('trash successfully');
    }).catch((error) => {
        console.error('trash failed with error: ' + error);
P
panqiangbiao 已提交
1762
    });
H
huweiqi 已提交
1763
    fetchFileResult.close();
P
panqiangbiao 已提交
1764
}
P
panqiangbiao 已提交
1765 1766
```

P
panqiangbiao 已提交
1767
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1768

P
panqiangbiao 已提交
1769
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1770 1771 1772

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

P
panqiangbiao 已提交
1773
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1774

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

P
panqiangbiao 已提交
1777 1778
**参数:**

H
HelloCrease 已提交
1779 1780 1781
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1782 1783 1784

**示例:**

1785
```js
P
panqiangbiao 已提交
1786
async function example() {
1787
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1788 1789
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1790 1791 1792
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1793 1794 1795
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1796 1797 1798 1799 1800 1801
    asset.isTrash((error, isTrash) => {
        if (error) {
            console.error('Failed to get trash state failed with error: ' + error);
            return;
        }
        console.info('Get trash state successfully, isTrash result: ' + isTrash);
Z
zhang-daiyue 已提交
1802
    });
H
huweiqi 已提交
1803
    fetchFileResult.close();
P
panqiangbiao 已提交
1804
}
P
panqiangbiao 已提交
1805 1806
```

P
panqiangbiao 已提交
1807
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1808

P
panqiangbiao 已提交
1809
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1810

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

P
panqiangbiao 已提交
1813
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1814

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

P
panqiangbiao 已提交
1817 1818
**返回值:**

H
HelloCrease 已提交
1819 1820
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1821
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1822 1823 1824

**示例:**

1825
```js
P
panqiangbiao 已提交
1826
async function example() {
1827
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1828 1829
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1830 1831 1832
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1833 1834 1835
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1836 1837 1838 1839
    asset.isTrash().then((isTrash) => {
        console.info('isTrash result: ' + isTrash);
    }).catch((error) => {
        console.error('isTrash failed with error: ' + error);
P
panqiangbiao 已提交
1840
    });
H
huweiqi 已提交
1841
    fetchFileResult.close();
P
panqiangbiao 已提交
1842
}
P
panqiangbiao 已提交
1843 1844
```

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

文件检索结果集。

Z
zengyawen 已提交
1849
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1850

P
panqiangbiao 已提交
1851
getCount(): number
P
panqiangbiao 已提交
1852 1853 1854

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

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

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

H
HelloCrease 已提交
1859 1860
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1861
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1862 1863 1864

**示例**

1865
```js
P
panqiangbiao 已提交
1866
async function example() {
1867
    let fileKeyObj = mediaLibrary.FileKey;
Z
zhang-daiyue 已提交
1868
    let fileType = mediaLibrary.MediaType.FILE;
P
panqiangbiao 已提交
1869 1870 1871
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
H
huweiqi 已提交
1872
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1873 1874 1875
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
H
huweiqi 已提交
1876 1877
    console.info('fetchCount result: ' + fetchCount);
    fetchFileResult.close();
P
panqiangbiao 已提交
1878
}
P
panqiangbiao 已提交
1879 1880
```

Z
zengyawen 已提交
1881
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1882

P
panqiangbiao 已提交
1883
isAfterLast(): boolean
P
panqiangbiao 已提交
1884 1885 1886

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

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

P
panqiangbiao 已提交
1889 1890
**返回值**

H
HelloCrease 已提交
1891 1892
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1893
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1894 1895 1896

**示例**

1897
```js
P
panqiangbiao 已提交
1898
async function example() {
1899
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1900 1901
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1902 1903 1904
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1905 1906 1907
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
H
huweiqi 已提交
1908
    console.info('mediaLibrary fetchFileResult.getCount, count:' + fetchCount);
P
panqiangbiao 已提交
1909 1910
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
H
huweiqi 已提交
1911 1912 1913 1914
        fileAsset = await fetchFileResult.getNextObject();
        if(i == fetchCount - 1) {
            var result = fetchFileResult.isAfterLast();
            console.info('mediaLibrary fileAsset isAfterLast result: ' + result);
H
huweiqi 已提交
1915
            fetchFileResult.close();
H
huweiqi 已提交
1916
        }
P
panqiangbiao 已提交
1917
    }
P
panqiangbiao 已提交
1918
}
P
panqiangbiao 已提交
1919 1920
```

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

P
panqiangbiao 已提交
1923
close(): void
P
panqiangbiao 已提交
1924 1925 1926

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

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

P
panqiangbiao 已提交
1929 1930
**示例**

1931
```js
P
panqiangbiao 已提交
1932
async function example() {
1933
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1934 1935
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1936 1937 1938
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1939 1940 1941 1942
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1943 1944
```

Z
zengyawen 已提交
1945
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1946

P
panqiangbiao 已提交
1947
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1948 1949 1950

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

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

P
panqiangbiao 已提交
1953 1954
**参数**

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

**示例**

1961
```js
P
panqiangbiao 已提交
1962
async function example() {
1963
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1964 1965
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1966 1967 1968
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1969 1970
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
1971 1972 1973 1974 1975 1976
    fetchFileResult.getFirstObject((error, fileAsset) => {
        if (error) {
            console.error('fetchFileResult getFirstObject failed with error: ' + error);
            return;
        }
        console.info('getFirstObject successfully, displayName : ' + fileAsset.displayName);
H
huweiqi 已提交
1977
        fetchFileResult.close();
P
panqiangbiao 已提交
1978 1979
    })
}
P
panqiangbiao 已提交
1980 1981
```

Z
zengyawen 已提交
1982
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1983

P
panqiangbiao 已提交
1984
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1985

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

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

P
panqiangbiao 已提交
1990 1991
**返回值**

Z
zengyawen 已提交
1992 1993
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1994
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1995 1996 1997

**示例**

1998
```js
P
panqiangbiao 已提交
1999
async function example() {
2000
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2001 2002
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2003 2004 2005
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2006 2007
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2008 2009
    fetchFileResult.getFirstObject().then((fileAsset) => {
        console.info('getFirstObject successfully, displayName: ' + fileAsset.displayName);
H
huweiqi 已提交
2010
        fetchFileResult.close();
H
huweiqi 已提交
2011 2012
    }).catch((error) => {
        console.error('getFirstObject failed with error: ' + error);
P
panqiangbiao 已提交
2013 2014
    });
}
P
panqiangbiao 已提交
2015 2016
```

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

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

H
huweiqi 已提交
2021 2022 2023
获取文件检索结果中的下一个文件资产,此方法使用callback形式返回结果。

> **说明**: 在使用前需要先使用[getFirstObject](#getfirstobject7)接口获取第一个文件资产,然后使用[isAfterLast](#isafterlast7)确认文件检索集当前不是指向最后一个时方可使用此接口。
P
panqiangbiao 已提交
2024

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

P
panqiangbiao 已提交
2027 2028
**参数**

Z
zengyawen 已提交
2029 2030 2031
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
2032 2033 2034

**示例**

2035
```js
P
panqiangbiao 已提交
2036
async function example() {
2037
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2038 2039
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2040 2041 2042
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2043 2044
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2045
    let fileAsset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
2046
    if (!fileAsset.isAfterLast) {
H
huweiqi 已提交
2047 2048 2049 2050 2051 2052
        fetchFileResult.getNextObject((error, fileAsset) => {
            if (error) {
                console.error('fetchFileResult getNextObject failed with error: ' + error);
                return;
            }
            console.log('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
H
huweiqi 已提交
2053
            fetchFileResult.close();
H
huweiqi 已提交
2054 2055
        })
    }
P
panqiangbiao 已提交
2056
}
H
huweiqi 已提交
2057

P
panqiangbiao 已提交
2058 2059
```

Z
zengyawen 已提交
2060
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2061

P
panqiangbiao 已提交
2062
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2063 2064 2065

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

H
huweiqi 已提交
2066 2067
> **说明**: 在使用前需要先使用[getFirstObject](#getfirstobject7)接口获取第一个文件资产,然后使用[isAfterLast](#isafterlast7)确认文件检索集当前不是指向最后一个时方可使用此接口。

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

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

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

**示例**

2078
```js
P
panqiangbiao 已提交
2079
async function example() {
2080
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2081 2082
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2083 2084 2085
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2086 2087
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2088
    let fileAsset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
2089
    if (!fileAsset.isAfterLast) {
H
huweiqi 已提交
2090 2091
        fetchFileResult.getNextObject().then((fileAsset) => {
            console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
H
huweiqi 已提交
2092
            fetchFileResult.close();
H
huweiqi 已提交
2093 2094 2095 2096
        }).catch((error) => {
            console.error('fetchFileResult getNextObject failed with error: ' + error);
        })
    }
P
panqiangbiao 已提交
2097
}
P
panqiangbiao 已提交
2098 2099
```

Z
zengyawen 已提交
2100
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2101

P
panqiangbiao 已提交
2102
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2103 2104 2105

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

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

P
panqiangbiao 已提交
2108 2109
**参数**

Z
zengyawen 已提交
2110 2111
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
Z
zengyawen 已提交
2112
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2113 2114 2115

**示例**

2116
```js
P
panqiangbiao 已提交
2117
async function example() {
2118
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2119 2120
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2121 2122 2123
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2124 2125
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2126 2127 2128 2129 2130 2131
    fetchFileResult.getLastObject((error, fileAsset) => {
        if (error) {
            console.error('getLastObject failed with error: ' + error);
            return;
        }
        console.info('getLastObject successfully, displayName: ' + fileAsset.displayName);
H
huweiqi 已提交
2132
        fetchFileResult.close();
P
panqiangbiao 已提交
2133 2134
    })
}
P
panqiangbiao 已提交
2135 2136
```

Z
zengyawen 已提交
2137
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2138

P
panqiangbiao 已提交
2139
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2140 2141 2142

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

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

P
panqiangbiao 已提交
2145 2146
**返回值**

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

**示例**

2153
```js
P
panqiangbiao 已提交
2154
async function example() {
2155
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2156 2157
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2158 2159 2160
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2161 2162
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2163 2164
    fetchFileResult.getLastObject().then((fileAsset) => {
        console.info('getLastObject successfully, displayName: ' + fileAsset.displayName);
H
huweiqi 已提交
2165
        fetchFileResult.close();
H
huweiqi 已提交
2166 2167 2168
    }).catch((error) => {
        console.error('getLastObject failed with error: ' + error);
    });
P
panqiangbiao 已提交
2169
}
P
panqiangbiao 已提交
2170 2171
```

Z
zengyawen 已提交
2172
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2173

P
panqiangbiao 已提交
2174
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2175 2176 2177

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

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

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

Z
zengyawen 已提交
2182
| 参数名       | 类型                                       | 必填   | 说明                 |
H
HelloCrease 已提交
2183
| -------- | ---------------------------------------- | ---- | ------------------ |
H
huweiqi 已提交
2184
| index    | number                                   | 是    | 要获取的文件的索引,从0开始(注意该值要小于文件检索集的count值)     |
Z
zengyawen 已提交
2185
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是    | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2186 2187 2188

**示例**

2189
```js
P
panqiangbiao 已提交
2190
async function example() {
2191
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2192 2193
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2194 2195 2196
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2197 2198
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2199 2200 2201 2202 2203 2204
    fetchFileResult.getPositionObject(0, (error, fileAsset) => {
        if (error) {
            console.error('getPositionObject failed with error: ' + error);
            return;
        }
        console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
H
huweiqi 已提交
2205
        fetchFileResult.close();
P
panqiangbiao 已提交
2206 2207
    })
}
P
panqiangbiao 已提交
2208 2209
```

Z
zengyawen 已提交
2210
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2211

P
panqiangbiao 已提交
2212
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2213 2214 2215

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

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

P
panqiangbiao 已提交
2218 2219
**参数**

Z
zengyawen 已提交
2220
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
2221
| ----- | ------ | ---- | -------------- |
H
huweiqi 已提交
2222
| index | number | 是    | 要获取的文件的索引,从0开始(注意该值要小于文件检索集的count值) |
P
panqiangbiao 已提交
2223 2224 2225

**返回值**

Z
zengyawen 已提交
2226 2227 2228
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2229 2230 2231

**示例**

2232
```js
P
panqiangbiao 已提交
2233
async function example() {
2234
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2235 2236
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2237 2238 2239
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2240 2241
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2242 2243
    fetchFileResult.getPositionObject(0).then((fileAsset) => {
        console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
H
huweiqi 已提交
2244
        fetchFileResult.close();
H
huweiqi 已提交
2245 2246
    }).catch((error) => {
        console.error('getPositionObject failed with error: ' + error);
Z
zhang-daiyue 已提交
2247
    });
P
panqiangbiao 已提交
2248
}
P
panqiangbiao 已提交
2249 2250
```

Z
zengyawen 已提交
2251
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2252

P
panqiangbiao 已提交
2253
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2254 2255 2256

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

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

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

Z
zengyawen 已提交
2261
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
2262
| -------- | ---------------------------------------- | ---- | -------------------- |
H
huweiqi 已提交
2263
| callback | AsyncCallback&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
2264 2265 2266

**示例**

2267
```js
P
panqiangbiao 已提交
2268
async function example() {
2269
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2270 2271
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2272 2273 2274
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2275 2276
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2277 2278 2279
    fetchFileResult.getAllObject((error, fileAssetList) => {
        if (error) {
           console.error('getAllObject failed with error: ' + error);
P
panqiangbiao 已提交
2280
           return;
2281 2282
        }
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
H
huweiqi 已提交
2283
            console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
H
huweiqi 已提交
2284 2285
        }
        fetchFileResult.close();
P
panqiangbiao 已提交
2286 2287
    })
}
P
panqiangbiao 已提交
2288 2289
```

Z
zengyawen 已提交
2290
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2291

P
panqiangbiao 已提交
2292
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2293 2294 2295

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

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

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

Z
zengyawen 已提交
2300 2301
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
H
huweiqi 已提交
2302
| Promise&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2303 2304 2305

**示例**

2306
```js
P
panqiangbiao 已提交
2307
async function example() {
2308
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2309 2310
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
2311 2312 2313
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
2314 2315
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
H
huweiqi 已提交
2316 2317 2318 2319
    fetchFileResult.getAllObject().then((fileAssetList) => {
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
            console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
        } 
H
huweiqi 已提交
2320
        fetchFileResult.close();
H
huweiqi 已提交
2321 2322 2323
    }).catch((error) => {
        console.error('getAllObject failed with error: ' + error);
    });
P
panqiangbiao 已提交
2324
}
P
panqiangbiao 已提交
2325 2326
```

Z
zengyawen 已提交
2327
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2328 2329 2330

实体相册

Z
zengyawen 已提交
2331
### 属性
P
panqiangbiao 已提交
2332

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

Z
zengyawen 已提交
2335
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2336
| ------------ | ------ | ---- | ---- | ------- |
Z
zengyawen 已提交
2337 2338 2339
| albumId | number | 是    | 否    | 相册编号    |
| albumName | string | 是    | 是    | 相册名称    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri   |
H
HelloCrease 已提交
2340
| dateModified | number | 是    | 否    | 修改日期    |
Z
zengyawen 已提交
2341 2342 2343
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
2344

P
panqiangbiao 已提交
2345 2346 2347
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2348 2349 2350

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

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

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

P
panqiangbiao 已提交
2355 2356
**参数**

Z
zengyawen 已提交
2357 2358 2359
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2360 2361 2362

**示例**

2363
```js
P
panqiangbiao 已提交
2364
async function example() {
P
panqiangbiao 已提交
2365 2366 2367 2368 2369 2370 2371
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
H
huweiqi 已提交
2372 2373 2374 2375 2376 2377
    album.commitModify((error) => {
        if (error) {
            console.error('commitModify failed with error: ' + error);
            return;
        }
        console.info('commitModify successful.');
P
panqiangbiao 已提交
2378 2379
    })
}
P
panqiangbiao 已提交
2380 2381
```

P
panqiangbiao 已提交
2382
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2383

P
panqiangbiao 已提交
2384
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2385 2386 2387

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

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

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

P
panqiangbiao 已提交
2392 2393
**返回值**

H
HelloCrease 已提交
2394 2395
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2396 2397 2398 2399
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2400
```js
P
panqiangbiao 已提交
2401
async function example() {
P
panqiangbiao 已提交
2402 2403 2404 2405 2406 2407 2408
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
H
huweiqi 已提交
2409 2410 2411 2412
    album.commitModify().then(() => {
        console.info('commitModify successfully');
    }).catch((error) => {
        console.error('commitModify failed with error: ' + error);
P
panqiangbiao 已提交
2413 2414
    });
}
P
panqiangbiao 已提交
2415 2416
```

Z
zengyawen 已提交
2417
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2418

P
panqiangbiao 已提交
2419
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2420 2421 2422

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

P
panqiangbiao 已提交
2423
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2424

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

P
panqiangbiao 已提交
2427 2428
**参数**

Z
zengyawen 已提交
2429
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2430
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2431 2432
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2433 2434 2435

**示例**

2436
```js
P
panqiangbiao 已提交
2437
async function example() {
P
panqiangbiao 已提交
2438 2439 2440 2441
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2442
    let fileNoArgsfetchOp = {
H
huweiqi 已提交
2443 2444
        selections: '',
        selectionArgs: [],
Z
zhang-daiyue 已提交
2445
    }
H
huweiqi 已提交
2446
    // 获取符合检索要求的相册,返回相册列表
P
panqiangbiao 已提交
2447 2448
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
H
huweiqi 已提交
2449 2450 2451 2452 2453 2454
    // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源
    album.getFileAssets(fileNoArgsfetchOp, (error, fetchFileResult) => {
        if (error) {
            console.error('album getFileAssets failed with error: ' + error);
            return;
        }
H
huweiqi 已提交
2455
        let count = fetchFileResult.getCount();
H
huweiqi 已提交
2456
        console.info('album getFileAssets successfully, count: ' + count);
H
huweiqi 已提交
2457
        fetchFileResult.close();
H
huweiqi 已提交
2458
    });
P
panqiangbiao 已提交
2459 2460 2461
}
```

Z
zengyawen 已提交
2462
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2463

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

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

P
panqiangbiao 已提交
2468
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2469

P
panqiangbiao 已提交
2470
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2471 2472 2473

**参数**

Z
zengyawen 已提交
2474
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2475
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2476
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2477 2478 2479

**返回值**

Z
zengyawen 已提交
2480 2481
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2482
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2483 2484 2485

**示例**

2486
```js
P
panqiangbiao 已提交
2487
async function example() {
P
panqiangbiao 已提交
2488 2489 2490 2491
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
H
huweiqi 已提交
2492
    let fileNoArgsfetchOp = {
H
huweiqi 已提交
2493 2494
        selections: '',
        selectionArgs: [],
2495
    };
H
huweiqi 已提交
2496
    // 获取符合检索要求的相册,返回相册列表
P
panqiangbiao 已提交
2497 2498
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
H
huweiqi 已提交
2499
    // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源
H
huweiqi 已提交
2500 2501
    album.getFileAssets(fileNoArgsfetchOp).then((fetchFileResult) => {
        let count = fetchFileResult.getCount();
H
huweiqi 已提交
2502
        console.info('album getFileAssets successfully, count: ' + count);
H
huweiqi 已提交
2503
        fetchFileResult.close();
H
huweiqi 已提交
2504 2505
    }).catch((error) => {
        console.error('album getFileAssets failed with error: ' + error);
P
panqiangbiao 已提交
2506 2507
    });
}
P
panqiangbiao 已提交
2508 2509
```

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

P
panqiangbiao 已提交
2512
注册设备的信息。
2513 2514

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

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

Z
zengyawen 已提交
2518 2519 2520 2521 2522 2523
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2524 2525 2526



Z
zengyawen 已提交
2527
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2528 2529 2530

枚举,媒体类型。

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

H
huweiqi 已提交
2533 2534 2535 2536 2537 2538
| 名称  |  值 |  说明 |
| ----- |  ---- | ---- |
| FILE  |  0 | 文件 |
| IMAGE |  1 | 图片 |
| VIDEO |  2 | 视频 |
| AUDIO |  3 | 音频 |
P
panqiangbiao 已提交
2539

Z
zengyawen 已提交
2540
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2541 2542 2543

枚举,文件关键信息。

zyjhandsome's avatar
zyjhandsome 已提交
2544
> **说明:**
H
huweiqi 已提交
2545 2546
> bucket_id字段在文件重命名或移动后可能会发生变化,开发者使用前需要重新获取。

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

H
huweiqi 已提交
2549
| 名称          | 值              | 说明                                                       |
Z
zengyawen 已提交
2550
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569
| ID            | 'file_id'             | 文件编号                                                   |
| RELATIVE_PATH | 'relative_path'       | 相对公共目录路径                                           |
| DISPLAY_NAME  | 'display_name'        | 显示名字                                                   |
| PARENT        | 'parent'              | 父目录id                                                   |
| MIME_TYPE     | 'mime_type'           | 文件扩展属性(如:image/*、video/*、file/*)                                             |
| 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'         | 专辑                                                       |
| DURATION      | 'duration'            | 持续时间(单位:毫秒)                                       |
| WIDTH         | 'width'               | 图片宽度(单位:像素)                                     |
| HEIGHT        | 'height'              | 图片高度(单位:像素)                                     |
| ORIENTATION   | 'orientation'         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
| ALBUM_ID      | 'bucket_id'           | 文件所归属的相册编号                                       |
| ALBUM_NAME    | 'bucket_display_name' | 文件所归属相册名称                                         |
P
panqiangbiao 已提交
2570

Z
zengyawen 已提交
2571
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2572 2573 2574

枚举,目录类型。

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

H
huweiqi 已提交
2577 2578 2579 2580 2581 2582 2583 2584
| 名称          | 值 |  说明               |
| ------------- | --- | ------------------ |
| DIR_CAMERA    |  0 | 表示Camera文件路径 |
| DIR_VIDEO     |  1 |  表示视频路径       |
| DIR_IMAGE     |  2 | 表示图片路径       |
| DIR_AUDIO     |  3 | 表示音频路径       |
| DIR_DOCUMENTS |  4 | 表示文档路径       |
| DIR_DOWNLOAD  |  5 |  表示下载路径       |
P
panqiangbiao 已提交
2585

Z
zengyawen 已提交
2586
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2587 2588

枚举,设备类型。
2589 2590

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

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

H
huweiqi 已提交
2594 2595 2596 2597 2598 2599 2600 2601 2602
| 名称         |  值 | 说明       |
| ------------ | --- | ---------- |
| TYPE_UNKNOWN |  0 | 未识别设备 |
| TYPE_LAPTOP  |  1 | 笔记本电脑 |
| TYPE_PHONE   |  2 | 手机       |
| TYPE_TABLET  |  3 | 平板电脑   |
| TYPE_WATCH   |  4 | 智能手表   |
| TYPE_CAR     |  5 | 车载设备   |
| TYPE_TV      |  6 | 电视设备   |
P
panqiangbiao 已提交
2603

Z
zengyawen 已提交
2604
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2605 2606 2607

检索条件。

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

H
huweiqi 已提交
2610 2611
| 名称                    | 类型                | 可读 | 可写 | 说明                                                         |
| ----------------------- | ------------------- | ---- | ---- | ------------------------------------------------------------ |
H
huweiqi 已提交
2612
| selections              | string              | 是   | 是   | 检索条件,使用[FileKey](#filekey8)中的枚举值作为检索条件的列名。示例:<br/>selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR ' + mediaLibrary.FileKey.MEDIA_TYPE + '= ?', |
2613
| selectionArgs           | Array&lt;string&gt; | 是   | 是   | 检索条件的值,对应selections中检索条件列的值。<br/>示例:<br/>selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
H
huweiqi 已提交
2614
| order                   | string              | 是   | 是   | 检索结果排序方式,使用[FileKey](#filekey8)中的枚举值作为检索结果排序的列,可以用升序或降序排列。示例:<br/>升序排列:order: mediaLibrary.FileKey.DATE_ADDED + ' ASC'<br/>降序排列:order: mediaLibrary.FileKey.DATE_ADDED + ' DESC' |
H
huweiqi 已提交
2615 2616 2617
| uri<sup>8+</sup>        | string              | 是   | 是   | 文件URI                                                      |
| networkId<sup>8+</sup>  | string              | 是   | 是   | 注册设备网络ID                                               |
| extendArgs<sup>8+</sup> | string              | 是   | 是   | 扩展的检索参数,目前没有扩展检索参数                         |
P
panqiangbiao 已提交
2618

P
panqiangbiao 已提交
2619
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2620 2621

图片尺寸。
2622 2623

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

H
HelloCrease 已提交
2625 2626 2627 2628
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2629

H
huweiqi 已提交
2630
## MediaAssetOption
P
panqiangbiao 已提交
2631 2632 2633 2634 2635 2636

媒体资源选项。

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


H
huweiqi 已提交
2637 2638 2639 2640 2641
| 名称         | 类型   | 可读 | 可写 | 说明                                                         |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| 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 已提交
2642

H
huweiqi 已提交
2643
## MediaSelectOption
P
panqiangbiao 已提交
2644 2645 2646 2647 2648

媒体资源类型选项。

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

H
huweiqi 已提交
2649 2650
| 名称    | 类型     | 可读 | 可写 | 说明                   |
| ----- | ------ | ---- | ---- | -------------------- |
2651
| type  | 'image' &#124; 'video' &#124; 'media' | 是    | 是  | 媒体类型,包括:image, video, media,当前仅支持media类型 |
H
huweiqi 已提交
2652
| count | number | 是    | 是  | 可以选择媒体数量的最大值,count = 1表示单选,count大于1表示多选。            |
Z
zhang-daiyue 已提交
2653