js-apis-medialibrary.md 100.5 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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

**返回值**

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

**示例:**

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

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

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

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

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

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

H
HelloCrease 已提交
224 225
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
226
| 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 已提交
227
| callback | Callback&lt;void&gt; | 是    | 回调返回空                                    |
P
panqiangbiao 已提交
228 229 230

**示例:**

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

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

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

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

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

H
HelloCrease 已提交
246 247
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
248
| 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 已提交
249
| callback | Callback&lt;void&gt; | 否    | 回调返回空                                    |
P
panqiangbiao 已提交
250 251 252

**示例:**

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

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

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

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

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

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

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

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

**示例:**

280
```js
P
panqiangbiao 已提交
281 282 283 284 285
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 已提交
286
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (error, fileAsset) => {
P
panqiangbiao 已提交
287
        if (fileAsset != undefined) {
H
huweiqi 已提交
288
            console.info('createAsset successfully, message');
P
panqiangbiao 已提交
289
        } else {
H
huweiqi 已提交
290
            console.error('createAsset failed with error: ' + error);
P
panqiangbiao 已提交
291 292 293
        }
    });
}
P
panqiangbiao 已提交
294 295
```

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

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

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

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

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

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

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

**返回值**

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

**示例:**

322
```js
H
huweiqi 已提交
323 324 325 326 327 328 329
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 已提交
330 331
    }).catch((error) => {
        console.error('createAsset failed with error: ' + error);
H
huweiqi 已提交
332 333
    });
}
P
panqiangbiao 已提交
334 335
```

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

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

删除媒体文件资源

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

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

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

**参数:**

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

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

**示例:**

```js
async function example() {
363 364
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
365 366 367 368 369 370 371
    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 已提交
372 373
        console.error('asset not exist');
        return;
Z
zengyawen 已提交
374 375
    }
    media.deleteAsset(asset.uri).then(() => {
H
huweiqi 已提交
376 377 378
        console.info('deleteAsset successfully');
    }).catch((error) => {
        console.error('deleteAsset failed with error: ' + error);
Z
zengyawen 已提交
379
    });
H
huweiqi 已提交
380
    fetchFileResult.close();
Z
zengyawen 已提交
381 382 383 384 385 386 387 388 389 390
}
```

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

删除媒体文件资源

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

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

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

**参数:**

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

**示例:**

```js
async function example() {
406 407
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
408 409 410 411 412 413 414
    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 已提交
415 416
        console.error('asset not exist');
        return;
Z
zengyawen 已提交
417
    }
H
huweiqi 已提交
418 419 420
    media.deleteAsset(asset.uri, (error) => {
        if (error != undefined) {
            console.error('deleteAsset failed with error: ' + error);
Z
zengyawen 已提交
421
        } else {
H
huweiqi 已提交
422
            console.info('deleteAsset successfully');
Z
zengyawen 已提交
423 424
        }
    });
H
huweiqi 已提交
425
    fetchFileResult.close();
Z
zengyawen 已提交
426 427 428
}
```

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

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

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

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

**参数:**

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

**示例:**

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

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

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

479
```js
P
panqiangbiao 已提交
480
async function example() {
P
panqiangbiao 已提交
481
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
H
huweiqi 已提交
482 483 484 485 486 487 488 489 490
    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 已提交
491 492 493
}
```

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

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

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

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

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

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

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

**示例:**

513
```js
H
huweiqi 已提交
514
async function example() {
H
huweiqi 已提交
515 516 517 518 519 520 521 522 523 524 525 526
   // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => {
    if (albumList != undefined) {
      console.info('getAlbums successfully: ' + JSON.stringify(albumList));
    } else {
      console.error('getAlbums failed with error: ' + error);
    }
  })
H
huweiqi 已提交
527
}
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
async function example() {
H
huweiqi 已提交
556 557 558 559 560 561 562 563 564 565
   // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
    console.info('getAlbums successfully: ' + JSON.stringify(albumList));
  }).catch((error) => {
    console.error('getAlbums failed with error: ' + error);
  });
H
huweiqi 已提交
566
}
P
panqiangbiao 已提交
567 568
```

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

