js-apis-medialibrary.md 91.8 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
36 37
const context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
潘强标 已提交
38 39 40
```

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

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

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

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

getMediaLibrary(): MediaLibrary

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

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

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

**返回值:**

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

**示例:**

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

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

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


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

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

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

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

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

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

**示例:**

93
```js
94 95
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
Z
zhang-daiyue 已提交
96
let imagesFetchOp = {
P
panqiangbiao 已提交
97 98 99
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
Z
zhang-daiyue 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
    if (fetchFileResult == undefined) {
        console.error('Failed to get fetchFileResult: ' + error);
        return;
    }
    const count = fetchFileResult.getCount();
    if (count < 0) {
        console.error('Failed to get count from fetchFileResult: count: ' + count);
        return;
    }
    if (count == 0) {
        console.info('The count of fetchFileResult is zero');
        return;
    }

    console.info('Get fetchFileResult success, count: ' + count);
    fetchFileResult.getFirstObject((err, fileAsset) => {
        if (fileAsset == undefined) {
            console.error('Failed to get first object: ' + err);
            return;
        }
H
huweiqi 已提交
121
        console.info('fileAsset.displayName ' + ': ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
122
        for (let i = 1; i < count; i++) {
123
            fetchFileResult.getNextObject((err, fileAsset) => {
Z
zhang-daiyue 已提交
124 125 126 127
                if (fileAsset == undefined) {
                    console.error('Failed to get next object: ' + err);
                    return;
                }
H
huweiqi 已提交
128
                console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
129
            })
Z
zhang-daiyue 已提交
130 131
        }
    });
P
panqiangbiao 已提交
132
});
P
panqiangbiao 已提交
133
```
Z
zengyawen 已提交
134
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
135

P
panqiangbiao 已提交
136
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
137 138 139

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

P
panqiangbiao 已提交
140
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
141

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

P
panqiangbiao 已提交
144 145
**参数:**

Z
zengyawen 已提交
146 147 148
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 文件检索选项 |
P
panqiangbiao 已提交
149 150 151

**返回值**

Z
zengyawen 已提交
152 153 154
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | 文件数据结果集 |
P
panqiangbiao 已提交
155 156 157

**示例:**

158
```js
159 160
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
Z
zhang-daiyue 已提交
161
let imagesFetchOp = {
P
panqiangbiao 已提交
162 163 164
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
Z
zhang-daiyue 已提交
165 166 167 168 169 170 171 172 173 174 175 176
media.getFileAssets(imagesFetchOp).then(function(fetchFileResult) {
    const count = fetchFileResult.getCount();
    if (count < 0) {
        console.error('Failed to get count from fetchFileResult: count: ' + count);
        return;
    }
    if (count == 0) {
        console.info('The count of fetchFileResult is zero');
        return;
    }
    console.info('Get fetchFileResult success, count: ' + count);
    fetchFileResult.getFirstObject().then(function(fileAsset) {
H
huweiqi 已提交
177
        console.info('fileAsset.displayName ' + ': ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
178 179
        for (let i = 1; i < count; i++) {
            fetchFileResult.getNextObject().then(function(fileAsset) {
H
huweiqi 已提交
180
                console.info('fileAsset.displayName ' + ': ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
181 182 183 184 185 186 187
            }).catch(function(err) {
                console.error('Failed to get next object: ' + err);
            })
        }
    }).catch(function(err) {
        console.error('Failed to get first object: ' + err);
    });
P
panqiangbiao 已提交
188
}).catch(function(err){
Z
zhang-daiyue 已提交
189
    console.error("Failed to get file assets: " + err);
P
panqiangbiao 已提交
190
});
P
panqiangbiao 已提交
191 192
```

P
panqiangbiao 已提交
193
### on<sup>8+</sup>
P
panqiangbiao 已提交
194

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

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

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

P
panqiangbiao 已提交
201 202
**参数:**

H
HelloCrease 已提交
203 204
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
205
| 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 已提交
206
| callback | Callback&lt;void&gt; | 是    | 回调返回空                                    |
P
panqiangbiao 已提交
207 208 209

**示例:**

210
```js
Z
zhang-daiyue 已提交
211
media.on('imageChange', () => {
P
panqiangbiao 已提交
212
    // image file had changed, do something
P
panqiangbiao 已提交
213 214
})
```
P
panqiangbiao 已提交
215
### off<sup>8+</sup>
P
panqiangbiao 已提交
216

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

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

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

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

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

**示例:**

232
```js
潘强标 已提交
233
media.off('imageChange', () => {
P
panqiangbiao 已提交
234
    // stop listening success
P
panqiangbiao 已提交
235 236 237
})
```

B
bmeangel 已提交
238
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
239

P
panqiangbiao 已提交
240
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
241 242 243

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

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

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

P
panqiangbiao 已提交
248 249
**参数:**

Z
zengyawen 已提交
250 251 252 253 254 255
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型                                                     |
| displayName  | string                                  | 是   | 展示文件名                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
256 257 258

**示例:**

259
```js
P
panqiangbiao 已提交
260 261 262 263 264
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
潘强标 已提交
265
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
P
panqiangbiao 已提交
266
        if (fileAsset != undefined) {
H
huweiqi 已提交
267
            console.info('createAsset successfully, message');
P
panqiangbiao 已提交
268
        } else {
H
huweiqi 已提交
269
            console.error('createAsset failed, message = ' + err);
P
panqiangbiao 已提交
270 271 272
        }
    });
}
P
panqiangbiao 已提交
273 274
```

P
panqiangbiao 已提交
275
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
276

P
panqiangbiao 已提交
277
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
278 279 280

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

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

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

P
panqiangbiao 已提交
285 286
**参数:**

Z
zengyawen 已提交
287 288 289 290 291
| 参数名       | 类型                     | 必填 | 说明                                                         |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | 是   | 媒体类型                                                     |
| displayName  | string                   | 是   | 展示文件名                                                   |
| relativePath | string                   | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
292 293 294

**返回值**

Z
zengyawen 已提交
295 296 297
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | 媒体数据FileAsset |
P
panqiangbiao 已提交
298 299 300

**示例:**

301
```js
H
huweiqi 已提交
302 303 304 305 306 307 308 309
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));
    }).catch((err) => {
H
huweiqi 已提交
310
        console.error('createAsset failed, message = ' + err);
H
huweiqi 已提交
311 312
    });
}
P
panqiangbiao 已提交
313 314
```

Z
zengyawen 已提交
315 316 317 318 319 320 321 322
### deleteAsset<sup>8+</sup>

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

删除媒体文件资源

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

323
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

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

**参数:**

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

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

**示例:**

```js
async function example() {
342 343
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
        console.error('asset not exist')
        return
    }
    media.deleteAsset(asset.uri).then(() => {
        console.info("deleteAsset successfully");
    }).catch((err) => {
H
huweiqi 已提交
357
        console.error("deleteAsset failed with error:"+ err);
Z
zengyawen 已提交
358 359 360 361 362 363 364 365 366 367 368
    });
}
```

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

删除媒体文件资源

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

369
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383

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

**参数:**

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

**示例:**

```js
async function example() {
384 385
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
        console.error('asset not exist')
        return
    }
    media.deleteAsset(asset.uri, (err) => {
        if (err != undefined) {
            console.info("deleteAsset successfully");
        } else {
H
huweiqi 已提交
400
            console.error("deleteAsset failed with error:"+ err);
Z
zengyawen 已提交
401 402 403 404 405
        }
    });
}
```

P
panqiangbiao 已提交
406
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
407

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

P
panqiangbiao 已提交
410
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
411 412 413 414 415

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

**参数:**

Z
zengyawen 已提交
416 417 418 419
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback 返回公共目录路径 |
P
panqiangbiao 已提交
420 421 422

**示例:**

423
```js
P
panqiangbiao 已提交
424
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
P
panqiangbiao 已提交
425
media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => {
P
panqiangbiao 已提交
426
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
427
        console.info('mediaLibraryTest : getPublicDirectory passed');
P
panqiangbiao 已提交
428
    } else {
H
huweiqi 已提交
429
        console.error('mediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
430 431 432 433
    }
});
```

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

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

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

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

**参数:**

Z
zengyawen 已提交
444 445 446
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
447 448 449

**返回值:**

Z
zengyawen 已提交
450 451 452
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
453 454 455

**示例:**

456
```js
P
panqiangbiao 已提交
457
async function example() {
P
panqiangbiao 已提交
458 459
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
    const dicResult = await media.getPublicDirectory(DIR_CAMERA);
P
panqiangbiao 已提交
460
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
461 462
        console.info('MediaLibraryTest : getPublicDirectory');
    } else {
H
huweiqi 已提交
463
        console.error('MediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
464
    }
P
panqiangbiao 已提交
465 466 467
}
```

Z
zengyawen 已提交
468
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
469

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

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

P
panqiangbiao 已提交
474
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
475

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

P
panqiangbiao 已提交
478 479
**参数**

Z
zengyawen 已提交
480 481 482 483
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册获取条件                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
484 485 486

**示例:**

487
```js
P
panqiangbiao 已提交
488 489 490 491
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
492
media.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
493 494 495 496 497
    if (albumList != undefined) {
        const album = albumList[0];
        console.info('album.albumName = ' + album.albumName);
        console.info('album.count = ' + album.count);
     } else {
H
huweiqi 已提交
498
        console.error('getAlbum fail, message = ' + err);
P
panqiangbiao 已提交
499
     }
