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

zyjhandsome's avatar
zyjhandsome 已提交
3
> **说明:**
P
panqiangbiao 已提交
4
> 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
P
panqiangbiao 已提交
5

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

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

P
panqiangbiao 已提交
13
getMediaLibrary(context: Context): MediaLibrary
P
panqiangbiao 已提交
14

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

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

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

P
panqiangbiao 已提交
21
**参数:** 
P
panqiangbiao 已提交
22

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

**返回值:**

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

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

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

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

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

46
let context = featureAbility.getContext();
47
let media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
48
```
49

Z
zengyawen 已提交
50 51 52 53 54 55
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

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

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

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

**返回值:**

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

**示例:**

```js
69
let media = mediaLibrary.getMediaLibrary();
Z
zengyawen 已提交
70 71
```

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

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


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

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

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

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

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

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

**示例:**

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

Z
zengyawen 已提交
148
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
149

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

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

P
panqiangbiao 已提交
154
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
155

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

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

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

**返回值**

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

**示例:**

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

P
panqiangbiao 已提交
220
### on<sup>8+</sup>
P
panqiangbiao 已提交
221

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

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

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

P
panqiangbiao 已提交
228 229
**参数:**

H
HelloCrease 已提交
230 231
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
232
| 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 已提交
233
| callback | Callback&lt;void&gt; | 是    | 回调返回空                                    |
P
panqiangbiao 已提交
234 235 236

**示例:**

237
```js
Z
zhang-daiyue 已提交
238
media.on('imageChange', () => {
P
panqiangbiao 已提交
239
    // image file had changed, do something
P
panqiangbiao 已提交
240 241
})
```
P
panqiangbiao 已提交
242
### off<sup>8+</sup>
P
panqiangbiao 已提交
243

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

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

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

P
panqiangbiao 已提交
250 251
**参数:**

H
HelloCrease 已提交
252 253
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
254
| 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 已提交
255
| callback | Callback&lt;void&gt; | 否    | 回调返回空                                    |
P
panqiangbiao 已提交
256 257 258

**示例:**

259
```js
潘强标 已提交
260
media.off('imageChange', () => {
H
huweiqi 已提交
261
    // stop listening successfully
P
panqiangbiao 已提交
262 263 264
})
```

B
bmeangel 已提交
265
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
266

P
panqiangbiao 已提交
267
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
268 269 270

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

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

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

P
panqiangbiao 已提交
275 276
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
302
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
303

P
panqiangbiao 已提交
304
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
305 306 307

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

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

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

P
panqiangbiao 已提交
312 313
**参数:**

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

**返回值**

Z
zengyawen 已提交
322 323 324
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | 媒体数据FileAsset |
P
panqiangbiao 已提交
325 326 327

**示例:**

328
```js
H
huweiqi 已提交
329 330 331 332 333 334 335
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 已提交
336 337
    }).catch((error) => {
        console.error('createAsset failed with error: ' + error);
H
huweiqi 已提交
338 339
    });
}
P
panqiangbiao 已提交
340 341
```

Z
zengyawen 已提交
342 343 344 345 346 347 348 349
### deleteAsset<sup>8+</sup>

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

删除媒体文件资源

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

350
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

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

**参数:**

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

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

**示例:**

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

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

删除媒体文件资源

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

397
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411

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

**参数:**

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

**示例:**

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

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

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

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

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

**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
463
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
464

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

P
panqiangbiao 已提交
467
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
468 469 470 471 472

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

**参数:**

Z
zengyawen 已提交
473 474 475
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
476 477 478

**返回值:**

Z
zengyawen 已提交
479 480 481
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
482 483 484

**示例:**

485
```js
P
panqiangbiao 已提交
486
async function example() {
P
panqiangbiao 已提交
487
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
H
huweiqi 已提交
488 489 490 491 492 493 494 495 496
    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 已提交
497 498 499
}
```

Z
zengyawen 已提交
500
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
501

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

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

P
panqiangbiao 已提交
506
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
507

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

P
panqiangbiao 已提交
510 511
**参数**

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

**示例:**

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

Z
zengyawen 已提交
535
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
536

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

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

P
panqiangbiao 已提交
541
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
542

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

P
panqiangbiao 已提交
545 546
**参数:**

Z
zengyawen 已提交
547 548 549
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
550 551 552

**返回值:**

Z
zengyawen 已提交
553 554 555
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
556 557 558

**示例:**

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

P
panqiangbiao 已提交
573
### release<sup>8+</sup>
P
panqiangbiao 已提交
574

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

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

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

P
panqiangbiao 已提交
582 583
**参数:**

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

**示例:**

590
```js
H
huweiqi 已提交
591
media.release(() => {
P
panqiangbiao 已提交
592
    // do something
P
panqiangbiao 已提交
593
});
P
panqiangbiao 已提交
594 595
```

P
panqiangbiao 已提交
596
### release<sup>8+</sup>
P
panqiangbiao 已提交
597

P
panqiangbiao 已提交
598
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
599

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

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

P
panqiangbiao 已提交
605 606
**返回值:**

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

**示例:**

613
```js
P
panqiangbiao 已提交
614
media.release()
P
panqiangbiao 已提交
615 616
```

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

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

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

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

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

**参数:**

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

**示例:**

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


Z
update  
zengyawen 已提交
653
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
654 655 656 657 658

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

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

Z
update  
zengyawen 已提交
659
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
660 661 662 663 664

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

**参数:**

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

**返回值:**

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

**示例:**

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


Z
update  
zengyawen 已提交
692
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
693 694 695

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

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

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

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

**参数:**

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

**示例:**

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


Z
update  
zengyawen 已提交
734
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
735 736 737

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

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

740
> **说明**: <br/>从API Version 9开始废弃。建议使用[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 = [
H
huweiqi 已提交
755 756
    'datashare:///media/xxxx/2',
    'datashare:///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


Z
update  
zengyawen 已提交
774
### startImagePreview<sup>(deprecated)</sup>
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

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

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

**参数:**

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

**返回值:**

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

**示例:**

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


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

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

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

825
> **说明**: <br/>从API Version 9开始废弃。建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
826 827 828 829 830

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

**参数:**

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

**示例:**

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


Z
update  
zengyawen 已提交
854
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
855 856 857 858 859

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

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

860
> **说明**: <br/>从API Version 9开始废弃。建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
861 862 863 864 865

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

**参数:**

H
HelloCrease 已提交
866 867
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
Z
zengyawen 已提交
868
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
869 870 871

**返回值:**

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

**示例:**

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

Z
zengyawen 已提交
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
```
### getActivePeers<sup>8+</sup>

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

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
907
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有在线对端设备的PeerInfo |
Z
zengyawen 已提交
908 909 910 911 912 913 914