**参数:**

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

**示例:**

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


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

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

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


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

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

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

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

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

**参数:**

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

**示例:**

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


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

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

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

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

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

**参数:**

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

**示例:**

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


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

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

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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


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

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

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

H
huweiqi 已提交
827 828 829
> **说明**: 
> 此接口为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 已提交
830 831 832 833 834

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

**参数:**

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

**示例:**

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


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

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

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

H
huweiqi 已提交
864 865 866
> **说明**: 
> 此接口为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 已提交
867 868 869 870 871

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

**参数:**

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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


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

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

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

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

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

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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


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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

H
HelloCrease 已提交
1119 1120
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1121
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1122 1123 1124

**示例:**

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

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

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

**示例:**

1238
```js
P
panqiangbiao 已提交
1239
async function example() {
P
panqiangbiao 已提交
1240
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1241 1242
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
H
huweiqi 已提交
1243 1244 1245 1246 1247 1248 1249
    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 已提交
1250 1251
    });
}
P
panqiangbiao 已提交
1252 1253
```

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

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

1280
```js
P
panqiangbiao 已提交
1281
async function example() {
P
panqiangbiao 已提交
1282
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1283 1284
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
H
huweiqi 已提交
1285 1286 1287 1288 1289 1290
    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 已提交
1291
}
P
panqiangbiao 已提交
1292 1293
```

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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

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

**示例:**

1407
```js
P
panqiangbiao 已提交
1408
async function example() {
H
huweiqi 已提交
1409
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1410 1411
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1412 1413 1414
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1415 1416 1417
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1418 1419 1420 1421 1422 1423
    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 已提交
1424
    });
H
huweiqi 已提交
1425
    fetchFileResult.close();
P
panqiangbiao 已提交
1426
}
P
panqiangbiao 已提交
1427 1428
```

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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

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

**示例:**

1619
```js
P
panqiangbiao 已提交
1620
async function example() {
1621
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1622 1623
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1624 1625 1626
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1627 1628 1629
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1630 1631 1632 1633 1634
    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 已提交
1635 1636
        }
    });
H
huweiqi 已提交
1637
    fetchFileResult.close();
P
panqiangbiao 已提交
1638
}
P
panqiangbiao 已提交
1639 1640
```

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

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

**示例:**

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

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

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

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

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

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

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

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

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

**返回值:**

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

**示例:**

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

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

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

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

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

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

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

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

**示例:**

1786
```js
P
panqiangbiao 已提交
1787
async function example() {
1788
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1789 1790
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
H
huweiqi 已提交
1791 1792 1793
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
P
panqiangbiao 已提交
1794 1795 1796
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
H
huweiqi 已提交
1797 1798 1799 1800 1801 1802
    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 已提交
1803
    });
H
huweiqi 已提交
1804
    fetchFileResult.close();
P
panqiangbiao 已提交
1805
}
P
panqiangbiao 已提交
1806 1807
```

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

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

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

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

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

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

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

**示例:**

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

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

文件检索结果集。

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

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

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

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

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

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

**示例**

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

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

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

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

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

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

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

**示例**

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

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

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

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

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

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

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

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

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

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

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

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

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

**示例**

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

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

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

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

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

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

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

**示例**

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

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

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

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

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

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

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

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

**示例**

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

