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

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

Z
zengyawen 已提交
10
## 导入模块
H
huweiqi 已提交
11

12
```js
13
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
P
panqiangbiao 已提交
14 15
```

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

P
panqiangbiao 已提交
18
getMediaLibrary(context: Context): MediaLibrary
P
panqiangbiao 已提交
19

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

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

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

H
huweiqi 已提交
26
**参数:**
P
panqiangbiao 已提交
27

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

**返回值:**

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

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

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

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

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

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

Z
zengyawen 已提交
55 56 57 58 59 60
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

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

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

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

**返回值:**

| 类型                          | 说明       |
| ----------------------------- | :--------- |
H
huweiqi 已提交
69
| [MediaLibrary](#medialibrary) | 媒体库实例。 |
Z
zengyawen 已提交
70 71 72 73

**示例:**

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

P
panqiangbiao 已提交
77
## MediaLibrary
P
panqiangbiao 已提交
78

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

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

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

H
huweiqi 已提交
85 86 87
> **说明:**
> 在API version 10上,摒弃了物理目录作为相册的设计,采用了逻辑相册的设计,一个相册中可以添加多个文件,一个文件也可以在多个相册中呈现。新的设计将带来parent、albumId、albumUri和albumName属性使用上的不兼容,无法作为MediaFetchOptions的参数在getFileAssets接口中使用。请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

P
panqiangbiao 已提交
88
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
89

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

P
panqiangbiao 已提交
92 93
**参数:**

Z
zengyawen 已提交
94 95
| 参数名   | 类型                                                | 必填 | 说明                              |
| -------- | --------------------------------------------------- | ---- | --------------------------------- |
H
huweiqi 已提交
96 97
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 文件检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | callback返回文件检索结果集。 |
P
panqiangbiao 已提交
98 99 100

**示例:**

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

Z
zengyawen 已提交
150
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
151

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

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

H
huweiqi 已提交
156 157 158
> **说明:**
> 在API version 10上,摒弃了物理目录作为相册的设计,采用了逻辑相册的设计,一个相册中可以添加多个文件,一个文件也可以在多个相册中呈现。新的设计将带来parent、albumId、albumUri和albumName属性使用上的不兼容,无法作为MediaFetchOptions的参数在getFileAssets接口中使用。请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

P
panqiangbiao 已提交
159
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
160

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

P
panqiangbiao 已提交
163 164
**参数:**

Z
zengyawen 已提交
165 166
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
H
huweiqi 已提交
167
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 文件检索选项。 |
P
panqiangbiao 已提交
168

H
huweiqi 已提交
169
**返回值:**
P
panqiangbiao 已提交
170

Z
zengyawen 已提交
171 172
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
H
huweiqi 已提交
173
| Promise&lt;[FetchFileResult](#fetchfileresult7)&gt; | Promise对象,返回文件检索结果集。 |
P
panqiangbiao 已提交
174 175 176

**示例:**

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

P
panqiangbiao 已提交
222
### on<sup>8+</sup>
P
panqiangbiao 已提交
223

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

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

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

P
panqiangbiao 已提交
230 231
**参数:**

H
HelloCrease 已提交
232 233
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
234 235
| 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;注册设备上文件变更。 |
| callback | Callback&lt;void&gt; | 是    | callbac返回空。                                    |
P
panqiangbiao 已提交
236 237 238

**示例:**

239
```js
Z
zhang-daiyue 已提交
240
media.on('imageChange', () => {
H
huweiqi 已提交
241 242
  // image file had changed, do something.
});
P
panqiangbiao 已提交
243
```
H
huweiqi 已提交
244

P
panqiangbiao 已提交
245
### off<sup>8+</sup>
P
panqiangbiao 已提交
246

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

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

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

P
panqiangbiao 已提交
253 254
**参数:**

H
HelloCrease 已提交
255 256
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
257 258
| 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;注册设备上文件变更。 |
| callback | Callback&lt;void&gt; | 否    | callback返回空。                                    |
P
panqiangbiao 已提交
259 260 261

**示例:**

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

B
bmeangel 已提交
268
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
269

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

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

H
huweiqi 已提交
274 275 276
> **说明:**
> 由于API version 10的SDK上relativePath和相册没有关联关系,文件创建成功后,relativePath的最后一级目录不会作为相册呈现。变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

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

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

P
panqiangbiao 已提交
281 282
**参数:**

Z
zengyawen 已提交
283 284
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
H
huweiqi 已提交
285 286 287 288
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型。                                                     |
| displayName  | string                                  | 是   | 展示文件名。                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径。 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | callback返回创建的媒体资源FileAsset对象。                          |
P
panqiangbiao 已提交
289 290 291

**示例:**

292
```js
P
panqiangbiao 已提交
293
async function example() {
H
huweiqi 已提交
294 295 296 297 298 299 300 301 302 303 304
  // 使用Callback方式创建Image类型文件。
  let mediaType = mediaLibrary.MediaType.IMAGE;
  let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
  const path = await media.getPublicDirectory(DIR_IMAGE);
  media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (error, fileAsset) => {
    if (fileAsset != undefined) {
      console.info('createAsset successfully, message');
    } else {
      console.error('createAsset failed with error: ' + error);
    }
  });
P
panqiangbiao 已提交
305
}
P
panqiangbiao 已提交
306 307
```

P
panqiangbiao 已提交
308
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
309

P
panqiangbiao 已提交
310
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
311 312 313

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

H
huweiqi 已提交
314 315 316
> **说明:**
> 由于API version 10的SDK上relativePath和相册没有关联关系,文件创建成功后,relativePath的最后一级目录不会作为相册呈现。变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

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

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

P
panqiangbiao 已提交
321 322
**参数:**

Z
zengyawen 已提交
323 324
| 参数名       | 类型                     | 必填 | 说明                                                         |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
H
huweiqi 已提交
325 326 327
| mediaType    | [MediaType](#mediatype8) | 是   | 媒体类型。                                                     |
| displayName  | string                   | 是   | 展示文件名。                                                   |
| relativePath | string                   | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path。 |
P
panqiangbiao 已提交
328

H
huweiqi 已提交
329
**返回值:**
P
panqiangbiao 已提交
330

Z
zengyawen 已提交
331 332
| 类型                     | 说明              |
| ------------------------ | ----------------- |
H
huweiqi 已提交
333
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise对象,返回创建媒体数据的FileAsset。 |
P
panqiangbiao 已提交
334 335 336

**示例:**

337
```js
H
huweiqi 已提交
338
async function example() {
H
huweiqi 已提交
339 340 341 342 343 344 345 346 347
  // 使用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((error) => {
    console.error('createAsset failed with error: ' + error);
  });
H
huweiqi 已提交
348
}
P
panqiangbiao 已提交
349 350
```

Z
zengyawen 已提交
351 352 353 354
### deleteAsset<sup>8+</sup>

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

H
huweiqi 已提交
355
删除媒体文件资源。
Z
zengyawen 已提交
356 357 358

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

359
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
360 361 362 363 364 365 366

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

**参数:**

| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
H
huweiqi 已提交
367
| uri | string | 是    | 需要删除的媒体文件资源的uri。 |
Z
zengyawen 已提交
368 369

**返回值:**
H
huweiqi 已提交
370

Z
zengyawen 已提交
371 372
| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
373
| Promise&lt;void&gt; | Promise对象,返回删除的结果。 |
Z
zengyawen 已提交
374 375 376 377 378

**示例:**

```js
async function example() {
H
huweiqi 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
  let fileKeyObj = mediaLibrary.FileKey;
  let fileType = mediaLibrary.MediaType.FILE;
  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((error) => {
    console.error('deleteAsset failed with error: ' + error);
  });
  fetchFileResult.close();
Z
zengyawen 已提交
397 398 399 400
}
```

### deleteAsset<sup>8+</sup>
H
huweiqi 已提交
401

Z
zengyawen 已提交
402 403
deleteAsset(uri: string, callback: AsyncCallback\<void>): void

H
huweiqi 已提交
404
删除媒体文件资源。
Z
zengyawen 已提交
405 406 407

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

408
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
409 410 411 412 413 414 415 416

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

**参数:**

| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | 是    | 需要删除的媒体文件资源的uri。 |
H
huweiqi 已提交
417
|callback |AsyncCallback\<void>| 是  |callback返回空。|
Z
zengyawen 已提交
418 419 420 421 422

**示例:**

```js
async function example() {
H
huweiqi 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  let fileKeyObj = mediaLibrary.FileKey;
  let fileType = mediaLibrary.MediaType.FILE;
  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, (error) => {
    if (error != undefined) {
      console.error('deleteAsset failed with error: ' + error);
    } else {
      console.info('deleteAsset successfully');
Z
zengyawen 已提交
440
    }
H
huweiqi 已提交
441 442
  });
  fetchFileResult.close();
Z
zengyawen 已提交
443 444 445
}
```

P
panqiangbiao 已提交
446
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
447

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

P
panqiangbiao 已提交
450
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
451 452 453 454 455

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

**参数:**

Z
zengyawen 已提交
456 457
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
H
huweiqi 已提交
458 459
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型。              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback返回公共目录路径。 |
P
panqiangbiao 已提交
460 461 462

**示例:**

463
```js
P
panqiangbiao 已提交
464
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
H
huweiqi 已提交
465
media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => {
H
huweiqi 已提交
466 467 468 469 470
  if (dicResult == 'Camera/') {
    console.info('getPublicDirectory DIR_CAMERA successfully');
  } else {
    console.error('getPublicDirectory DIR_CAMERA failed with error: ' + error);
  }
P
panqiangbiao 已提交
471 472 473
});
```

P
panqiangbiao 已提交
474
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
475

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

P
panqiangbiao 已提交
478
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
479 480 481 482 483

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

**参数:**

Z
zengyawen 已提交
484 485
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
H
huweiqi 已提交
486
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型。 |
P
panqiangbiao 已提交
487 488 489

**返回值:**

Z
zengyawen 已提交
490 491
| 类型             | 说明             |
| ---------------- | ---------------- |
H
huweiqi 已提交
492
| Promise\<string> | Promise对象,返回公共目录路径。 |
P
panqiangbiao 已提交
493 494 495

**示例:**

496
```js
P
panqiangbiao 已提交
497
async function example() {
H
huweiqi 已提交
498 499 500 501 502 503 504 505 506 507
  let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
  media.getPublicDirectory(DIR_CAMERA).then((dicResult) => {
    if (dicResult == 'Camera/') {
      console.info('getPublicDirectory DIR_CAMERA successfully');
    } else {
      console.error('getPublicDirectory DIR_CAMERA failed');
    }
  }).catch((error) => {
    console.error('getPublicDirectory failed with error: ' + error);
  });
P
panqiangbiao 已提交
508 509 510
}
```

Z
zengyawen 已提交
511
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
512

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

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

H
huweiqi 已提交
517 518 519
> **说明:**
> 由于API version 10的SDK上relativePath和相册没有关联关系,在使用getAlbums时不支持relativePath作为查询条件,当前仅支持“Camera”和“ScreenShots”两类相册,变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

P
panqiangbiao 已提交
520
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
521

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

H
huweiqi 已提交
524
**参数:**
P
panqiangbiao 已提交
525

Z
zengyawen 已提交
526 527
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
H
huweiqi 已提交
528 529
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册检索条件。                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | callback返回获取的Album结果集。 |
P
panqiangbiao 已提交
530 531 532

**示例:**

533
```js
H
huweiqi 已提交
534
async function example() {
H
huweiqi 已提交
535 536
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
H
huweiqi 已提交
537
    selectionArgs: ['Camera'],
H
huweiqi 已提交
538 539 540 541 542 543 544
  };
  media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => {
    if (albumList != undefined) {
      console.info('getAlbums successfully: ' + JSON.stringify(albumList));
    } else {
      console.error('getAlbums failed with error: ' + error);
    }
H
huweiqi 已提交
545
  });
H
huweiqi 已提交
546
}
P
panqiangbiao 已提交
547 548
```

Z
zengyawen 已提交
549
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
550

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

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

H
huweiqi 已提交
555 556 557
> **说明:**
> 由于API version 10的SDK上relativePath和相册没有关联关系,在使用getAlbums时不支持relativePath作为查询条件.当前仅支持“Camera”和“ScreenShots”两类相册,变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

P
panqiangbiao 已提交
558
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
559

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

P
panqiangbiao 已提交
562 563
**参数:**

Z
zengyawen 已提交
564 565
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
H
huweiqi 已提交
566
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件。 |
P
panqiangbiao 已提交
567 568 569

**返回值:**

Z
zengyawen 已提交
570 571
| 类型                             | 说明          |
| -------------------------------- | ------------- |
H
huweiqi 已提交
572
| Promise<Array<[Album](#album7)>> | Promise对象,返回获取的Album结果集。 |
P
panqiangbiao 已提交
573 574 575

**示例:**

576
```js
H
huweiqi 已提交
577
async function example() {
H
huweiqi 已提交
578 579
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
H
huweiqi 已提交
580
    selectionArgs: ['Camera'],
H
huweiqi 已提交
581 582 583 584 585 586
  };
  media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
    console.info('getAlbums successfully: ' + JSON.stringify(albumList));
  }).catch((error) => {
    console.error('getAlbums failed with error: ' + error);
  });
H
huweiqi 已提交
587
}
P
panqiangbiao 已提交
588 589
```

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

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

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

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

P
panqiangbiao 已提交
599 600
**参数:**

H
HelloCrease 已提交
601 602
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
603
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。 |
P
panqiangbiao 已提交
604 605 606

**示例:**

607
```js
H
huweiqi 已提交
608
media.release(() => {
H
huweiqi 已提交
609
  // do something.
P
panqiangbiao 已提交
610
});
P
panqiangbiao 已提交
611 612
```

P
panqiangbiao 已提交
613
### release<sup>8+</sup>
P
panqiangbiao 已提交
614

P
panqiangbiao 已提交
615
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
616

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

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

P
panqiangbiao 已提交
622 623
**返回值:**

H
HelloCrease 已提交
624 625
| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
626
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
627 628 629

**示例:**

630
```js
H
huweiqi 已提交
631
media.release();
P
panqiangbiao 已提交
632 633
```

H
huweiqi 已提交
634
### storeMediaAsset
P
panqiangbiao 已提交
635 636 637 638 639

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

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

H
huweiqi 已提交
640
> **说明**:此接口为API Version 6开始支持,只支持FA模型使用。
P
panqiangbiao 已提交
641 642 643 644 645

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

**参数:**

H
HelloCrease 已提交
646 647 648
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
H
huweiqi 已提交
649
| callback | AsyncCallback&lt;string&gt;           | 是    | callback返回保存媒体资源成功后得到的URI。 |
P
panqiangbiao 已提交
650 651 652

**示例:**

653
```js
P
panqiangbiao 已提交
654
let option = {
H
huweiqi 已提交
655 656 657
  src : '/data/storage/el2/base/haps/entry/image.png',
  mimeType : 'image/*',
  relativePath : 'Pictures/'
P
panqiangbiao 已提交
658
};
H
huweiqi 已提交
659
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (error, value) => {
H
huweiqi 已提交
660 661 662 663 664 665
  if (error) {
    console.error('storeMediaAsset failed with error: ' + error);
    return;
  }
  console.info('Media resources stored. ');
  // Obtain the URI that stores media resources.
P
panqiangbiao 已提交
666
});
667
```
P
panqiangbiao 已提交
668

H
huweiqi 已提交
669
### storeMediaAsset
P
panqiangbiao 已提交
670 671 672 673 674

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

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

H
huweiqi 已提交
675
> **说明**:此接口为API Version 6开始支持,只支持FA模型使用。
P
panqiangbiao 已提交
676 677 678 679 680

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

**参数:**

H
HelloCrease 已提交
681 682 683
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
684 685 686

**返回值:**

H
HelloCrease 已提交
687 688
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
H
huweiqi 已提交
689
| Promise&lt;string&gt; | Promise对象,返回保存媒体资源成功后得到的URI。 |
P
panqiangbiao 已提交
690 691 692

**示例:**

693
```js
P
panqiangbiao 已提交
694
let option = {
H
huweiqi 已提交
695 696 697
  src : '/data/storage/el2/base/haps/entry/image.png',
  mimeType : 'image/*',
  relativePath : 'Pictures/'
P
panqiangbiao 已提交
698 699
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
H
huweiqi 已提交
700 701
  console.info('Media resources stored.');
  // Obtain the URI that stores media resources.
H
huweiqi 已提交
702
}).catch((error) => {
H
huweiqi 已提交
703
  console.error('storeMediaAsset failed with error: ' + error);
P
panqiangbiao 已提交
704
});
705
```
P
panqiangbiao 已提交
706

H
huweiqi 已提交
707
### startImagePreview
P
panqiangbiao 已提交
708 709 710

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

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

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

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

**参数:**

H
HelloCrease 已提交
721 722
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
723
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI('https://','datashare://')列表。 |
H
HelloCrease 已提交
724
| index    | number                    | 是    | 开始显示的图片序号。                               |
H
huweiqi 已提交
725
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。                        |
P
panqiangbiao 已提交
726 727 728

**示例:**

729
```js
P
panqiangbiao 已提交
730
let images = [
H
huweiqi 已提交
731 732
  'file://media/xxxx/2',
  'file://media/xxxx/3'
P
panqiangbiao 已提交
733
];
H
huweiqi 已提交
734
/* 网络图片使用方式。
P
panqiangbiao 已提交
735
let images = [
H
huweiqi 已提交
736 737
  'https://media.xxxx.com/image1.jpg',
  'https://media.xxxx.com/image2.jpg'
P
panqiangbiao 已提交
738
];
H
HelloCrease 已提交
739
*/
P
panqiangbiao 已提交
740
let index = 1;
H
huweiqi 已提交
741
mediaLibrary.getMediaLibrary().startImagePreview(images, index, (error) => {
H
huweiqi 已提交
742 743 744 745 746
  if (error) {
    console.error('startImagePreview failed with error: ' + error);
    return;
  }
  console.info('Succeeded in previewing the images.');
P
panqiangbiao 已提交
747
});
748
```
P
panqiangbiao 已提交
749

H
huweiqi 已提交
750
### startImagePreview
P
panqiangbiao 已提交
751 752 753

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

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

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

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

**参数:**

H
HelloCrease 已提交
764 765
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
766
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI('https://','datashare://')列表。 |
H
huweiqi 已提交
767
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。                        |
P
panqiangbiao 已提交
768 769 770

**示例:**

771
```js
P
panqiangbiao 已提交
772
let images = [
H
huweiqi 已提交
773 774
  'file://media/xxxx/2',
  'file://media/xxxx/3'
P
panqiangbiao 已提交
775
];
H
huweiqi 已提交
776
/* 网络图片使用方式。
P
panqiangbiao 已提交
777
let images = [
H
huweiqi 已提交
778 779
  'https://media.xxxx.com/image1.jpg',
  'https://media.xxxx.com/image2.jpg'
P
panqiangbiao 已提交
780
];
H
HelloCrease 已提交
781
*/
H
huweiqi 已提交
782
mediaLibrary.getMediaLibrary().startImagePreview(images, (error) => {
H
huweiqi 已提交
783 784 785 786 787
  if (error) {
    console.error('startImagePreview failed with error: ' + error);
    return;
  }
  console.info('Succeeded in previewing the images.');
P
panqiangbiao 已提交
788
});
789
```
P
panqiangbiao 已提交
790

H
huweiqi 已提交
791
### startImagePreview
P
panqiangbiao 已提交
792 793 794

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

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

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

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

**参数:**

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

**返回值:**

H
HelloCrease 已提交
812 813
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
H
huweiqi 已提交
814
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
815 816 817

**示例:**

818
```js
P
panqiangbiao 已提交
819
let images = [
H
huweiqi 已提交
820 821
  'file://media/xxxx/2',
  'file://media/xxxx/3'
P
panqiangbiao 已提交
822
];
H
huweiqi 已提交
823
/* 网络图片使用方式。
P
panqiangbiao 已提交
824
let images = [
H
huweiqi 已提交
825 826
  'https://media.xxxx.com/image1.jpg',
  'https://media.xxxx.com/image2.jpg'
P
panqiangbiao 已提交
827
];
H
HelloCrease 已提交
828
*/
P
panqiangbiao 已提交
829 830
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
H
huweiqi 已提交
831
  console.info('Succeeded in previewing the images.');
H
huweiqi 已提交
832
}).catch((error) => {
H
huweiqi 已提交
833
  console.error('startImagePreview failed with error: ' + error);
P
panqiangbiao 已提交
834
});
835
```
P
panqiangbiao 已提交
836

H
huweiqi 已提交
837
### startMediaSelect
P
panqiangbiao 已提交
838 839 840 841 842

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

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

H
huweiqi 已提交
843
> **说明**:
H
huweiqi 已提交
844 845
> 此接口为API Version 6开始支持,只支持FA模型使用。
> 建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
846 847 848 849 850

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

**参数:**

H
HelloCrease 已提交
851 852
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
H
huweiqi 已提交
853
| option   | [MediaSelectOption](#mediaselectoption)  | 是    | 媒体选择选项。                              |
H
huweiqi 已提交
854
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | callback返回选择的媒体URI列表。 |
P
panqiangbiao 已提交
855 856 857

**示例:**

858
```js
Z
zhang-daiyue 已提交
859
let option : mediaLibrary.MediaSelectOption = {
H
huweiqi 已提交
860 861
  type : 'media',
  count : 2
P
panqiangbiao 已提交
862
};
H
huweiqi 已提交
863
mediaLibrary.getMediaLibrary().startMediaSelect(option, (error, value) => {
H
huweiqi 已提交
864 865 866 867 868 869
  if (error) {
    console.error('startMediaSelect failed with error: ' + error);
    return;
  }
  console.info('Media resources selected.');
  // Obtain the media selection value.
P
panqiangbiao 已提交
870
});
871
```
P
panqiangbiao 已提交
872

H
huweiqi 已提交
873
### startMediaSelect
P
panqiangbiao 已提交
874 875 876 877 878

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

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

H
huweiqi 已提交
879
> **说明**:
H
huweiqi 已提交
880 881
> 此接口为API Version 6开始支持,只支持FA模型使用。
> 建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
882 883 884 885 886

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

**参数:**

H
HelloCrease 已提交
887 888
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
H
huweiqi 已提交
889
| option | [MediaSelectOption](#mediaselectoption) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
890 891 892

**返回值:**

H
HelloCrease 已提交
893 894
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
H
huweiqi 已提交
895
| Promise&lt;Array&lt;string&gt;&gt; | Promise对象,返回选择的媒体URI列表。 |
P
panqiangbiao 已提交
896 897 898

**示例:**

899
```js
Z
zhang-daiyue 已提交
900
let option : mediaLibrary.MediaSelectOption = {
H
huweiqi 已提交
901 902
  type : 'media',
  count : 2
P
panqiangbiao 已提交
903
};
Z
zhang-daiyue 已提交
904
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
H
huweiqi 已提交
905 906
  console.info('Media resources selected.');
  // Obtain the media selection value.
H
huweiqi 已提交
907
}).catch((error) => {
H
huweiqi 已提交
908
  console.error('startMediaSelect failed with error: ' + error);
P
panqiangbiao 已提交
909
});
Z
zengyawen 已提交
910
```
H
huweiqi 已提交
911

Z
zengyawen 已提交
912 913 914 915
### getActivePeers<sup>8+</sup>

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

H
huweiqi 已提交
916
获取在线对端设备的信息,使用Promise方式返回异步结果。
Z
zengyawen 已提交
917 918 919 920 921 922 923 924 925 926 927

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
928
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | Promise对象,返回获取的所有在线对端设备的PeerInfo。 |
Z
zengyawen 已提交
929 930 931 932 933

**示例:**

```js
async function example() {
H
huweiqi 已提交
934 935 936 937 938 939 940 941 942
  media.getActivePeers().then((devicesInfo) => {
    if (devicesInfo != undefined) {
      console.info('get distributed info ' + JSON.stringify(devicesInfo));
    } else {
      console.info('get distributed info is undefined!');
    }
  }).catch((error) => {
    console.error('get distributed info failed with error: ' + error);
  });
Z
zengyawen 已提交
943 944 945 946
}
```

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

Z
zengyawen 已提交
948 949 950 951 952 953 954 955 956 957 958 959 960 961
getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
962
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | callback返回获取的所有在线对端设备的PeerInfo对象。 |
Z
zengyawen 已提交
963 964 965 966 967

**示例:**

```js
async function example() {
H
huweiqi 已提交
968 969 970 971 972 973 974
  media.getActivePeers((error, devicesInfo) => {
    if (devicesInfo != undefined) {
      console.info('get distributed info ' + JSON.stringify(devicesInfo));
    } else {
      console.error('get distributed failed with error: ' + error);
    }
  });
Z
zengyawen 已提交
975 976 977 978 979 980 981
}
```

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

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

H
huweiqi 已提交
982
获取所有对端设备的信息,使用Promise方式返回异步结果。
Z
zengyawen 已提交
983 984 985 986 987 988 989 990 991 992 993

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
994
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | Promise对象,返回获取的所有对端设备的PeerInfo。 |
Z
zengyawen 已提交
995 996 997 998 999

**示例:**

```js
async function example() {
H
huweiqi 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008
  media.getAllPeers().then((devicesInfo) => {
    if (devicesInfo != undefined) {
      console.info('get distributed info ' + JSON.stringify(devicesInfo));
    } else {
      console.info('get distributed info is undefined!');
    }
  }).catch((error) => {
    console.error('get distributed info failed with error: ' + error);
  });
Z
zengyawen 已提交
1009 1010 1011 1012
}
```

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

Z
zengyawen 已提交
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

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

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
1028
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | callback返回获取的所有对端设备的PeerInfo对象。 |
Z
zengyawen 已提交
1029 1030 1031 1032 1033

**示例:**

```js
async function example() {
H
huweiqi 已提交
1034 1035 1036 1037 1038 1039 1040
  media.getAllPeers((error, devicesInfo) => {
    if (devicesInfo != undefined) {
      console.info('get distributed info ' + JSON.stringify(devicesInfo));
    } else {
      console.error('get distributed failed with error: ' + error);
    }
  });
Z
zengyawen 已提交
1041
}
1042
```
P
panqiangbiao 已提交
1043

Z
zengyawen 已提交
1044
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
1045 1046 1047

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

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

Z
zengyawen 已提交
1053 1054 1055
### 属性

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

Z
zengyawen 已提交
1057 1058
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
H
huweiqi 已提交
1059 1060 1061 1062 1063 1064 1065
| id                        | number                   | 是   | 否   | 文件资源编号。                                           |
| uri                       | string                   | 是   | 否   | 文件资源uri(如:file://media/image/2)。         |
| mimeType                  | string                   | 是   | 否   | 文件扩展属性。                                           |
| mediaType<sup>8+</sup>    | [MediaType](#mediatype8) | 是   | 否   | 媒体类型。                                               |
| displayName               | string                   | 是   | 是   | 显示文件名,包含后缀名。                                 |
| title                     | string                   | 是   | 是   | 文件标题。                                               |
| relativePath<sup>8+</sup> | string                   | 是   | 是   | 相对公共目录路径。                                       |
H
huweiqi 已提交
1066
| parent<sup>8+</sup>       | number                   | 是   | 否   | 父目录id。由于API version 10的SDK上Asset可以存在多个相册,该属性不兼容。获取值始终为0。                        |
H
huweiqi 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
| size                      | number                   | 是   | 否   | 文件大小(单位:字节)。                                 |
| dateAdded                 | number                   | 是   | 否   | 添加日期(添加文件时间到1970年1月1日的秒数值)。         |
| dateModified              | number                   | 是   | 否   | 修改日期(修改文件时间到1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)。|
| dateTaken                 | number                   | 是   | 否   | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)。         |
| artist<sup>8+</sup>       | string                   | 是   | 否   | 作者。                                                   |
| audioAlbum<sup>8+</sup>   | string                   | 是   | 否   | 专辑。                                                   |
| width                     | number                   | 是   | 否   | 图片宽度(单位:像素)。                                 |
| height                    | number                   | 是   | 否   | 图片高度(单位:像素)。                                 |
| orientation               | number                   | 是   | 是   | 图片显示方向(顺时针旋转角度,如0,90,180  单位:度)。 |
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)。                                   |
H
huweiqi 已提交
1077 1078 1079
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号。由于API version 10的SDK上Asset可以存在多个相册,该属性不兼容。获取值始终为0。                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri。由于API version 10的SDK上Asset可以存在多个相册,该属性不兼容。获取值始终为空字符串。                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称。由于API version 10的SDK上Asset可以存在多个相册,该属性不兼容。获取值始终为空字符串。                     |
P
panqiangbiao 已提交
1080 1081

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

P
panqiangbiao 已提交
1083
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1084 1085 1086

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

P
panqiangbiao 已提交
1087
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1088

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

P
panqiangbiao 已提交
1091 1092
**参数:**

H
HelloCrease 已提交
1093 1094
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
H
huweiqi 已提交
1095
| callback | AsyncCallback&lt;boolean&gt; | 是    | callback返回boolean值,值为true则是目录,值为false则非目录。 |
P
panqiangbiao 已提交
1096 1097 1098

**示例:**

1099
```js
P
panqiangbiao 已提交
1100
async function example() {
H
huweiqi 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.isDirectory((error, isDirectory) => {
    if (error) {
      console.error('isDirectory failed with error: ' + error);
    } else {
      console.info('isDirectory result:' + isDirectory);
    }
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1118
}
P
panqiangbiao 已提交
1119 1120
```

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

P
panqiangbiao 已提交
1123
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1124 1125 1126

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

P
panqiangbiao 已提交
1127
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1128

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

P
panqiangbiao 已提交
1131 1132
**返回值:**

H
HelloCrease 已提交
1133 1134
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
H
huweiqi 已提交
1135
| Promise&lt;boolean&gt; | Promise对象,返回boolean值,值为true则是目录,值为false则非目录。 |
P
panqiangbiao 已提交
1136 1137 1138

**示例:**

1139
```js
P
panqiangbiao 已提交
1140
async function example() {
H
huweiqi 已提交
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.isDirectory().then((isDirectory) => {
    console.info('isDirectory result:' + isDirectory);
  }).catch((error) => {
    console.error('isDirectory failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1156
}
P
panqiangbiao 已提交
1157 1158
```

P
panqiangbiao 已提交
1159
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1160

P
panqiangbiao 已提交
1161
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1162 1163 1164

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

H
huweiqi 已提交
1165 1166 1167
> **说明:**
> 由于API version 10的SDK上audio没有orientation属性,在使用commitModify接口时将无法对audio资源的orientation属性进行修改。请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

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

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

P
panqiangbiao 已提交
1172 1173
**参数:**

H
HelloCrease 已提交
1174 1175
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1176
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。 |
P
panqiangbiao 已提交
1177 1178 1179

**示例:**

1180
```js
P
panqiangbiao 已提交
1181
async function example() {
H
huweiqi 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.title = 'newtitle';
  asset.commitModify(() => {
    console.info('commitModify successfully');
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1196
}
P
panqiangbiao 已提交
1197 1198
```

P
panqiangbiao 已提交
1199
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1200

P
panqiangbiao 已提交
1201
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1202 1203 1204

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

H
huweiqi 已提交
1205 1206 1207
> **说明:**
> 由于API version 10的SDK上audio没有orientation属性,在使用commitModify接口时将无法对audio资源的orientation属性进行修改。请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。

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

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

P
panqiangbiao 已提交
1212 1213
**返回值:**

H
HelloCrease 已提交
1214 1215
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1216
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
1217 1218 1219

**示例:**

1220
```js
P
panqiangbiao 已提交
1221
async function example() {
H
huweiqi 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.title = 'newtitle';
  await asset.commitModify();
  fetchFileResult.close();
P
panqiangbiao 已提交
1234
}
P
panqiangbiao 已提交
1235 1236
```

P
panqiangbiao 已提交
1237
### open<sup>8+</sup>
P
panqiangbiao 已提交
1238

P
panqiangbiao 已提交
1239
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1240 1241 1242

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

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

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

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

H
huweiqi 已提交
1249
**参数:**
P
panqiangbiao 已提交
1250

H
HelloCrease 已提交
1251 1252
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
H
huweiqi 已提交
1253 1254
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
| callback | AsyncCallback&lt;number&gt; | 是    | callback返回文件描述符。                            |
P
panqiangbiao 已提交
1255 1256 1257

**示例:**

1258
```js
P
panqiangbiao 已提交
1259
async function example() {
H
huweiqi 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
  let mediaType = mediaLibrary.MediaType.IMAGE;
  let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
  const path = await media.getPublicDirectory(DIR_IMAGE);
  const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
  asset.open('rw', (error, fd) => {
    if (fd > 0) {
      asset.close(fd);
    } else {
      console.error('File Open failed with error: ' + error);
    }
  });
P
panqiangbiao 已提交
1271
}
P
panqiangbiao 已提交
1272 1273
```

P
panqiangbiao 已提交
1274
### open<sup>8+</sup>
P
panqiangbiao 已提交
1275

P
panqiangbiao 已提交
1276
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1277 1278 1279

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

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

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

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

P
panqiangbiao 已提交
1286 1287
**参数:**

H
HelloCrease 已提交
1288 1289
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
H
huweiqi 已提交
1290
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
P
panqiangbiao 已提交
1291 1292 1293

**返回值:**

H
HelloCrease 已提交
1294 1295
| 类型                    | 说明            |
| --------------------- | ------------- |
H
huweiqi 已提交
1296
| Promise&lt;number&gt; | Promise对象,返回文件描述符。 |
P
panqiangbiao 已提交
1297 1298 1299

**示例:**

1300
```js
P
panqiangbiao 已提交
1301
async function example() {
H
huweiqi 已提交
1302 1303 1304 1305 1306 1307 1308 1309 1310
  let mediaType = mediaLibrary.MediaType.IMAGE;
  let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
  const path = await media.getPublicDirectory(DIR_IMAGE);
  const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
  asset.open('rw').then((fd) => {
    console.info('File open fd: ' + fd);
  }).catch((error) => {
    console.error('File open failed with error: ' + error);
  });
P
panqiangbiao 已提交
1311
}
P
panqiangbiao 已提交
1312 1313
```

P
panqiangbiao 已提交
1314
### close<sup>8+</sup>
P
panqiangbiao 已提交
1315

P
panqiangbiao 已提交
1316
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1317 1318 1319

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

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

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

P
panqiangbiao 已提交
1324 1325
**参数:**

H
HelloCrease 已提交
1326 1327
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1328 1329
| fd       | number                    | 是    | 文件描述符。 |
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。 |
P
panqiangbiao 已提交
1330 1331 1332

**示例:**

1333
```js
P
panqiangbiao 已提交
1334
async function example() {
H
huweiqi 已提交
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.open('rw').then((fd) => {
    console.info('File open fd: ' + fd);
    asset.close(fd, (error) => {
      if (error) {
        console.error('asset.close failed with error: ' + error);
      } else {
        console.info('asset.close successfully');
      }
P
panqiangbiao 已提交
1352
    });
H
huweiqi 已提交
1353 1354 1355 1356
  }).catch((error) => {
    console.error('File open failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1357
}
P
panqiangbiao 已提交
1358 1359
```

P
panqiangbiao 已提交
1360
### close<sup>8+</sup>
P
panqiangbiao 已提交
1361

P
panqiangbiao 已提交
1362
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1363 1364 1365

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

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

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

P
panqiangbiao 已提交
1370 1371
**参数:**

H
HelloCrease 已提交
1372 1373
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
H
huweiqi 已提交
1374
| fd   | number | 是    | 文件描述符。 |
P
panqiangbiao 已提交
1375 1376 1377

**返回值:**

H
HelloCrease 已提交
1378 1379
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1380
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
1381 1382 1383

**示例:**

1384
```js
P
panqiangbiao 已提交
1385
async function example() {
H
huweiqi 已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.open('rw').then((fd) => {
    console.info('File fd!' + fd);
    asset.close(fd).then(() => {
      console.info('asset.close successfully');
    }).catch((closeErr) => {
      console.error('asset.close fail, closeErr: ' + closeErr);
P
panqiangbiao 已提交
1401
    });
H
huweiqi 已提交
1402 1403 1404 1405
  }).catch((error) => {
    console.error('open File failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1406
}
P
panqiangbiao 已提交
1407 1408
```

P
panqiangbiao 已提交
1409
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1410

P
panqiangbiao 已提交
1411
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1412 1413 1414

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

P
panqiangbiao 已提交
1415
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1416

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

P
panqiangbiao 已提交
1419 1420
**参数:**

H
HelloCrease 已提交
1421 1422
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
H
huweiqi 已提交
1423
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | callback返回缩略图的PixelMap。 |
P
panqiangbiao 已提交
1424 1425 1426

**示例:**

1427
```js
P
panqiangbiao 已提交
1428
async function example() {
H
huweiqi 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.getThumbnail((error, pixelmap) => {
    if (error) {
      console.error('mediaLibrary getThumbnail failed with error: ' + error);
    } else {
      console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
    }
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1446
}
P
panqiangbiao 已提交
1447 1448
```

P
panqiangbiao 已提交
1449
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1450

P
panqiangbiao 已提交
1451
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1452 1453 1454

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

P
panqiangbiao 已提交
1455
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1456

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

P
panqiangbiao 已提交
1459 1460
**参数:**

H
HelloCrease 已提交
1461 1462
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
H
huweiqi 已提交
1463 1464
| size     | [Size](#size8)                      | 是    | 缩略图尺寸。            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | callback返回缩略图的PixelMap。 |
P
panqiangbiao 已提交
1465 1466 1467

**示例:**

1468
```js
P
panqiangbiao 已提交
1469
async function example() {
H
huweiqi 已提交
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let size = { width: 720, height: 720 };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.getThumbnail(size, (error, pixelmap) => {
    if (error) {
      console.error('mediaLibrary getThumbnail failed with error: ' + error);
    } else {
      console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
    }
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1488
}
P
panqiangbiao 已提交
1489 1490
```

P
panqiangbiao 已提交
1491
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1492

P
panqiangbiao 已提交
1493
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1494 1495 1496

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

P
panqiangbiao 已提交
1497
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1498

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

P
panqiangbiao 已提交
1501 1502
**参数:**

H
HelloCrease 已提交
1503 1504
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
H
huweiqi 已提交
1505
| size | [Size](#size8) | 否    | 缩略图尺寸。 |
P
panqiangbiao 已提交
1506 1507 1508

**返回值:**

H
HelloCrease 已提交
1509 1510
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
H
huweiqi 已提交
1511
| Promise&lt;image.PixelMap&gt; | Promise对象,返回缩略图的PixelMap。 |
P
panqiangbiao 已提交
1512 1513 1514

**示例:**

1515
```js
P
panqiangbiao 已提交
1516
async function example() {
H
huweiqi 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let size = { width: 720, height: 720 };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.getThumbnail(size).then((pixelmap) => {
    console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
  }).catch((error) => {
    console.error('mediaLibrary getThumbnail failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1533
}
P
panqiangbiao 已提交
1534 1535
```

P
panqiangbiao 已提交
1536
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1537

P
panqiangbiao 已提交
1538
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1539 1540 1541

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

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

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

P
panqiangbiao 已提交
1546 1547
**参数:**

H
HelloCrease 已提交
1548 1549
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
H
huweiqi 已提交
1550 1551
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
| callback   | AsyncCallback&lt;void&gt; | 是    | callback返回空。                              |
P
panqiangbiao 已提交
1552 1553 1554

**示例:**

1555
```js
P
panqiangbiao 已提交
1556
async function example() {
H
huweiqi 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.favorite(true,(error) => {
    if (error) {
      console.error('mediaLibrary favorite failed with error: ' + error);
    } else {
      console.info('mediaLibrary favorite Successful');
    }
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1574
}
P
panqiangbiao 已提交
1575 1576
```

P
panqiangbiao 已提交
1577
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1578

P
panqiangbiao 已提交
1579
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1580 1581 1582

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

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

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

P
panqiangbiao 已提交
1587 1588
**参数:**

H
HelloCrease 已提交
1589 1590
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
H
huweiqi 已提交
1591
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
P
panqiangbiao 已提交
1592 1593 1594

**返回值:**

H
HelloCrease 已提交
1595 1596
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1597
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
1598 1599 1600

**示例:**

1601
```js
P
panqiangbiao 已提交
1602
async function example() {
H
huweiqi 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.favorite(true).then(() => {
    console.info('mediaLibrary favorite Successful');
  }).catch((error) => {
    console.error('mediaLibrary favorite failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1618
}
P
panqiangbiao 已提交
1619 1620
```

P
panqiangbiao 已提交
1621
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1622

P
panqiangbiao 已提交
1623
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1624 1625 1626

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

P
panqiangbiao 已提交
1627
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1628

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

P
panqiangbiao 已提交
1631 1632
**参数:**

H
HelloCrease 已提交
1633 1634
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
H
huweiqi 已提交
1635
| callback | AsyncCallback&lt;boolean&gt; | 是    | callback返回boolean值,值为true则为已收藏,值为false则为未收藏。 |
P
panqiangbiao 已提交
1636 1637 1638

**示例:**

1639
```js
P
panqiangbiao 已提交
1640
async function example() {
H
huweiqi 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.isFavorite((error, isFavorite) => {
    if (error) {
      console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
    } else {
      console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
    }
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1658
}
P
panqiangbiao 已提交
1659 1660
```

P
panqiangbiao 已提交
1661
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1662

P
panqiangbiao 已提交
1663
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1664 1665 1666

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

P
panqiangbiao 已提交
1667
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1668

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

P
panqiangbiao 已提交
1671 1672
**返回值:**

H
HelloCrease 已提交
1673 1674
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
H
huweiqi 已提交
1675
| Promise&lt;boolean&gt; | Promise对象,返回boolean值,值为true则为已收藏,值为false则为未收藏。 |
P
panqiangbiao 已提交
1676 1677 1678

**示例:**

1679
```js
P
panqiangbiao 已提交
1680
async function example() {
H
huweiqi 已提交
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.isFavorite().then((isFavorite) => {
    console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
  }).catch((error) => {
    console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1696
}
P
panqiangbiao 已提交
1697 1698
```

P
panqiangbiao 已提交
1699
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1700

A
AOL 已提交
1701
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1702 1703 1704

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

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

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

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

P
panqiangbiao 已提交
1711 1712
**参数:**

H
HelloCrease 已提交
1713 1714
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
H
huweiqi 已提交
1715 1716
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件。 |
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。     |
P
panqiangbiao 已提交
1717 1718 1719

**示例:**

1720
```js
P
panqiangbiao 已提交
1721
async function example() {
H
huweiqi 已提交
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.trash(true, (error) => {
    if (error) {
      console.error('mediaLibrary trash failed with error: ' + error);
    } else {
      console.info('mediaLibrary trash Successful');
    }
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1739
}
P
panqiangbiao 已提交
1740 1741
```

P
panqiangbiao 已提交
1742
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1743

A
AOL 已提交
1744
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1745 1746 1747

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

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

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

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

P
panqiangbiao 已提交
1754 1755
**参数:**

H
HelloCrease 已提交
1756 1757
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
H
huweiqi 已提交
1758
| isTrash | boolean | 是    | 是否设置为垃圾文件。 |
P
panqiangbiao 已提交
1759 1760 1761

**返回值:**

H
HelloCrease 已提交
1762 1763
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1764
| Promise&lt;void&gt; | Promise对象,返回空 |
P
panqiangbiao 已提交
1765 1766 1767

**示例:**

1768
```js
P
panqiangbiao 已提交
1769
async function example() {
H
huweiqi 已提交
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.trash(true).then(() => {
    console.info('trash successfully');
  }).catch((error) => {
    console.error('trash failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1785
}
P
panqiangbiao 已提交
1786 1787
```

P
panqiangbiao 已提交
1788
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1789

P
panqiangbiao 已提交
1790
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1791 1792 1793

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

P
panqiangbiao 已提交
1794
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1795

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

P
panqiangbiao 已提交
1798 1799
**参数:**

H
HelloCrease 已提交
1800 1801
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
H
huweiqi 已提交
1802
| callback | AsyncCallback&lt;boolean&gt; | 是    | callback返回boolean值,值为true则为垃圾文件,值为false则为非垃圾文件。 |
P
panqiangbiao 已提交
1803 1804 1805

**示例:**

1806
```js
P
panqiangbiao 已提交
1807
async function example() {
H
huweiqi 已提交
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.isTrash((error, isTrash) => {
    if (error) {
      console.error('Failed to get trash state failed with error: ' + error);
      return;
    }
    console.info('Get trash state successfully, isTrash result: ' + isTrash);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1825
}
P
panqiangbiao 已提交
1826 1827
```

P
panqiangbiao 已提交
1828
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1829

P
panqiangbiao 已提交
1830
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1831

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

P
panqiangbiao 已提交
1834
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1835

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

P
panqiangbiao 已提交
1838 1839
**返回值:**

H
HelloCrease 已提交
1840 1841
| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
1842
| Promise&lt;void&gt; | Promise对象,返回boolean值,值为true则为垃圾文件,值为false则为非垃圾文件。 |
P
panqiangbiao 已提交
1843 1844 1845

**示例:**

1846
```js
P
panqiangbiao 已提交
1847
async function example() {
H
huweiqi 已提交
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  const fetchFileResult = await media.getFileAssets(getImageOp);
  const asset = await fetchFileResult.getFirstObject();
  asset.isTrash().then((isTrash) => {
    console.info('isTrash result: ' + isTrash);
  }).catch((error) => {
    console.error('isTrash failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1863
}
P
panqiangbiao 已提交
1864 1865
```

Z
zengyawen 已提交
1866
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1867 1868 1869

文件检索结果集。

Z
zengyawen 已提交
1870
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1871

P
panqiangbiao 已提交
1872
getCount(): number
P
panqiangbiao 已提交
1873 1874 1875

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

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

H
huweiqi 已提交
1878
**返回值:**
P
panqiangbiao 已提交
1879

H
HelloCrease 已提交
1880 1881
| 类型     | 说明       |
| ------ | -------- |
H
huweiqi 已提交
1882
| number | 检索到的文件总数。 |
P
panqiangbiao 已提交
1883

H
huweiqi 已提交
1884
**示例:**
P
panqiangbiao 已提交
1885

1886
```js
P
panqiangbiao 已提交
1887
async function example() {
H
huweiqi 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
  let fileKeyObj = mediaLibrary.FileKey;
  let fileType = mediaLibrary.MediaType.FILE;
  let getFileCountOneOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [fileType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
  const fetchCount = fetchFileResult.getCount();
  console.info('fetchCount result: ' + fetchCount);
  fetchFileResult.close();
P
panqiangbiao 已提交
1899
}
P
panqiangbiao 已提交
1900 1901
```

Z
zengyawen 已提交
1902
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1903

P
panqiangbiao 已提交
1904
isAfterLast(): boolean
P
panqiangbiao 已提交
1905 1906 1907

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

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

H
huweiqi 已提交
1910
**返回值:**
P
panqiangbiao 已提交
1911

H
HelloCrease 已提交
1912 1913
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1914
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1915

H
huweiqi 已提交
1916
**示例:**
P
panqiangbiao 已提交
1917

1918
```js
P
panqiangbiao 已提交
1919
async function example() {
H
huweiqi 已提交
1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  const fetchCount = fetchFileResult.getCount();
  console.info('mediaLibrary fetchFileResult.getCount, count:' + fetchCount);
  let fileAsset = await fetchFileResult.getFirstObject();
  for (var i = 1; i < fetchCount; i++) {
    fileAsset = await fetchFileResult.getNextObject();
    if(i == fetchCount - 1) {
      var result = fetchFileResult.isAfterLast();
      console.info('mediaLibrary fileAsset isAfterLast result: ' + result);
      fetchFileResult.close();
P
panqiangbiao 已提交
1937
    }
H
huweiqi 已提交
1938
  }
P
panqiangbiao 已提交
1939
}
P
panqiangbiao 已提交
1940 1941
```

Z
zengyawen 已提交
1942
### close<sup>7+</sup>
P
panqiangbiao 已提交
1943

P
panqiangbiao 已提交
1944
close(): void
P
panqiangbiao 已提交
1945 1946 1947

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

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

H
huweiqi 已提交
1950
**示例:**
P
panqiangbiao 已提交
1951

1952
```js
P
panqiangbiao 已提交
1953
async function example() {
H
huweiqi 已提交
1954 1955 1956 1957 1958 1959 1960 1961 1962
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.close();
P
panqiangbiao 已提交
1963
}
P
panqiangbiao 已提交
1964 1965
```

Z
zengyawen 已提交
1966
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1967

P
panqiangbiao 已提交
1968
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1969 1970 1971

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

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

H
huweiqi 已提交
1974
**参数:**
P
panqiangbiao 已提交
1975

Z
zengyawen 已提交
1976 1977
| 参数名   | 类型                                          | 必填 | 说明                                        |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
H
huweiqi 已提交
1978
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | callback返回文件检索结果集中第一个FileAsset对象。 |
P
panqiangbiao 已提交
1979

H
huweiqi 已提交
1980
**示例:**
P
panqiangbiao 已提交
1981

1982
```js
P
panqiangbiao 已提交
1983
async function example() {
H
huweiqi 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.getFirstObject((error, fileAsset) => {
    if (error) {
      console.error('fetchFileResult getFirstObject failed with error: ' + error);
      return;
    }
    console.info('getFirstObject successfully, displayName : ' + fileAsset.displayName);
    fetchFileResult.close();
  })
P
panqiangbiao 已提交
2000
}
P
panqiangbiao 已提交
2001 2002
```

Z
zengyawen 已提交
2003
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
2004

P
panqiangbiao 已提交
2005
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2006

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

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

H
huweiqi 已提交
2011
**返回值:**
P
panqiangbiao 已提交
2012

Z
zengyawen 已提交
2013 2014
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
H
huweiqi 已提交
2015
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise对象,返回文件检索结果集中第一个FileAsset。 |
P
panqiangbiao 已提交
2016

H
huweiqi 已提交
2017
**示例:**
P
panqiangbiao 已提交
2018

2019
```js
P
panqiangbiao 已提交
2020
async function example() {
H
huweiqi 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.getFirstObject().then((fileAsset) => {
    console.info('getFirstObject successfully, displayName: ' + fileAsset.displayName);
    fetchFileResult.close();
  }).catch((error) => {
    console.error('getFirstObject failed with error: ' + error);
  });
P
panqiangbiao 已提交
2035
}
P
panqiangbiao 已提交
2036 2037
```

Z
zengyawen 已提交
2038
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2039

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

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

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

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

H
huweiqi 已提交
2048
**参数:**
P
panqiangbiao 已提交
2049

Z
zengyawen 已提交
2050 2051
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
H
huweiqi 已提交
2052
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | callback返回文件检索结果集中下一个FileAsset对象。 |
P
panqiangbiao 已提交
2053

H
huweiqi 已提交
2054
**示例:**
P
panqiangbiao 已提交
2055

2056
```js
P
panqiangbiao 已提交
2057
async function example() {
H
huweiqi 已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  let fileAsset = await fetchFileResult.getFirstObject();
  console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
  if (!fetchFileResult.isAfterLast()) {
    fetchFileResult.getNextObject((error, fileAsset) => {
      if (error) {
        console.error('fetchFileResult getNextObject failed with error: ' + error);
        return;
      }
      console.log('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
      fetchFileResult.close();
    })
  }
P
panqiangbiao 已提交
2078
}
H
huweiqi 已提交
2079

P
panqiangbiao 已提交
2080 2081
```

Z
zengyawen 已提交
2082
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2083

H
huweiqi 已提交
2084
getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2085 2086 2087

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

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

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

H
huweiqi 已提交
2092
**返回值:**
P
panqiangbiao 已提交
2093

Z
zengyawen 已提交
2094 2095
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
2096
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise对象,返回文件检索结果集中下一个FileAsset。 |
P
panqiangbiao 已提交
2097

H
huweiqi 已提交
2098
**示例:**
P
panqiangbiao 已提交
2099

2100
```js
P
panqiangbiao 已提交
2101
async function example() {
H
huweiqi 已提交
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  let fileAsset = await fetchFileResult.getFirstObject();
  console.log('fetchFileResult getFirstObject successfully, displayName: ' + fileAsset.displayName);
  if (!fetchFileResult.isAfterLast()) {
    fetchFileResult.getNextObject().then((fileAsset) => {
      console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
      fetchFileResult.close();
    }).catch((error) => {
      console.error('fetchFileResult getNextObject failed with error: ' + error);
    })
  }
P
panqiangbiao 已提交
2120
}
P
panqiangbiao 已提交
2121 2122
```

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

P
panqiangbiao 已提交
2125
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2126 2127 2128

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

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

H
huweiqi 已提交
2131
**参数:**
P
panqiangbiao 已提交
2132

Z
zengyawen 已提交
2133 2134
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
H
huweiqi 已提交
2135
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | callback返回文件检索结果集中最后一个FileAsset对象。 |
P
panqiangbiao 已提交
2136

H
huweiqi 已提交
2137
**示例:**
P
panqiangbiao 已提交
2138

2139
```js
P
panqiangbiao 已提交
2140
async function example() {
H
huweiqi 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.getLastObject((error, fileAsset) => {
    if (error) {
      console.error('getLastObject failed with error: ' + error);
      return;
    }
    console.info('getLastObject successfully, displayName: ' + fileAsset.displayName);
    fetchFileResult.close();
  })
P
panqiangbiao 已提交
2157
}
P
panqiangbiao 已提交
2158 2159
```

Z
zengyawen 已提交
2160
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2161

P
panqiangbiao 已提交
2162
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2163 2164 2165

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

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

H
huweiqi 已提交
2168
**返回值:**
P
panqiangbiao 已提交
2169

Z
zengyawen 已提交
2170 2171
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
2172
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise对象,返回文件检索结果集中最后一个FileAsset。 |
P
panqiangbiao 已提交
2173

H
huweiqi 已提交
2174
**示例:**
P
panqiangbiao 已提交
2175

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

Z
zengyawen 已提交
2195
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2196

P
panqiangbiao 已提交
2197
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2198 2199 2200

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

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

H
huweiqi 已提交
2203
**参数:**
P
panqiangbiao 已提交
2204

Z
zengyawen 已提交
2205
| 参数名       | 类型                                       | 必填   | 说明                 |
H
HelloCrease 已提交
2206
| -------- | ---------------------------------------- | ---- | ------------------ |
H
huweiqi 已提交
2207 2208
| index    | number                                   | 是    | 要获取的文件的索引,从0开始(注意该值要小于文件检索集的count值)。     |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是    | callback返回文件检索结果集中指定索引处的FileAsset对象。 |
P
panqiangbiao 已提交
2209

H
huweiqi 已提交
2210
**示例:**
P
panqiangbiao 已提交
2211

2212
```js
P
panqiangbiao 已提交
2213
async function example() {
H
huweiqi 已提交
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.getPositionObject(0, (error, fileAsset) => {
    if (error) {
      console.error('getPositionObject failed with error: ' + error);
      return;
    }
    console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
    fetchFileResult.close();
  })
P
panqiangbiao 已提交
2230
}
P
panqiangbiao 已提交
2231 2232
```

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

P
panqiangbiao 已提交
2235
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2236 2237 2238

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

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

H
huweiqi 已提交
2241
**参数:**
P
panqiangbiao 已提交
2242

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

H
huweiqi 已提交
2247
**返回值:**
P
panqiangbiao 已提交
2248

Z
zengyawen 已提交
2249 2250
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
H
huweiqi 已提交
2251
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise对象,返回文件检索结果集中指定索引处的FileAsset。 |
P
panqiangbiao 已提交
2252

H
huweiqi 已提交
2253
**示例:**
P
panqiangbiao 已提交
2254

2255
```js
P
panqiangbiao 已提交
2256
async function example() {
H
huweiqi 已提交
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.getPositionObject(0).then((fileAsset) => {
    console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
    fetchFileResult.close();
  }).catch((error) => {
    console.error('getPositionObject failed with error: ' + error);
  });
P
panqiangbiao 已提交
2271
}
P
panqiangbiao 已提交
2272 2273
```

Z
zengyawen 已提交
2274
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2275

P
panqiangbiao 已提交
2276
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2277 2278 2279

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

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

H
huweiqi 已提交
2282
**参数:**
P
panqiangbiao 已提交
2283

Z
zengyawen 已提交
2284
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
2285
| -------- | ---------------------------------------- | ---- | -------------------- |
H
huweiqi 已提交
2286
| callback | AsyncCallback&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | 是    | callback返回文件检索结果集中所有的FileAsset对象。 |
P
panqiangbiao 已提交
2287

H
huweiqi 已提交
2288
**示例:**
P
panqiangbiao 已提交
2289

2290
```js
P
panqiangbiao 已提交
2291
async function example() {
H
huweiqi 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.getAllObject((error, fileAssetList) => {
    if (error) {
      console.error('getAllObject failed with error: ' + error);
      return;
    }
    for (let i = 0; i < fetchFileResult.getCount(); i++) {
      console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
    }
    fetchFileResult.close();
  })
P
panqiangbiao 已提交
2310
}
P
panqiangbiao 已提交
2311 2312
```

Z
zengyawen 已提交
2313
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2314

P
panqiangbiao 已提交
2315
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2316 2317 2318

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

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

H
huweiqi 已提交
2321
**返回值:**
P
panqiangbiao 已提交
2322

Z
zengyawen 已提交
2323 2324
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
H
huweiqi 已提交
2325
| Promise&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | Promise对象,返回文件检索结果集中所有的FileAsset。 |
P
panqiangbiao 已提交
2326

H
huweiqi 已提交
2327
**示例:**
P
panqiangbiao 已提交
2328

2329
```js
P
panqiangbiao 已提交
2330
async function example() {
H
huweiqi 已提交
2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
  let fileKeyObj = mediaLibrary.FileKey;
  let imageType = mediaLibrary.MediaType.IMAGE;
  let getImageOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
    order: fileKeyObj.DATE_ADDED + ' DESC',
  };
  let fetchFileResult = await media.getFileAssets(getImageOp);
  fetchFileResult.getAllObject().then((fileAssetList) => {
    for (let i = 0; i < fetchFileResult.getCount(); i++) {
      console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
    } 
    fetchFileResult.close();
  }).catch((error) => {
    console.error('getAllObject failed with error: ' + error);
  });
P
panqiangbiao 已提交
2347
}
P
panqiangbiao 已提交
2348 2349
```

Z
zengyawen 已提交
2350
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2351 2352 2353

实体相册

Z
zengyawen 已提交
2354
### 属性
P
panqiangbiao 已提交
2355

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

Z
zengyawen 已提交
2358
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2359
| ------------ | ------ | ---- | ---- | ------- |
H
huweiqi 已提交
2360 2361 2362 2363 2364 2365 2366
| albumId | number | 是    | 否    | 相册编号。    |
| albumName | string | 是    | 是    | 相册名称。    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri。   |
| dateModified | number | 是    | 否    | 修改日期。    |
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量。 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径。    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri。 |
P
panqiangbiao 已提交
2367

P
panqiangbiao 已提交
2368 2369 2370
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2371 2372 2373

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

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

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

H
huweiqi 已提交
2378
**参数:**
P
panqiangbiao 已提交
2379

Z
zengyawen 已提交
2380 2381
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
2382
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回空。 |
P
panqiangbiao 已提交
2383

H
huweiqi 已提交
2384
**示例:**
P
panqiangbiao 已提交
2385

2386
```js
P
panqiangbiao 已提交
2387
async function example() {
H
huweiqi 已提交
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
  album.albumName = 'hello';
  album.commitModify((error) => {
    if (error) {
      console.error('commitModify failed with error: ' + error);
      return;
    }
    console.info('commitModify successful.');
H
huweiqi 已提交
2402
  });
P
panqiangbiao 已提交
2403
}
P
panqiangbiao 已提交
2404 2405
```

P
panqiangbiao 已提交
2406
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2407

P
panqiangbiao 已提交
2408
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2409 2410 2411

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

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

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

H
huweiqi 已提交
2416
**返回值:**
P
panqiangbiao 已提交
2417

H
HelloCrease 已提交
2418 2419
| 类型                  | 说明           |
| ------------------- | ------------ |
H
huweiqi 已提交
2420
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
2421

H
huweiqi 已提交
2422
**示例:**
P
panqiangbiao 已提交
2423

2424
```js
P
panqiangbiao 已提交
2425
async function example() {
H
huweiqi 已提交
2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
  album.albumName = 'hello';
  album.commitModify().then(() => {
    console.info('commitModify successfully');
  }).catch((error) => {
    console.error('commitModify failed with error: ' + error);
  });
}
```

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

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

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

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

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

H
huweiqi 已提交
2452
**参数:**
H
huweiqi 已提交
2453 2454 2455

| 参数名   | 类型                                                | 必填 | 说明                                |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
H
huweiqi 已提交
2456
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | callback返回相册中的文件检索结果集。 |
H
huweiqi 已提交
2457

H
huweiqi 已提交
2458
**示例:**
H
huweiqi 已提交
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479

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

Z
zengyawen 已提交
2483
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2484

P
panqiangbiao 已提交
2485
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2486 2487 2488

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

P
panqiangbiao 已提交
2489
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2490

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

H
huweiqi 已提交
2493
**参数:**
P
panqiangbiao 已提交
2494

Z
zengyawen 已提交
2495
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2496
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2497
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
H
huweiqi 已提交
2498
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | callback返回相册中的文件检索结果集。 |
P
panqiangbiao 已提交
2499

H
huweiqi 已提交
2500
**示例:**
P
panqiangbiao 已提交
2501

2502
```js
P
panqiangbiao 已提交
2503
async function example() {
H
huweiqi 已提交
2504 2505 2506 2507 2508 2509 2510 2511
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
H
huweiqi 已提交
2512 2513
  };
  // 获取符合检索要求的相册,返回相册列表。
H
huweiqi 已提交
2514 2515
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
H
huweiqi 已提交
2516
  // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源。
H
huweiqi 已提交
2517 2518 2519 2520
  album.getFileAssets(fileNoArgsfetchOp, (error, fetchFileResult) => {
    if (error) {
      console.error('album getFileAssets failed with error: ' + error);
      return;
Z
zhang-daiyue 已提交
2521
    }
H
huweiqi 已提交
2522 2523 2524 2525
    let count = fetchFileResult.getCount();
    console.info('album getFileAssets successfully, count: ' + count);
    fetchFileResult.close();
  });
P
panqiangbiao 已提交
2526 2527 2528
}
```

Z
zengyawen 已提交
2529
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2530

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

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

P
panqiangbiao 已提交
2535
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2536

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

H
huweiqi 已提交
2539
**参数:**
P
panqiangbiao 已提交
2540

Z
zengyawen 已提交
2541
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2542
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2543
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2544

H
huweiqi 已提交
2545
**返回值:**
P
panqiangbiao 已提交
2546

Z
zengyawen 已提交
2547 2548
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
H
huweiqi 已提交
2549
| Promise<[FetchFileResult](#fetchfileresult7)> | Promise对象,返回相册中的文件检索结果集。 |
P
panqiangbiao 已提交
2550

H
huweiqi 已提交
2551
**示例:**
P
panqiangbiao 已提交
2552

2553
```js
P
panqiangbiao 已提交
2554
async function example() {
H
huweiqi 已提交
2555 2556 2557 2558 2559 2560 2561 2562 2563
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
  };
H
huweiqi 已提交
2564
  // 获取符合检索要求的相册,返回相册列表。
H
huweiqi 已提交
2565 2566
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
H
huweiqi 已提交
2567
  // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源。
H
huweiqi 已提交
2568 2569 2570 2571 2572 2573 2574
  album.getFileAssets(fileNoArgsfetchOp).then((fetchFileResult) => {
    let count = fetchFileResult.getCount();
    console.info('album getFileAssets successfully, count: ' + count);
    fetchFileResult.close();
  }).catch((error) => {
    console.error('album getFileAssets failed with error: ' + error);
  });
P
panqiangbiao 已提交
2575
}
P
panqiangbiao 已提交
2576 2577
```

P
panqiangbiao 已提交
2578
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2579

P
panqiangbiao 已提交
2580
注册设备的信息。
2581 2582

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

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

Z
zengyawen 已提交
2586 2587
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
H
huweiqi 已提交
2588 2589 2590 2591
| deviceName | string                     | 是   | 否   | 注册设备的名称。   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID。 |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型。         |
| isOnline   | boolean                    | 是   | 否   | 是否在线。         |
P
panqiangbiao 已提交
2592

Z
zengyawen 已提交
2593
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2594 2595 2596

枚举,媒体类型。

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

H
huweiqi 已提交
2599 2600
| 名称  |  值 |  说明 |
| ----- |  ---- | ---- |
H
huweiqi 已提交
2601 2602 2603 2604
| FILE  |  0 | 文件。 |
| IMAGE |  1 | 图片。 |
| VIDEO |  2 | 视频。|
| AUDIO |  3 | 音频。 |
P
panqiangbiao 已提交
2605

Z
zengyawen 已提交
2606
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2607 2608 2609

枚举,文件关键信息。

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

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

H
huweiqi 已提交
2615
| 名称          | 值              | 说明                                                       |
Z
zengyawen 已提交
2616
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
| ID            | 'file_id'             | 文件编号。                                                   |
| RELATIVE_PATH | 'relative_path'       | 相对公共目录路径。                                           |
| DISPLAY_NAME  | 'display_name'        | 显示名字。                                                   |
| PARENT        | 'parent'              | 父目录id。                                                   |
| MIME_TYPE     | 'mime_type'           | 文件扩展属性(如:image/*、video/*、file/*)。                                             |
| MEDIA_TYPE    | 'media_type'          | 媒体类型。                                                   |
| SIZE          | 'size'                | 文件大小(单位:字节)。                                     |
| DATE_ADDED    | 'date_added'          | 添加日期(添加文件时间到1970年1月1日的秒数值)。             |
| DATE_MODIFIED | 'date_modified'       | 修改日期(修改文件时间到1970年1月1日的秒数值,修改文件名不会改变此值,当文件内容发生修改时才会更新)。 |
| DATE_TAKEN    | 'date_taken'          | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)。             |
| TITLE         | 'title'               | 文件标题。                                                   |
| ARTIST        | 'artist'              | 作者。                                                       |
| AUDIOALBUM    | 'audio_album'         | 专辑。                                                       |
| DURATION      | 'duration'            | 持续时间(单位:毫秒)。                                       |
| WIDTH         | 'width'               | 图片宽度(单位:像素)。                                     |
| HEIGHT        | 'height'              | 图片高度(单位:像素)。                                     |
| ORIENTATION   | 'orientation'         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度)。 |
| ALBUM_ID      | 'bucket_id'           | 文件所归属的相册编号。                                       |
| ALBUM_NAME    | 'bucket_display_name' | 文件所归属相册名称。                                         |
P
panqiangbiao 已提交
2636

Z
zengyawen 已提交
2637
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2638 2639 2640

枚举,目录类型。

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

H
huweiqi 已提交
2643 2644
| 名称          | 值 |  说明               |
| ------------- | --- | ------------------ |
H
huweiqi 已提交
2645 2646 2647 2648 2649 2650
| DIR_CAMERA    |  0 | 表示Camera文件路径。 |
| DIR_VIDEO     |  1 |  表示视频路径。       |
| DIR_IMAGE     |  2 | 表示图片路径。       |
| DIR_AUDIO     |  3 | 表示音频路径。       |
| DIR_DOCUMENTS |  4 | 表示文档路径。       |
| DIR_DOWNLOAD  |  5 |  表示下载路径。       |
P
panqiangbiao 已提交
2651

Z
zengyawen 已提交
2652
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2653 2654

枚举,设备类型。
2655 2656

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

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

H
huweiqi 已提交
2660 2661
| 名称         |  值 | 说明       |
| ------------ | --- | ---------- |
H
huweiqi 已提交
2662 2663 2664 2665 2666 2667 2668
| TYPE_UNKNOWN |  0 | 未识别设备。 |
| TYPE_LAPTOP  |  1 | 笔记本电脑。 |
| TYPE_PHONE   |  2 | 手机。       |
| TYPE_TABLET  |  3 | 平板电脑。   |
| TYPE_WATCH   |  4 | 智能手表。   |
| TYPE_CAR     |  5 | 车载设备。   |
| TYPE_TV      |  6 | 电视设备。   |
P
panqiangbiao 已提交
2669

Z
zengyawen 已提交
2670
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2671 2672 2673

检索条件。

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

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

P
panqiangbiao 已提交
2685
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2686 2687

图片尺寸。
2688 2689

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

H
HelloCrease 已提交
2691 2692
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
H
huweiqi 已提交
2693 2694
| width  | number | 是    | 是    | 宽(单位:像素)。 |
| height | number | 是    | 是    | 高(单位:像素)。 |
P
panqiangbiao 已提交
2695

H
huweiqi 已提交
2696
## MediaAssetOption
P
panqiangbiao 已提交
2697 2698 2699 2700 2701

媒体资源选项。

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

H
huweiqi 已提交
2702 2703 2704 2705 2706
| 名称         | 类型   | 可读 | 可写 | 说明                                                         |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| 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 已提交
2707

H
huweiqi 已提交
2708
## MediaSelectOption
P
panqiangbiao 已提交
2709 2710 2711 2712 2713

媒体资源类型选项。

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

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