**示例:**

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

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

Z
zengyawen 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939 940
getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
941
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有在线对端设备的PeerInfo |
Z
zengyawen 已提交
942 943 944 945 946

**示例:**

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


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

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

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
974
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有对端设备的PeerInfo |
Z
zengyawen 已提交
975 976 977 978 979 980 981

**示例:**

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

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

Z
zengyawen 已提交
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
1008
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有对端设备的PeerInfo |
Z
zengyawen 已提交
1009 1010 1011 1012 1013

**示例:**

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

Z
zengyawen 已提交
1024
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
1025 1026 1027

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

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

Z
zengyawen 已提交
1032 1033 1034
### 属性

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

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


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

P
panqiangbiao 已提交
1063
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1064 1065 1066

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

P
panqiangbiao 已提交
1067
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1068

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

P
panqiangbiao 已提交
1071 1072
**参数:**

H
HelloCrease 已提交
1073 1074 1075
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1076 1077 1078

**示例:**

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

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

P
panqiangbiao 已提交
1103
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1104 1105 1106

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

P
panqiangbiao 已提交
1107
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1108

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

P
panqiangbiao 已提交
1111 1112
**返回值:**

H
HelloCrease 已提交
1113 1114
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1115
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1116 1117 1118

**示例:**

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

P
panqiangbiao 已提交
1139
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1140

P
panqiangbiao 已提交
1141
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1142 1143 1144

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

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

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

P
panqiangbiao 已提交
1149 1150
**参数:**

H
HelloCrease 已提交
1151 1152 1153
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1154 1155 1156

**示例:**

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

P
panqiangbiao 已提交
1176
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1177