P
panqiangbiao 已提交
2060 2061
```

Z
zengyawen 已提交
2062
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2063

H
huweiqi 已提交
2064
getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2065 2066 2067

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

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

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

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

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

**示例**

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

Z
zengyawen 已提交
2103
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2104

P
panqiangbiao 已提交
2105
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2106 2107 2108

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

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

P
panqiangbiao 已提交
2111 2112
**参数**

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

**示例**

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

Z
zengyawen 已提交
2140
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2141

P
panqiangbiao 已提交
2142
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2143 2144 2145

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

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

P
panqiangbiao 已提交
2148 2149
**返回值**

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

**示例**

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

Z
zengyawen 已提交
2175
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2176

P
panqiangbiao 已提交
2177
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2178 2179 2180

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

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

P
panqiangbiao 已提交
2183 2184
**参数**

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

**示例**

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

Z
zengyawen 已提交
2213
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2214

P
panqiangbiao 已提交
2215
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2216 2217 2218

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

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

P
panqiangbiao 已提交
2221 2222
**参数**

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

**返回值**

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

**示例**

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

Z
zengyawen 已提交
2254
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2255

P
panqiangbiao 已提交
2256
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2257 2258 2259

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

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

P
panqiangbiao 已提交
2262 2263
**参数**

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

**示例**

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

Z
zengyawen 已提交
2293
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2294

P
panqiangbiao 已提交
2295
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2296 2297 2298

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

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

P
panqiangbiao 已提交
2301 2302
**返回值**

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

**示例**

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

Z
zengyawen 已提交
2330
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2331 2332 2333

实体相册

Z
zengyawen 已提交
2334
### 属性
P
panqiangbiao 已提交
2335

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

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

P
panqiangbiao 已提交
2348 2349 2350
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2351 2352 2353

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

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

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

P
panqiangbiao 已提交
2358 2359
**参数**

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

**示例**

2366
```js
P
panqiangbiao 已提交
2367
async function example() {
H
huweiqi 已提交
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
  album.albumName = 'hello';
  album.commitModify((error) => {
    if (error) {
      console.error('commitModify failed with error: ' + error);
      return;
    }
    console.info('commitModify successful.');
  })
P
panqiangbiao 已提交
2383
}
P
panqiangbiao 已提交
2384 2385
```

P
panqiangbiao 已提交
2386
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2387

P
panqiangbiao 已提交
2388
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2389 2390 2391

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

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

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

P
panqiangbiao 已提交
2396 2397
**返回值**

H
HelloCrease 已提交
2398 2399
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2400 2401 2402 2403
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2404
```js
P
panqiangbiao 已提交
2405
async function example() {
H
huweiqi 已提交
2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
  album.albumName = 'hello';
  album.commitModify().then(() => {
    console.info('commitModify successfully');
  }).catch((error) => {
    console.error('commitModify failed with error: ' + error);
  });
}
```

### getFileAssets<sup>7+</sup>

getFileAssets(callback: AsyncCallback&lt;FetchFileResult&gt;): void

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

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

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

**参数**

| 参数名   | 类型                                                | 必填 | 说明                                |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |

**示例**

```js
async function example() {
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  // 获取符合检索要求的相册,返回相册列表。
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
  // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源。
  album.getFileAssets((error, fetchFileResult) => {
    if (error) {
      console.error('album getFileAssets failed with error: ' + error);
      return;
    }
    let count = fetchFileResult.getCount();
    console.info('album getFileAssets successfully, count: ' + count);
    fetchFileResult.close();
  });
P
panqiangbiao 已提交
2460
}
P
panqiangbiao 已提交
2461 2462
```

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

P
panqiangbiao 已提交
2465
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2466 2467 2468

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

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

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

P
panqiangbiao 已提交
2473 2474
**参数**

Z
zengyawen 已提交
2475
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2476
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2477 2478
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2479 2480 2481

**示例**

2482
```js
P
panqiangbiao 已提交
2483
async function example() {
H
huweiqi 已提交
2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
  }
  // 获取符合检索要求的相册,返回相册列表
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
  // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源
  album.getFileAssets(fileNoArgsfetchOp, (error, fetchFileResult) => {
    if (error) {
      console.error('album getFileAssets failed with error: ' + error);
      return;
Z
zhang-daiyue 已提交
2501
    }
H
huweiqi 已提交
2502 2503 2504 2505
    let count = fetchFileResult.getCount();
    console.info('album getFileAssets successfully, count: ' + count);
    fetchFileResult.close();
  });