P
panqiangbiao 已提交
500
})
P
panqiangbiao 已提交
501 502
```

Z
zengyawen 已提交
503
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
504

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

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

P
panqiangbiao 已提交
509
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
510

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

P
panqiangbiao 已提交
513 514
**参数:**

Z
zengyawen 已提交
515 516 517
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
518 519 520

**返回值:**

Z
zengyawen 已提交
521 522 523
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
524 525 526

**示例:**

527
```js
P
panqiangbiao 已提交
528 529 530 531
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
532
media.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
P
panqiangbiao 已提交
533 534
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
H
huweiqi 已提交
535
    console.error("getAlbums failed with error: " + err);
P
panqiangbiao 已提交
536
});
P
panqiangbiao 已提交
537 538
```

P
panqiangbiao 已提交
539
### release<sup>8+</sup>
P
panqiangbiao 已提交
540

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

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

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

P
panqiangbiao 已提交
548 549
**参数:**

H
HelloCrease 已提交
550 551 552
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调表示成功还是失败 |
P
panqiangbiao 已提交
553 554 555

**示例:**

556
```js
P
panqiangbiao 已提交
557
media.release((err) => {
P
panqiangbiao 已提交
558
    // do something
P
panqiangbiao 已提交
559
});
P
panqiangbiao 已提交
560 561
```

P
panqiangbiao 已提交
562
### release<sup>8+</sup>
P
panqiangbiao 已提交
563

P
panqiangbiao 已提交
564
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
565

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

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

P
panqiangbiao 已提交
571 572
**返回值:**

H
HelloCrease 已提交
573 574
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
575
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
576 577 578

**示例:**

579
```js
P
panqiangbiao 已提交
580
media.release()
P
panqiangbiao 已提交
581 582
```

Z
update  
zengyawen 已提交
583
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
584 585 586 587 588

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

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

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

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

**参数:**

H
HelloCrease 已提交
595 596 597 598
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
| callback | AsyncCallback&lt;string&gt;           | 是    | 媒体资源保存回调,返回保存成功后得到的URI。 |
P
panqiangbiao 已提交
599 600 601

**示例:**

602
```js
P
panqiangbiao 已提交
603
let option = {
潘强标 已提交
604 605 606
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
607 608 609
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (err, value) => {
    if (err) {
H
huweiqi 已提交
610
        console.error("An error occurred when storing media resources.");
P
panqiangbiao 已提交
611 612
        return;
    }
H
huweiqi 已提交
613
    console.info("Media resources stored. ");
P
panqiangbiao 已提交
614 615
    // Obtain the URI that stores media resources.
});
616
```
P
panqiangbiao 已提交
617 618


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

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

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

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

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

**参数:**

H
HelloCrease 已提交
631 632 633
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
634 635 636

**返回值:**

H
HelloCrease 已提交
637 638
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
639 640 641 642
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

643
```js
P
panqiangbiao 已提交
644
let option = {
潘强标 已提交
645 646 647
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
648 649
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
H
huweiqi 已提交
650
    console.info("Media resources stored.");
P
panqiangbiao 已提交
651 652
    // Obtain the URI that stores media resources.
}).catch((err) => {
H
huweiqi 已提交
653
    console.error("An error occurred when storing media resources.");
P
panqiangbiao 已提交
654
});
655
```
P
panqiangbiao 已提交
656 657


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

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

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

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

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

**参数:**

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

**示例:**

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


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

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

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

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

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

**参数:**

H
HelloCrease 已提交
712 713
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
714
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
715
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
716 717 718

**示例:**

719
```js
P
panqiangbiao 已提交
720
let images = [
Z
zhang-daiyue 已提交
721 722
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
723
];
H
HelloCrease 已提交
724
/* 网络图片使用方式
P
panqiangbiao 已提交
725 726 727 728
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
729
*/
P
panqiangbiao 已提交
730 731
mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => {
    if (err) {
H
huweiqi 已提交
732
        console.error("An error occurred when previewing the images.");
P
panqiangbiao 已提交
733 734
        return;
    }
H
huweiqi 已提交
735
    console.info("Succeeded in previewing the images.");
P
panqiangbiao 已提交
736
});
737
```
P
panqiangbiao 已提交
738 739


Z
update  
zengyawen 已提交
740
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
741 742 743

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

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

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

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

**参数:**

H
HelloCrease 已提交
752 753
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
754
| images | Array&lt;string&gt; | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
755
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
756 757 758

**返回值:**

H
HelloCrease 已提交
759 760
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
P
panqiangbiao 已提交
761 762 763 764
| Promise&lt;void&gt; | Promise实例,用于异步获取预览结果,失败时返回错误信息。 |

**示例:**

765
```js
P
panqiangbiao 已提交
766
let images = [
Z
zhang-daiyue 已提交
767 768
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
769
];
H
HelloCrease 已提交
770
/* 网络图片使用方式
P
panqiangbiao 已提交
771 772 773 774
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
775
*/
P
panqiangbiao 已提交
776 777
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
H
huweiqi 已提交
778
    console.info("Succeeded in previewing the images.");