P
panqiangbiao 已提交
1178
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1179 1180 1181

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

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

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

P
panqiangbiao 已提交
1186 1187
**返回值:**

H
HelloCrease 已提交
1188 1189
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1190
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1191 1192 1193

**示例:**

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

P
panqiangbiao 已提交
1211
### open<sup>8+</sup>
P
panqiangbiao 已提交
1212

P
panqiangbiao 已提交
1213
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1214 1215 1216

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

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

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

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

P
panqiangbiao 已提交
1223 1224
**参数**

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

**示例:**

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

P
panqiangbiao 已提交
1248
### open<sup>8+</sup>
P
panqiangbiao 已提交
1249

P
panqiangbiao 已提交
1250
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1251 1252 1253

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

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

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

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

P
panqiangbiao 已提交
1260 1261
**参数:**

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

**返回值:**

H
HelloCrease 已提交
1268 1269
| 类型                    | 说明            |
| --------------------- | ------------- |
1270
| Promise&lt;number&gt; | Promise返回文件描述符 |
P
panqiangbiao 已提交
1271 1272 1273

**示例:**

1274
```js
P
panqiangbiao 已提交
1275
async function example() {
P
panqiangbiao 已提交
1276
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1277 1278
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
H
huweiqi 已提交
1279 1280 1281 1282 1283 1284
    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 已提交
1285
}
P
panqiangbiao 已提交
1286 1287
```

P
panqiangbiao 已提交
1288
### close<sup>8+</sup>
P
panqiangbiao 已提交
1289

P
panqiangbiao 已提交
1290
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1291 1292 1293

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

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

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

P
panqiangbiao 已提交
1298 1299
**参数:**

H
HelloCrease 已提交
1300 1301 1302 1303
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1304 1305 1306

**示例:**

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

P
panqiangbiao 已提交
1334
### close<sup>8+</sup>
P
panqiangbiao 已提交
1335

P
panqiangbiao 已提交
1336
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1337 1338 1339

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

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

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

P
panqiangbiao 已提交
1344 1345
**参数:**

H
HelloCrease 已提交
1346 1347 1348
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1349 1350 1351

**返回值:**

H
HelloCrease 已提交
1352 1353
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1354
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1355 1356 1357

**示例:**

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

P
panqiangbiao 已提交
1383
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1384

P
panqiangbiao 已提交
1385
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1386 1387 1388

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

P
panqiangbiao 已提交
1389
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1390

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

P
panqiangbiao 已提交
1393 1394
**参数:**

H
HelloCrease 已提交
1395 1396 1397
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1398 1399 1400

**示例:**

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

P
panqiangbiao 已提交
1423
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1424

P
panqiangbiao 已提交
1425
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1426 1427 1428

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

P
panqiangbiao 已提交
1429
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1430

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

P
panqiangbiao 已提交
1433 1434
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
1465
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1466

P
panqiangbiao 已提交
1467
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1468 1469 1470

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

P
panqiangbiao 已提交
1471
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1472

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

P
panqiangbiao 已提交
1475 1476
**参数:**

H
HelloCrease 已提交
1477 1478 1479
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1480 1481 1482

**返回值:**

H
HelloCrease 已提交
1483 1484
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1485
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1486 1487 1488

**示例:**

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

P
panqiangbiao 已提交
1510
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1511

P
panqiangbiao 已提交
1512
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1513 1514 1515

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

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

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

P
panqiangbiao 已提交
1520 1521
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
1551
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1552

P
panqiangbiao 已提交
1553
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1554 1555 1556

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

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

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

P
panqiangbiao 已提交
1561 1562
**参数:**

H
HelloCrease 已提交
1563 1564 1565
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1566 1567 1568

**返回值:**

H
HelloCrease 已提交
1569 1570
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1571
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1572 1573 1574

**示例:**

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

P
panqiangbiao 已提交
1595
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1596

P
panqiangbiao 已提交
1597
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1598 1599 1600

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

P
panqiangbiao 已提交
1601
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1602

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

P
panqiangbiao 已提交
1605 1606
**参数:**

H
HelloCrease 已提交
1607 1608 1609
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1610 1611 1612

**示例:**

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

P
panqiangbiao 已提交
1635
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1636

P
panqiangbiao 已提交
1637
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1638 1639 1640

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