P
panqiangbiao 已提交
2506 2507 2508
}
```

Z
zengyawen 已提交
2509
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2510

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

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

P
panqiangbiao 已提交
2515
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2516

P
panqiangbiao 已提交
2517
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2518 2519 2520

**参数**

Z
zengyawen 已提交
2521
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2522
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2523
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2524 2525 2526

**返回值**

Z
zengyawen 已提交
2527 2528
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2529
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2530 2531 2532

**示例**

2533
```js
P
panqiangbiao 已提交
2534
async function example() {
H
huweiqi 已提交
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
  };
  // 获取符合检索要求的相册,返回相册列表
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
  // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源
  album.getFileAssets(fileNoArgsfetchOp).then((fetchFileResult) => {
    let count = fetchFileResult.getCount();
    console.info('album getFileAssets successfully, count: ' + count);
    fetchFileResult.close();
  }).catch((error) => {
    console.error('album getFileAssets failed with error: ' + error);
  });
P
panqiangbiao 已提交
2555
}
P
panqiangbiao 已提交
2556 2557
```

P
panqiangbiao 已提交
2558
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2559

P
panqiangbiao 已提交
2560
注册设备的信息。
2561 2562

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

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

Z
zengyawen 已提交
2566 2567 2568 2569 2570 2571
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2572 2573 2574



Z
zengyawen 已提交
2575
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2576 2577 2578

枚举,媒体类型。

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

H
huweiqi 已提交
2581 2582 2583 2584 2585 2586
| 名称  |  值 |  说明 |
| ----- |  ---- | ---- |
| FILE  |  0 | 文件 |
| IMAGE |  1 | 图片 |
| VIDEO |  2 | 视频 |
| AUDIO |  3 | 音频 |
P
panqiangbiao 已提交
2587

Z
zengyawen 已提交
2588
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2589 2590 2591

枚举,文件关键信息。

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

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

H
huweiqi 已提交
2597
| 名称          | 值              | 说明                                                       |
Z
zengyawen 已提交
2598
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
| 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 已提交
2618

Z
zengyawen 已提交
2619
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2620 2621 2622

枚举,目录类型。

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

H
huweiqi 已提交
2625 2626 2627 2628 2629 2630 2631 2632
| 名称          | 值 |  说明               |
| ------------- | --- | ------------------ |
| DIR_CAMERA    |  0 | 表示Camera文件路径 |
| DIR_VIDEO     |  1 |  表示视频路径       |
| DIR_IMAGE     |  2 | 表示图片路径       |
| DIR_AUDIO     |  3 | 表示音频路径       |
| DIR_DOCUMENTS |  4 | 表示文档路径       |
| DIR_DOWNLOAD  |  5 |  表示下载路径       |
P
panqiangbiao 已提交
2633

Z
zengyawen 已提交
2634
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2635 2636

枚举,设备类型。
2637 2638

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

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

H
huweiqi 已提交
2642 2643 2644 2645 2646 2647 2648 2649 2650
| 名称         |  值 | 说明       |
| ------------ | --- | ---------- |
| TYPE_UNKNOWN |  0 | 未识别设备 |
| TYPE_LAPTOP  |  1 | 笔记本电脑 |
| TYPE_PHONE   |  2 | 手机       |
| TYPE_TABLET  |  3 | 平板电脑   |
| TYPE_WATCH   |  4 | 智能手表   |
| TYPE_CAR     |  5 | 车载设备   |
| TYPE_TV      |  6 | 电视设备   |
P
panqiangbiao 已提交
2651

Z
zengyawen 已提交
2652
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2653 2654 2655

检索条件。

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

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

P
panqiangbiao 已提交
2667
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2668 2669

图片尺寸。
2670 2671

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

H
HelloCrease 已提交
2673 2674 2675 2676
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2677

H
huweiqi 已提交
2678
## MediaAssetOption
P
panqiangbiao 已提交
2679 2680 2681 2682 2683 2684

媒体资源选项。

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


H
huweiqi 已提交
2685 2686 2687 2688 2689
| 名称         | 类型   | 可读 | 可写 | 说明                                                         |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| 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 已提交
2690

H
huweiqi 已提交
2691
## MediaSelectOption
P
panqiangbiao 已提交
2692 2693 2694 2695 2696

媒体资源类型选项。

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

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