P
panqiangbiao 已提交
779
}).catch((err) => {
H
huweiqi 已提交
780
    console.error("An error occurred when previewing the images.");
P
panqiangbiao 已提交
781
});
782
```
P
panqiangbiao 已提交
783 784


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

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

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

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

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

**参数:**

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

**示例:**

804
```js
Z
zhang-daiyue 已提交
805 806 807
let option : mediaLibrary.MediaSelectOption = {
    type : "media",
    count : 2
P
panqiangbiao 已提交
808
};
Z
zhang-daiyue 已提交
809
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
P
panqiangbiao 已提交
810
    if (err) {
H
huweiqi 已提交
811
        console.error("An error occurred when selecting media resources.");
P
panqiangbiao 已提交
812 813
        return;
    }
H
huweiqi 已提交
814
    console.info("Media resources selected.");
P
panqiangbiao 已提交
815 816
    // Obtain the media selection value.
});
817
```
P
panqiangbiao 已提交
818 819


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

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

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

826
> **说明**: <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 已提交
827 828 829 830 831

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

**参数:**

H
HelloCrease 已提交
832 833
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
Z
zengyawen 已提交
834
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
835 836 837

**返回值:**

H
HelloCrease 已提交
838 839
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
Z
zhang-daiyue 已提交
840
| Promise&lt;Array&lt;string&gt;&gt; | Promise实例,用于异步获取选择的媒体URI(datashare://)列表。 |
P
panqiangbiao 已提交
841 842 843

**示例:**

844
```js
Z
zhang-daiyue 已提交
845 846 847
let option : mediaLibrary.MediaSelectOption = {
    type : "media",
    count : 2
P
panqiangbiao 已提交
848
};
Z
zhang-daiyue 已提交
849
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
H
huweiqi 已提交
850
    console.info("Media resources selected.");
P
panqiangbiao 已提交
851 852
    // Obtain the media selection value.
}).catch((err) => {
H
huweiqi 已提交
853
    console.error("An error occurred when selecting media resources.");
P
panqiangbiao 已提交
854 855
});