P
panqiangbiao 已提交
1641
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1642

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

P
panqiangbiao 已提交
1645 1646
**返回值:**

H
HelloCrease 已提交
1647 1648
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1649
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1650 1651 1652

**示例:**

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

P
panqiangbiao 已提交
1673
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1674

A
AOL 已提交
1675
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1676 1677 1678

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

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

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

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

P
panqiangbiao 已提交
1685 1686
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
1716
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1717

A
AOL 已提交
1718
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1719 1720 1721

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

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

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

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

P
panqiangbiao 已提交
1728 1729
**参数:**

H
HelloCrease 已提交
1730 1731 1732
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1733 1734 1735

**返回值:**

H
HelloCrease 已提交
1736 1737
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1738
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1739 1740 1741

**示例:**

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

P
panqiangbiao 已提交
1762
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1763

P
panqiangbiao 已提交
1764
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1765 1766 1767

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

P
panqiangbiao 已提交
1768
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1769

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

P
panqiangbiao 已提交
1772 1773
**参数:**

H
HelloCrease 已提交
1774 1775 1776
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1777 1778 1779

**示例:**

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

P
panqiangbiao 已提交
1802
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1803

P
panqiangbiao 已提交
1804
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1805

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

P
panqiangbiao 已提交
1808
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1809

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

P
panqiangbiao 已提交
1812 1813
**返回值:**

H
HelloCrease 已提交
1814 1815
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1816
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1817 1818 1819

**示例:**

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

Z
zengyawen 已提交
1840
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1841 1842 1843

文件检索结果集。

Z
zengyawen 已提交
1844
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1845

P
panqiangbiao 已提交
1846
getCount(): number
P
panqiangbiao 已提交
1847 1848 1849

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

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

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

H
HelloCrease 已提交
1854 1855
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1856
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1857 1858 1859

**示例**

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

Z
zengyawen 已提交
1876
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1877

P
panqiangbiao 已提交
1878
isAfterLast(): boolean
P
panqiangbiao 已提交
1879 1880 1881

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

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

P
panqiangbiao 已提交
1884 1885
**返回值**

H
HelloCrease 已提交
1886 1887
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1888
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1889 1890 1891

**示例**

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

Z
zengyawen 已提交
1916
### close<sup>7+</sup>
P
panqiangbiao 已提交
1917

P
panqiangbiao 已提交
1918
close(): void
P
panqiangbiao 已提交
1919 1920 1921

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

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

P
panqiangbiao 已提交
1924 1925
**示例**

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

Z
zengyawen 已提交
1940
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1941

P
panqiangbiao 已提交
1942
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1943 1944 1945

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

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

P
panqiangbiao 已提交
1948 1949
**参数**

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

**示例**

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

Z
zengyawen 已提交
1977
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1978

P
panqiangbiao 已提交
1979
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1980

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

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

P
panqiangbiao 已提交
1985 1986
**返回值**

Z
zengyawen 已提交
1987 1988
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1989
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1990 1991 1992

**示例**

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

Z
zengyawen 已提交
2012
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2013

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

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

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

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

P
panqiangbiao 已提交
2022 2023
**参数**

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

**示例**

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

P
panqiangbiao 已提交
2053 2054
```

Z
zengyawen 已提交
2055
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2056

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

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

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

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

P
panqiangbiao 已提交
2065 2066
**返回值**

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

**示例**

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

Z
zengyawen 已提交
2095
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2096

P
panqiangbiao 已提交
2097
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2098 2099 2100

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

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

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

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

**示例**

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

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

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

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

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

P
panqiangbiao 已提交
2140 2141
**返回值**

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

**示例**

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

Z
zengyawen 已提交
2167
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2168

P
panqiangbiao 已提交
2169
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2170 2171 2172

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

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

P
panqiangbiao 已提交
2175 2176
**参数**

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

**示例**

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

Z
zengyawen 已提交
2205
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2206

P
panqiangbiao 已提交
2207
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2208 2209 2210

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

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

P
panqiangbiao 已提交
2213 2214
**参数**

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

**返回值**

Z
zengyawen 已提交
2221 2222 2223
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2224 2225 2226

**示例**

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

Z
zengyawen 已提交
2246
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2247

P
panqiangbiao 已提交
2248
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2249 2250 2251

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

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

P
panqiangbiao 已提交
2254 2255
**参数**

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

**示例**

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

Z
zengyawen 已提交
2285
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2286

P
panqiangbiao 已提交
2287
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2288 2289 2290

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

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

P
panqiangbiao 已提交
2293 2294
**返回值**

Z
zengyawen 已提交
2295 2296
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
H
huweiqi 已提交
2297
| Promise&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2298 2299 2300

**示例**

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

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

实体相册

Z
zengyawen 已提交
2326
### 属性
P
panqiangbiao 已提交
2327

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

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

P
panqiangbiao 已提交
2340 2341 2342
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2343 2344 2345

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

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

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

P
panqiangbiao 已提交
2350 2351
**参数**

Z
zengyawen 已提交
2352 2353 2354
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2355 2356 2357

**示例**

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

P
panqiangbiao 已提交
2377
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2378

P
panqiangbiao 已提交
2379
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2380 2381 2382

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

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

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

P
panqiangbiao 已提交
2387 2388
**返回值**

H
HelloCrease 已提交
2389 2390
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2391 2392 2393 2394
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

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

Z
zengyawen 已提交
2412
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2413

P
panqiangbiao 已提交
2414
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2415 2416 2417

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

P
panqiangbiao 已提交
2418
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2419

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

P
panqiangbiao 已提交
2422 2423
**参数**

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

**示例**

2431
```js
P
panqiangbiao 已提交
2432
async function example() {
P
panqiangbiao 已提交
2433 2434 2435 2436
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2437
    let fileNoArgsfetchOp = {
H
huweiqi 已提交
2438 2439
        selections: '',
        selectionArgs: [],
Z
zhang-daiyue 已提交
2440
    }
H
huweiqi 已提交
2441
    // 获取符合检索要求的相册,返回相册列表
P
panqiangbiao 已提交
2442 2443
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
H
huweiqi 已提交
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453
    // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源
    album.getFileAssets(fileNoArgsfetchOp, (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 已提交
2454 2455 2456
}
```

Z
zengyawen 已提交
2457
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2458

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

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

P
panqiangbiao 已提交
2463
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2464

P
panqiangbiao 已提交
2465
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2466 2467 2468

**参数**

Z
zengyawen 已提交
2469
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2470
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2471
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2472 2473 2474

**返回值**

Z
zengyawen 已提交
2475 2476
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2477
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2478 2479 2480

**示例**

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

P
panqiangbiao 已提交
2505
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2506

P
panqiangbiao 已提交
2507
注册设备的信息。
2508 2509

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

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

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



Z
zengyawen 已提交
2522
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2523 2524 2525

枚举,媒体类型。

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

H
huweiqi 已提交
2528 2529 2530 2531 2532 2533
| 名称  |  值 |  说明 |
| ----- |  ---- | ---- |
| FILE  |  0 | 文件 |
| IMAGE |  1 | 图片 |
| VIDEO |  2 | 视频 |
| AUDIO |  3 | 音频 |
P
panqiangbiao 已提交
2534

Z
zengyawen 已提交
2535
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2536 2537 2538

枚举,文件关键信息。

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

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

H
huweiqi 已提交
2544
| 名称          | 值              | 说明                                                       |
Z
zengyawen 已提交
2545
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564
| 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 已提交
2565

Z
zengyawen 已提交
2566
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2567 2568 2569

枚举,目录类型。

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

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

Z
zengyawen 已提交
2581
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2582 2583

枚举,设备类型。
2584 2585

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

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

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

Z
zengyawen 已提交
2599
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2600 2601 2602

检索条件。

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

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

P
panqiangbiao 已提交
2614
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2615 2616

图片尺寸。
2617 2618

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

H
HelloCrease 已提交
2620 2621 2622 2623
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2624

Z
update  
zengyawen 已提交
2625
## MediaAssetOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2626 2627 2628

媒体资源选项。

Z
update  
zengyawen 已提交
2629
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2630 2631 2632 2633

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


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

Z
update  
zengyawen 已提交
2640
## MediaSelectOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2641 2642 2643

媒体资源类型选项。

Z
update  
zengyawen 已提交
2644
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2645 2646 2647

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

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