Z
zengyawen 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
```
### getActivePeers<sup>8+</sup>

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

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
873
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有在线对端设备的PeerInfo |
Z
zengyawen 已提交
874 875 876 877 878 879 880 881 882 883 884 885 886 887

**示例:**

```js
async function example() {
    media.getActivePeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
            console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed info is undefined!')
        }
    }).catch((err) => {
H
huweiqi 已提交
888
        console.error("get distributed info failed with error:" + err);
Z
zengyawen 已提交
889 890 891 892 893
    });
}
```

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

Z
zengyawen 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908
getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

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

**示例:**

```js
async function example() {
    media.getActivePeers((err, devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
                console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
H
huweiqi 已提交
921
            console.error('get distributed fail, message = ' + err)
Z
zengyawen 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
        }
    })
}
```


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

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

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
944
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | 返回获取的所有对端设备的PeerInfo |
Z
zengyawen 已提交
945 946 947 948 949 950 951 952 953 954 955 956 957 958

**示例:**

```js
async function example() {
    media.getAllPeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
                console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed info is undefined!')
        }
    }).catch((err) => {
H
huweiqi 已提交
959
        console.error("get distributed info failed with error: " + err);
Z
zengyawen 已提交
960 961 962 963 964
    });
}
```

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

Z
zengyawen 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

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

**示例:**

```js
async function example() {
    media.getAllPeers((err, devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
            console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
H
huweiqi 已提交
992
            console.error('get distributed fail, message = ' + err)
Z
zengyawen 已提交
993 994 995
        }
    })
}
996
```
P
panqiangbiao 已提交
997

Z
zengyawen 已提交
998
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
999 1000 1001

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

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

Z
zengyawen 已提交
1006 1007 1008
### 属性

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

Z
zengyawen 已提交
1010 1011 1012
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| id                        | number                   | 是   | 否   | 文件资源编号                                           |
Z
zhang-daiyue 已提交
1013
| uri                       | string                   | 是   | 否   | 文件资源uri(如:datashare:///media/image/2)         |
Z
zengyawen 已提交
1014 1015 1016 1017 1018 1019 1020 1021
| 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 已提交
1022
| dateModified              | number                   | 是   | 否   | 修改日期(修改文件时间到1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)|
Z
zengyawen 已提交
1023 1024 1025 1026 1027 1028
| dateTaken                 | number                   | 是   | 否   | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)         |
| artist<sup>8+</sup>       | string                   | 是   | 否   | 作者                                                   |
| audioAlbum<sup>8+</sup>   | string                   | 是   | 否   | 专辑                                                   |
| width                     | number                   | 是   | 否   | 图片宽度(单位:像素)                                 |
| height                    | number                   | 是   | 否   | 图片高度(单位:像素)                                 |
| orientation               | number                   | 是   | 是   | 图片显示方向(顺时针旋转角度,如0,90,180  单位:度) |
潘强标 已提交
1029
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)                                   |
Z
zengyawen 已提交
1030 1031 1032
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
1033 1034 1035


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

P
panqiangbiao 已提交
1037
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1038 1039 1040

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

P
panqiangbiao 已提交
1041
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1042

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

P
panqiangbiao 已提交
1045 1046
**参数:**

H
HelloCrease 已提交
1047 1048 1049
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1050 1051 1052

**示例:**

1053
```js
P
panqiangbiao 已提交
1054
async function example() {
Z
zhang-daiyue 已提交
1055
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1056 1057 1058 1059
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1060 1061
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1062 1063 1064 1065 1066 1067 1068
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
1069 1070
```

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

P
panqiangbiao 已提交
1073
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1074 1075 1076

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

P
panqiangbiao 已提交
1077
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1078

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

P
panqiangbiao 已提交
1081 1082
**返回值:**

H
HelloCrease 已提交
1083 1084
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1085
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1086 1087 1088

**示例:**

1089
```js
P
panqiangbiao 已提交
1090
async function example() {
Z
zhang-daiyue 已提交
1091
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1092 1093 1094 1095
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1096 1097
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1098 1099 1100 1101 1102 1103
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory().then(function(isDirectory){
        console.info("isDirectory result:"+ isDirectory);
    }).catch(function(err){
H
huweiqi 已提交
1104
        console.error("isDirectory failed with error: " + err);
P
panqiangbiao 已提交
1105 1106
    });
}
P
panqiangbiao 已提交
1107 1108
```

P
panqiangbiao 已提交
1109
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1110

P
panqiangbiao 已提交
1111
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1112 1113 1114

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

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

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

P
panqiangbiao 已提交
1119 1120
**参数:**

H
HelloCrease 已提交
1121 1122 1123
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1124 1125 1126

**示例:**

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

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

P
panqiangbiao 已提交
1148
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1149 1150 1151

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

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

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

P
panqiangbiao 已提交
1156 1157
**返回值:**

H
HelloCrease 已提交
1158 1159
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1160
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1161 1162 1163

**示例:**

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

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

P
panqiangbiao 已提交
1183
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1184 1185 1186

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

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

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

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

P
panqiangbiao 已提交
1193 1194
**参数**

H
HelloCrease 已提交
1195 1196 1197
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
1198
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件描述符                            |
P
panqiangbiao 已提交
1199 1200 1201

**示例:**

1202
```js
P
panqiangbiao 已提交
1203
async function example() {
P
panqiangbiao 已提交
1204
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1205 1206
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1207
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1208 1209 1210 1211
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
H
huweiqi 已提交
1212
                console.error('File Open Failed!' + openError);
P
panqiangbiao 已提交
1213 1214 1215
            }
    });
}
P
panqiangbiao 已提交
1216 1217
```

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

P
panqiangbiao 已提交
1220
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1221 1222 1223

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

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

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

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

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

H
HelloCrease 已提交
1232 1233 1234
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
1235 1236 1237

**返回值:**

H
HelloCrease 已提交
1238 1239
| 类型                    | 说明            |
| --------------------- | ------------- |
1240
| Promise&lt;number&gt; | Promise返回文件描述符 |
P
panqiangbiao 已提交
1241 1242 1243

**示例:**

1244
```js
P
panqiangbiao 已提交
1245
async function example() {
P
panqiangbiao 已提交
1246
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1247 1248
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1249
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1250 1251 1252 1253 1254
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
H
huweiqi 已提交
1255
            console.error('File err!' + err);
P
panqiangbiao 已提交
1256
        });
P
panqiangbiao 已提交
1257
}
P
panqiangbiao 已提交
1258 1259
```

P
panqiangbiao 已提交
1260
### close<sup>8+</sup>
P
panqiangbiao 已提交
1261

P
panqiangbiao 已提交
1262
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1263 1264 1265

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

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

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

P
panqiangbiao 已提交
1270 1271
**参数:**

H
HelloCrease 已提交
1272 1273 1274 1275
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1276 1277 1278

**示例:**

1279
```js
P
panqiangbiao 已提交
1280
async function example() {
Z
zhang-daiyue 已提交
1281
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1282 1283 1284 1285
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1286 1287
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1288 1289 1290
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1291 1292 1293 1294
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd, (closeErr) => {
            if (closeErr != undefined) {
H
huweiqi 已提交
1295 1296
                console.error('mediaLibraryTest : close : FAIL ' + closeErr);
                console.error('mediaLibraryTest : ASSET_CALLBACK : FAIL');
Z
zhang-daiyue 已提交
1297 1298 1299 1300 1301 1302
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
H
huweiqi 已提交
1303
        console.error('File err!' + err);
P
panqiangbiao 已提交
1304 1305
    });
}
P
panqiangbiao 已提交
1306 1307
```

P
panqiangbiao 已提交
1308
### close<sup>8+</sup>
P
panqiangbiao 已提交
1309

P
panqiangbiao 已提交
1310
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1311 1312 1313

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

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

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

P
panqiangbiao 已提交
1318 1319
**参数:**

H
HelloCrease 已提交
1320 1321 1322
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1323 1324 1325

**返回值:**

H
HelloCrease 已提交
1326 1327
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1328
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1329 1330 1331

**示例:**

1332
```js
P
panqiangbiao 已提交
1333
async function example() {
Z
zhang-daiyue 已提交
1334
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1335 1336 1337 1338
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1339 1340
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1341 1342 1343
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1344 1345 1346 1347
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd).then((closeErr) => {
            if (closeErr != undefined) {
H
huweiqi 已提交
1348 1349
                console.error('mediaLibraryTest : close : FAIL ' + closeErr);
                console.error('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
1350

Z
zhang-daiyue 已提交
1351 1352 1353 1354 1355 1356
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
H
huweiqi 已提交
1357
        console.error('File err!' + err);
P
panqiangbiao 已提交
1358 1359
    });
}
P
panqiangbiao 已提交
1360 1361
```

P
panqiangbiao 已提交
1362
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1363

P
panqiangbiao 已提交
1364
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1365 1366 1367

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

P
panqiangbiao 已提交
1368
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1369

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

P
panqiangbiao 已提交
1372 1373
**参数:**

H
HelloCrease 已提交
1374 1375 1376
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1377 1378 1379

**示例:**

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

P
panqiangbiao 已提交
1398
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1399

P
panqiangbiao 已提交
1400
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1401 1402 1403

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

P
panqiangbiao 已提交
1404
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1405

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

P
panqiangbiao 已提交
1408 1409
**参数:**

H
HelloCrease 已提交
1410 1411 1412 1413
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1414 1415 1416

**示例:**

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

P
panqiangbiao 已提交
1436
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1437

P
panqiangbiao 已提交
1438
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1439 1440 1441

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

P
panqiangbiao 已提交
1442
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1443

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

P
panqiangbiao 已提交
1446 1447
**参数:**

H
HelloCrease 已提交
1448 1449 1450
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1451 1452 1453

**返回值:**

H
HelloCrease 已提交
1454 1455
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1456
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1457 1458 1459

**示例:**

1460
```js
P
panqiangbiao 已提交
1461
async function example() {
1462
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1463 1464
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
潘强标 已提交
1465 1466 1467 1468
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1469
    };
Z
zhang-daiyue 已提交
1470
    let size = { width: 720, height: 720 };
P
panqiangbiao 已提交
1471 1472
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
潘强标 已提交
1473 1474
    asset.getThumbnail(size)
    .then((pixelmap) => {
Z
zengyawen 已提交
1475
        console.info('mediaLibraryTest : getThumbnail Successful '+ pixelmap);
潘强标 已提交
1476 1477
    })
    .catch((err) => {
H
huweiqi 已提交
1478
        console.error('mediaLibraryTest : getThumbnail fail, err: ' + err);
P
panqiangbiao 已提交
1479 1480
    });
}
P
panqiangbiao 已提交
1481 1482
```

P
panqiangbiao 已提交
1483
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1484

P
panqiangbiao 已提交
1485
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1486 1487 1488

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

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

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

P
panqiangbiao 已提交
1493 1494
**参数:**

H
HelloCrease 已提交
1495 1496 1497 1498
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1499 1500 1501

**示例:**

1502
```js
P
panqiangbiao 已提交
1503
async function example() {
1504
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1505 1506 1507 1508
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1509 1510
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1511 1512 1513 1514 1515 1516 1517
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
1518 1519
```

P
panqiangbiao 已提交
1520
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1521

P
panqiangbiao 已提交
1522
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1523 1524 1525

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

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

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

P
panqiangbiao 已提交
1530 1531
**参数:**

H
HelloCrease 已提交
1532 1533 1534
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1535 1536 1537

**返回值:**

H
HelloCrease 已提交
1538 1539
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1540
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1541 1542 1543

**示例:**

1544
```js
P
panqiangbiao 已提交
1545
async function example() {
1546
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1547 1548 1549 1550
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1551 1552
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1553 1554 1555 1556 1557 1558
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true).then(function() {
        console.info("favorite successfully");
    }).catch(function(err){
H
huweiqi 已提交
1559
        console.error("favorite failed with error: " + err);
P
panqiangbiao 已提交
1560 1561
    });
}
P
panqiangbiao 已提交
1562 1563
```

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

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

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

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

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

P
panqiangbiao 已提交
1574 1575
**参数:**

H
HelloCrease 已提交
1576 1577 1578
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1579 1580 1581

**示例:**

1582
```js
P
panqiangbiao 已提交
1583
async function example() {
1584
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1585 1586 1587 1588
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1589 1590
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite((err, isFavorite) => {
        if (isFavorite) {
            console.info('FileAsset is favorite');
        }else{
            console.info('FileAsset is not favorite');
        }
    });
}
P
panqiangbiao 已提交
1602 1603
```

P
panqiangbiao 已提交
1604
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1605

P
panqiangbiao 已提交
1606
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1607 1608 1609

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

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

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

P
panqiangbiao 已提交
1614 1615
**返回值:**

H
HelloCrease 已提交
1616 1617
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1618
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1619 1620 1621

**示例:**

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

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

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

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

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

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

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

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

H
HelloCrease 已提交
1656 1657 1658 1659
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1660 1661 1662

**示例:**

1663
```js
P
panqiangbiao 已提交
1664
async function example() {
1665
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1666 1667 1668 1669
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1670 1671
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1672 1673 1674 1675 1676
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1677
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1678
    }
P
panqiangbiao 已提交
1679
}
P
panqiangbiao 已提交
1680 1681
```

P
panqiangbiao 已提交
1682
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1683

A
AOL 已提交
1684
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1685 1686 1687

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

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

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

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

P
panqiangbiao 已提交
1694 1695
**参数:**

H
HelloCrease 已提交
1696 1697 1698
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1699 1700 1701

**返回值:**

H
HelloCrease 已提交
1702 1703
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1704
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1705 1706 1707

**示例:**

1708
```js
P
panqiangbiao 已提交
1709
async function example() {
1710
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1711 1712 1713 1714
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1715 1716
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1717 1718 1719 1720 1721 1722
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true).then(function() {
        console.info("trash successfully");
    }).catch(function(err){
H
huweiqi 已提交
1723
        console.error("trash failed with error: " + err);
P
panqiangbiao 已提交
1724 1725
    });
}
P
panqiangbiao 已提交
1726 1727
```

P
panqiangbiao 已提交
1728
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1729

P
panqiangbiao 已提交
1730
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1731 1732 1733

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

P
panqiangbiao 已提交
1734
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1735

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

P
panqiangbiao 已提交
1738 1739
**参数:**

H
HelloCrease 已提交
1740 1741 1742
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1743 1744 1745

**示例:**

1746
```js
P
panqiangbiao 已提交
1747
async function example() {
1748
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1749 1750 1751 1752
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1753 1754
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1755 1756 1757
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1758 1759 1760 1761 1762 1763 1764
    asset.isTrash((err, isTrash) => {
      if (isTrash == undefined) {
        console.error('Failed to get trash state: ' + err);
        return;
      }
      console.info('Get trash state success: ' + isTrash);
    });
P
panqiangbiao 已提交
1765
}
P
panqiangbiao 已提交
1766 1767
```

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

P
panqiangbiao 已提交
1770
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1771

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

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

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

P
panqiangbiao 已提交
1778 1779
**返回值:**

H
HelloCrease 已提交
1780 1781
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1782
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1783 1784 1785

**示例:**

1786
```js
P
panqiangbiao 已提交
1787
async function example() {
1788
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1789 1790 1791 1792
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1793
      order: fileKeyObj.DATE_ADDED + " DESC",
P
panqiangbiao 已提交
1794 1795 1796 1797
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash().then(function(isTrash){
Z
zhang-daiyue 已提交
1798
      console.info("isTrash result: " + isTrash);
P
panqiangbiao 已提交
1799
    }).catch(function(err){
Z
zhang-daiyue 已提交
1800
      console.error("isTrash failed with error: " + err);
P
panqiangbiao 已提交
1801 1802
    });
}
P
panqiangbiao 已提交
1803 1804
```

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

文件检索结果集。

Z
zengyawen 已提交
1809
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1810

P
panqiangbiao 已提交
1811
getCount(): number
P
panqiangbiao 已提交
1812 1813 1814

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

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

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

H
HelloCrease 已提交
1819 1820
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1821
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1822 1823 1824

**示例**

1825
```js
P
panqiangbiao 已提交
1826
async function example() {
1827
    let fileKeyObj = mediaLibrary.FileKey;
Z
zhang-daiyue 已提交
1828
    let fileType = mediaLibrary.MediaType.FILE;
P
panqiangbiao 已提交
1829 1830 1831
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1832 1833
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1834 1835 1836 1837
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1838 1839
```

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

P
panqiangbiao 已提交
1842
isAfterLast(): boolean
P
panqiangbiao 已提交
1843 1844 1845

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

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

P
panqiangbiao 已提交
1848 1849
**返回值**

H
HelloCrease 已提交
1850 1851
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1852
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1853 1854 1855

**示例**

1856
```js
P
panqiangbiao 已提交
1857
async function example() {
1858
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1859 1860 1861 1862
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1863 1864
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1865 1866 1867
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1868
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1869 1870 1871 1872
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1873
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1874
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1875 1876
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1877 1878 1879
              fetchFileResult.close();
            }
    }
P
panqiangbiao 已提交
1880
}
P
panqiangbiao 已提交
1881 1882
```

Z
zengyawen 已提交
1883
### close<sup>7+</sup>
P
panqiangbiao 已提交
1884

P
panqiangbiao 已提交
1885
close(): void
P
panqiangbiao 已提交
1886 1887 1888

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

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

P
panqiangbiao 已提交
1891 1892
**示例**

1893
```js
P
panqiangbiao 已提交
1894
async function example() {
1895
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1896 1897 1898 1899
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1900 1901
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1902 1903 1904 1905
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1906 1907
```

Z
zengyawen 已提交
1908
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1909

P
panqiangbiao 已提交
1910
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1911 1912 1913

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

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

P
panqiangbiao 已提交
1916 1917
**参数**

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

**示例**

1924
```js
P
panqiangbiao 已提交
1925
async function example() {
1926
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1927 1928 1929 1930
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1931 1932
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1933 1934
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
1935
    fetchFileResult.getFirstObject((err, fileAsset) => {
P
panqiangbiao 已提交
1936 1937 1938 1939
       if (err) {
           console.error('Failed ');
           return;
       }
H
huweiqi 已提交
1940
       console.info('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
1941 1942
    })
}
P
panqiangbiao 已提交
1943 1944
```

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

P
panqiangbiao 已提交
1947
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1948

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

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

P
panqiangbiao 已提交
1953 1954
**返回值**

Z
zengyawen 已提交
1955 1956
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1957
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1958 1959 1960

**示例**

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

Z
zengyawen 已提交
1980
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1981

P
panqiangbiao 已提交
1982
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1983 1984 1985

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

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

P
panqiangbiao 已提交
1988 1989
**参数**

Z
zengyawen 已提交
1990 1991 1992
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1993 1994 1995

**示例**

1996
```js
P
panqiangbiao 已提交
1997
async function example() {
1998
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1999 2000 2001 2002
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2003 2004
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2005 2006
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2007
    fetchFileResult.getNextObject((err, fileAsset) => {
P
panqiangbiao 已提交
2008 2009 2010 2011
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2012
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2013 2014
    })
}
P
panqiangbiao 已提交
2015 2016
```

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

P
panqiangbiao 已提交
2019
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2020 2021 2022

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

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

P
panqiangbiao 已提交
2025 2026
**返回值**

Z
zengyawen 已提交
2027 2028 2029
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2030 2031 2032

**示例**

2033
```js
P
panqiangbiao 已提交
2034
async function example() {
2035
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2036 2037 2038 2039
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2040 2041
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2042 2043 2044
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
2045
    console.info('mediaLibraryTest : count:' + fetchCount);
Z
zhang-daiyue 已提交
2046
    let fileAsset = await fetchFileResult.getNextObject();
P
panqiangbiao 已提交
2047
}
P
panqiangbiao 已提交
2048 2049
```

Z
zengyawen 已提交
2050
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2051

P
panqiangbiao 已提交
2052
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2053 2054 2055

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

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

P
panqiangbiao 已提交
2058 2059
**参数**

Z
zengyawen 已提交
2060 2061
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
Z
zengyawen 已提交
2062
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2063 2064 2065

**示例**

2066
```js
P
panqiangbiao 已提交
2067
async function example() {
2068
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2069 2070 2071 2072
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2073 2074
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2075 2076
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2077
    fetchFileResult.getLastObject((err, fileAsset) => {
P
panqiangbiao 已提交
2078 2079 2080 2081
       if (err) {
           console.error('Failed ');
           return;
       }
H
huweiqi 已提交
2082
       console.info('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2083 2084
    })
}
P
panqiangbiao 已提交
2085 2086
```

Z
zengyawen 已提交
2087
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2088

P
panqiangbiao 已提交
2089
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2090 2091 2092

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

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

P
panqiangbiao 已提交
2095 2096
**返回值**

Z
zengyawen 已提交
2097 2098 2099
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2100 2101 2102

**示例**

2103
```js
P
panqiangbiao 已提交
2104
async function example() {
2105
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2106 2107 2108 2109
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2110 2111
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2112 2113 2114 2115
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
2116 2117
```

Z
zengyawen 已提交
2118
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2119

P
panqiangbiao 已提交
2120
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2121 2122 2123

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

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

P
panqiangbiao 已提交
2126 2127
**参数**

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

**示例**

2135
```js
P
panqiangbiao 已提交
2136
async function example() {
2137
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2138 2139 2140 2141
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2142 2143
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2144 2145
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2146
    fetchFileResult.getPositionObject(0, (err, fileAsset) => {
P
panqiangbiao 已提交
2147 2148 2149 2150
       if (err) {
           console.error('Failed ');
           return;
       }
H
huweiqi 已提交
2151
       console.info('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2152 2153
    })
}
P
panqiangbiao 已提交
2154 2155
```

Z
zengyawen 已提交
2156
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2157

P
panqiangbiao 已提交
2158
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2159 2160 2161

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

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

P
panqiangbiao 已提交
2164 2165
**参数**

Z
zengyawen 已提交
2166
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
2167 2168
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
2169 2170 2171

**返回值**

Z
zengyawen 已提交
2172 2173 2174
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2175 2176 2177

**示例**

2178
```js
P
panqiangbiao 已提交
2179
async function example() {
2180
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2181 2182 2183 2184
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2185 2186
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2187 2188
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2189
    fetchFileResult.getPositionObject(1) .then(function (fileAsset){
H
huweiqi 已提交
2190
        console.info('fileAsset.displayName : ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
2191
    }).catch(function (err) {
H
huweiqi 已提交
2192
        console.error("getFileAssets failed with error: " + err);
Z
zhang-daiyue 已提交
2193
    });
P
panqiangbiao 已提交
2194
}
P
panqiangbiao 已提交
2195 2196
```

Z
zengyawen 已提交
2197
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2198

P
panqiangbiao 已提交
2199
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2200 2201 2202

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

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

P
panqiangbiao 已提交
2205 2206
**参数**

Z
zengyawen 已提交
2207
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
2208
| -------- | ---------------------------------------- | ---- | -------------------- |
Z
zengyawen 已提交
2209
| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
2210 2211 2212

**示例**

2213
```js
P
panqiangbiao 已提交
2214
async function example() {
2215
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2216 2217 2218 2219
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2220 2221
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2222 2223
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2224
    fetchFileResult.getAllObject((err, fileAsset) => {
2225
        if (err) {
P
panqiangbiao 已提交
2226 2227
           console.error('Failed ');
           return;
2228 2229
        }
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
H
huweiqi 已提交
2230
            console.info('fileAsset.displayName : ' + fileAsset[i].displayName);
2231
        } 
P
panqiangbiao 已提交
2232 2233
    })
}
P
panqiangbiao 已提交
2234 2235
```

Z
zengyawen 已提交
2236
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2237

P
panqiangbiao 已提交
2238
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2239 2240 2241

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

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

P
panqiangbiao 已提交
2244 2245
**返回值**

Z
zengyawen 已提交
2246 2247 2248
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2249 2250 2251

**示例**

2252
```js
P
panqiangbiao 已提交
2253
async function example() {
2254
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2255 2256 2257 2258
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2259 2260
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2261 2262 2263 2264
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
P
panqiangbiao 已提交
2265 2266
```

Z
zengyawen 已提交
2267
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2268 2269 2270

实体相册

Z
zengyawen 已提交
2271
### 属性
P
panqiangbiao 已提交
2272

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

Z
zengyawen 已提交
2275
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2276
| ------------ | ------ | ---- | ---- | ------- |
Z
zengyawen 已提交
2277 2278 2279
| albumId | number | 是    | 否    | 相册编号    |
| albumName | string | 是    | 是    | 相册名称    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri   |
H
HelloCrease 已提交
2280
| dateModified | number | 是    | 否    | 修改日期    |
Z
zengyawen 已提交
2281 2282 2283
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
2284

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

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2288 2289 2290

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

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

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

P
panqiangbiao 已提交
2295 2296
**参数**

Z
zengyawen 已提交
2297 2298 2299
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2300 2301 2302

**示例**

2303
```js
P
panqiangbiao 已提交
2304
async function example() {
P
panqiangbiao 已提交
2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify((err) => {
       if (err) {
           console.error('Failed ');
           return;
       }
H
huweiqi 已提交
2317
       console.info('Modify successful.');
P
panqiangbiao 已提交
2318 2319
    })
}
P
panqiangbiao 已提交
2320 2321
```

P
panqiangbiao 已提交
2322
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2323

P
panqiangbiao 已提交
2324
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2325 2326 2327

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

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

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

P
panqiangbiao 已提交
2332 2333
**返回值**

H
HelloCrease 已提交
2334 2335
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2336 2337 2338 2339
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2340
```js
P
panqiangbiao 已提交
2341
async function example() {
P
panqiangbiao 已提交
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify().then(function() {
        console.info("commitModify successfully");
    }).catch(function(err){
H
huweiqi 已提交
2352
        console.error("commitModify failed with error: " + err);
P
panqiangbiao 已提交
2353 2354
    });
}
P
panqiangbiao 已提交
2355 2356
```

Z
zengyawen 已提交
2357
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2358

P
panqiangbiao 已提交
2359
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2360 2361 2362

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

P
panqiangbiao 已提交
2363
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2364

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

P
panqiangbiao 已提交
2367 2368
**参数**

Z
zengyawen 已提交
2369
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2370
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2371 2372
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2373 2374 2375

**示例**

2376
```js
P
panqiangbiao 已提交
2377
async function example() {
P
panqiangbiao 已提交
2378 2379 2380 2381
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2382 2383 2384 2385
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
P
panqiangbiao 已提交
2386 2387 2388 2389 2390 2391
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
P
panqiangbiao 已提交
2392 2393 2394
}
```

Z
zengyawen 已提交
2395
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2396

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

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

P
panqiangbiao 已提交
2401
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2402

P
panqiangbiao 已提交
2403
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2404 2405 2406

**参数**

Z
zengyawen 已提交
2407
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2408
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2409
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2410 2411 2412

**返回值**

Z
zengyawen 已提交
2413 2414
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2415
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2416 2417 2418

**示例**

2419
```js
P
panqiangbiao 已提交
2420
async function example() {
P
panqiangbiao 已提交
2421 2422 2423 2424
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2425 2426 2427
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
2428
    };
P
panqiangbiao 已提交
2429 2430 2431
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
H
huweiqi 已提交
2432
        console.info("getFileAssets successfully: " + JSON.stringify(albumFetchFileResult));
P
panqiangbiao 已提交
2433
    }).catch(function(err){
H
huweiqi 已提交
2434
        console.error("getFileAssets failed with error: " + err);
P
panqiangbiao 已提交
2435 2436
    });
}
P
panqiangbiao 已提交
2437 2438
```

P
panqiangbiao 已提交
2439
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2440

P
panqiangbiao 已提交
2441
注册设备的信息。
2442 2443

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

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

Z
zengyawen 已提交
2447 2448 2449 2450 2451 2452
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2453 2454 2455



Z
zengyawen 已提交
2456
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2457 2458 2459

枚举,媒体类型。

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

H
huweiqi 已提交
2462 2463 2464 2465 2466 2467
| 名称  |  值 |  说明 |
| ----- |  ---- | ---- |
| FILE  |  0 | 文件 |
| IMAGE |  1 | 图片 |
| VIDEO |  2 | 视频 |
| AUDIO |  3 | 音频 |
P
panqiangbiao 已提交
2468

Z
zengyawen 已提交
2469
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2470 2471 2472

枚举,文件关键信息。

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

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

H
huweiqi 已提交
2478
| 名称          | 值              | 说明                                                       |
Z
zengyawen 已提交
2479
| ------------- | ------------------- | ---------------------------------------------------------- |
2480 2481 2482 2483 2484 2485 2486 2487
| ID            | "file_id"             | 文件编号                                                   |
| RELATIVE_PATH | "relative_path"       | 相对公共目录路径                                           |
| DISPLAY_NAME  | "display_name"        | 显示名字                                                   |
| PARENT        | "parent"              | 父目录id                                                   |
| MIME_TYPE     | "mime_type"           | 文件扩展属性                                               |
| MEDIA_TYPE    | "media_type"          | 媒体类型                                                   |
| SIZE          | "size"                | 文件大小(单位:字节)                                     |
| DATE_ADDED    | "date_added"          | 添加日期(添加文件时间到1970年1月1日的秒数值)             |
H
huweiqi 已提交
2488
| DATE_MODIFIED | "date_modified"       | 修改日期(修改文件时间到1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新) |
2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
| 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 已提交
2499

Z
zengyawen 已提交
2500
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2501 2502 2503

枚举,目录类型。

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

H
huweiqi 已提交
2506 2507 2508 2509 2510 2511 2512 2513
| 名称          | 值 |  说明               |
| ------------- | --- | ------------------ |
| DIR_CAMERA    |  0 | 表示Camera文件路径 |
| DIR_VIDEO     |  1 |  表示视频路径       |
| DIR_IMAGE     |  2 | 表示图片路径       |
| DIR_AUDIO     |  3 | 表示音频路径       |
| DIR_DOCUMENTS |  4 | 表示文档路径       |
| DIR_DOWNLOAD  |  5 |  表示下载路径       |
P
panqiangbiao 已提交
2514

Z
zengyawen 已提交
2515
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2516 2517

枚举,设备类型。
2518 2519

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

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

H
huweiqi 已提交
2523 2524 2525 2526 2527 2528 2529 2530 2531
| 名称         |  值 | 说明       |
| ------------ | --- | ---------- |
| TYPE_UNKNOWN |  0 | 未识别设备 |
| TYPE_LAPTOP  |  1 | 笔记本电脑 |
| TYPE_PHONE   |  2 | 手机       |
| TYPE_TABLET  |  3 | 平板电脑   |
| TYPE_WATCH   |  4 | 智能手表   |
| TYPE_CAR     |  5 | 车载设备   |
| TYPE_TV      |  6 | 电视设备   |
P
panqiangbiao 已提交
2532

Z
zengyawen 已提交
2533
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2534 2535 2536

检索条件。

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

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

P
panqiangbiao 已提交
2548
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2549 2550

图片尺寸。
2551 2552

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

H
HelloCrease 已提交
2554 2555 2556 2557
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2558

Z
update  
zengyawen 已提交
2559
## MediaAssetOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2560 2561 2562

媒体资源选项。

Z
update  
zengyawen 已提交
2563
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2564 2565 2566 2567

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


H
huweiqi 已提交
2568 2569 2570 2571 2572
| 名称         | 类型   | 可读 | 可写 | 说明                                                         |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| 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 已提交
2573

Z
update  
zengyawen 已提交
2574
## MediaSelectOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2575 2576 2577

媒体资源类型选项。

Z
update  
zengyawen 已提交
2578
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2579 2580 2581

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

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