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

zyjhandsome's avatar
zyjhandsome 已提交
3
> **说明:**
H
huweiqi 已提交
4
>
H
huweiqi 已提交
5
> - 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
H
huweiqi 已提交
6
> - 本模块从API version 9开始废弃。对应的替代接口请参考具体接口说明。
P
panqiangbiao 已提交
7

Z
zengyawen 已提交
8
## 导入模块
H
huweiqi 已提交
9

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

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

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

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

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

H
huweiqi 已提交
22 23 24
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getPhotoAccessHelper](js-apis-photoAccessHelper.md#photoaccesshelpergetphotoaccesshelper)替代。

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

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

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

**返回值:**

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

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

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

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

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

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

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

getMediaLibrary(): MediaLibrary

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

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

H
huweiqi 已提交
64 65 66
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

Z
zengyawen 已提交
67 68 69 70 71 72
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**返回值:**

| 类型                          | 说明       |
| ----------------------------- | :--------- |
H
huweiqi 已提交
73
| [MediaLibrary](#medialibrary) | 媒体库实例。 |
Z
zengyawen 已提交
74 75 76 77

**示例:**

```js
78
let media = mediaLibrary.getMediaLibrary();
Z
zengyawen 已提交
79 80
```

P
panqiangbiao 已提交
81
## MediaLibrary
P
panqiangbiao 已提交
82

Z
zengyawen 已提交
83
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
84

P
panqiangbiao 已提交
85
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 
P
panqiangbiao 已提交
86 87 88

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

H
huweiqi 已提交
89
> **说明:**
H
huweiqi 已提交
90 91 92
>
> - 此接口从API Version 9开始废弃。请使用[getAssets](js-apis-photoAccessHelper.md#getassets)替代。
> - 在API version 10上,摒弃了物理目录作为相册的设计,采用了逻辑相册的设计,一个相册中可以添加多个文件,一个文件也可以在多个相册中呈现。新的设计将带来parent、albumId、albumUri和albumName属性使用上的不兼容,无法作为MediaFetchOptions的参数在getFileAssets接口中使用。请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。
H
huweiqi 已提交
93

P
panqiangbiao 已提交
94
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
95

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

P
panqiangbiao 已提交
98 99
**参数:**

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

**示例:**

107
```js
H
huweiqi 已提交
108
async function example() {
H
huweiqi 已提交
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 145 146 147 148 149 150
  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 已提交
151
    });
H
huweiqi 已提交
152
  });
H
huweiqi 已提交
153
}
P
panqiangbiao 已提交
154
```
H
huweiqi 已提交
155

Z
zengyawen 已提交
156
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
157

P
panqiangbiao 已提交
158
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
159 160 161

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

H
huweiqi 已提交
162
> **说明:**
H
huweiqi 已提交
163 164 165
>
> - 此接口从API Version 9开始废弃。请使用[getAssets](js-apis-photoAccessHelper.md#getassets-1)替代。
> - 在API version 10上,摒弃了物理目录作为相册的设计,采用了逻辑相册的设计,一个相册中可以添加多个文件,一个文件也可以在多个相册中呈现。新的设计将带来parent、albumId、albumUri和albumName属性使用上的不兼容,无法作为MediaFetchOptions的参数在getFileAssets接口中使用。请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。
H
huweiqi 已提交
166

P
panqiangbiao 已提交
167
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
168

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

P
panqiangbiao 已提交
171 172
**参数:**

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

H
huweiqi 已提交
177
**返回值:**
P
panqiangbiao 已提交
178

Z
zengyawen 已提交
179 180
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
H
huweiqi 已提交
181
| Promise&lt;[FetchFileResult](#fetchfileresult7)&gt; | Promise对象,返回文件检索结果集。 |
P
panqiangbiao 已提交
182 183 184

**示例:**

185
```js
H
huweiqi 已提交
186
async function example() {
H
huweiqi 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  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 已提交
219
    }).catch((error) => {
H
huweiqi 已提交
220 221
      // 调用getFirstObject接口失败。
      console.error('get first object failed with error: ' + error);
Z
zhang-daiyue 已提交
222
    });
H
huweiqi 已提交
223 224 225 226
  }).catch((error) => {
    // 调用getFileAssets接口失败。
    console.error('get file assets failed with error: ' + error);
  });
H
huweiqi 已提交
227
}
P
panqiangbiao 已提交
228 229
```

P
panqiangbiao 已提交
230
### on<sup>8+</sup>
P
panqiangbiao 已提交
231

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

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

H
huweiqi 已提交
236 237 238
> **说明:**
> 此接口从API Version 9开始废弃。请使用[registerChange](js-apis-photoAccessHelper.md#registerchange)替代。

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

P
panqiangbiao 已提交
241 242
**参数:**

H
HelloCrease 已提交
243 244
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
245 246
| 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 已提交
247 248 249

**示例:**

250
```js
Z
zhang-daiyue 已提交
251
media.on('imageChange', () => {
H
huweiqi 已提交
252 253
  // image file had changed, do something.
});
P
panqiangbiao 已提交
254
```
H
huweiqi 已提交
255

P
panqiangbiao 已提交
256
### off<sup>8+</sup>
P
panqiangbiao 已提交
257

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

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

H
huweiqi 已提交
262 263 264
> **说明:**
> 此接口从API Version 9开始废弃。请使用[unRegisterChange](js-apis-photoAccessHelper.md#unregisterchange)替代。

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

P
panqiangbiao 已提交
267 268
**参数:**

H
HelloCrease 已提交
269 270
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
271 272
| 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 已提交
273 274 275

**示例:**

276
```js
潘强标 已提交
277
media.off('imageChange', () => {
H
huweiqi 已提交
278 279
  // stop listening successfully.
});
P
panqiangbiao 已提交
280 281
```

B
bmeangel 已提交
282
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
283

P
panqiangbiao 已提交
284
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
285 286 287

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

H
huweiqi 已提交
288
> **说明:**
H
huweiqi 已提交
289 290 291
>
> - 此接口从API Version 9开始废弃。请使用[createAsset](js-apis-photoAccessHelper.md#createasset)替代。
> - 由于API version 10的SDK上relativePath和相册没有关联关系,文件创建成功后,relativePath的最后一级目录不会作为相册呈现。变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。
H
huweiqi 已提交
292

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

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

P
panqiangbiao 已提交
297 298
**参数:**

Z
zengyawen 已提交
299 300
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
H
huweiqi 已提交
301 302 303 304
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型。                                                     |
| displayName  | string                                  | 是   | 展示文件名。                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径。 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | callback返回创建的媒体资源FileAsset对象。                          |
P
panqiangbiao 已提交
305 306 307

**示例:**

308
```js
P
panqiangbiao 已提交
309
async function example() {
H
huweiqi 已提交
310 311 312 313 314 315 316 317 318 319 320
  // 使用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 已提交
321
}
P
panqiangbiao 已提交
322 323
```

P
panqiangbiao 已提交
324
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
325

P
panqiangbiao 已提交
326
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
327 328 329

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

H
huweiqi 已提交
330
> **说明:**
H
huweiqi 已提交
331 332 333
>
> - 此接口从API Version 9开始废弃。请使用[createAsset](js-apis-photoAccessHelper.md#createasset-1)替代。
> - 由于API version 10的SDK上relativePath和相册没有关联关系,文件创建成功后,relativePath的最后一级目录不会作为相册呈现。变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。
H
huweiqi 已提交
334

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

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

P
panqiangbiao 已提交
339 340
**参数:**

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

H
huweiqi 已提交
347
**返回值:**
P
panqiangbiao 已提交
348

Z
zengyawen 已提交
349 350
| 类型                     | 说明              |
| ------------------------ | ----------------- |
H
huweiqi 已提交
351
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise对象,返回创建媒体数据的FileAsset。 |
P
panqiangbiao 已提交
352 353 354

**示例:**

355
```js
H
huweiqi 已提交
356
async function example() {
H
huweiqi 已提交
357 358 359 360 361 362 363 364 365
  // 使用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 已提交
366
}
P
panqiangbiao 已提交
367 368
```

Z
zengyawen 已提交
369 370 371 372
### deleteAsset<sup>8+</sup>

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

H
huweiqi 已提交
373
删除媒体文件资源。
Z
zengyawen 已提交
374

H
huweiqi 已提交
375 376 377
> **说明:**
> 此接口从API Version 9开始废弃。请使用[deleteAssets](js-apis-photoAccessHelper.md#deleteassets-3)替代。

Z
zengyawen 已提交
378 379
**系统接口**:此接口为系统接口。

380
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
381 382 383 384 385 386 387

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

**参数:**

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

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

Z
zengyawen 已提交
392 393
| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
394
| Promise&lt;void&gt; | Promise对象,返回删除的结果。 |
Z
zengyawen 已提交
395 396 397 398 399

**示例:**

```js
async function example() {
H
huweiqi 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
  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 已提交
418 419 420 421
}
```

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

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

H
huweiqi 已提交
425
删除媒体文件资源。
Z
zengyawen 已提交
426

H
huweiqi 已提交
427 428 429
> **说明:**
> 此接口从API Version 9开始废弃。请使用[deleteAssets](js-apis-photoAccessHelper.md#deleteassets-2)替代。

Z
zengyawen 已提交
430 431
**系统接口**:此接口为系统接口。

432
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
433 434 435 436 437 438 439 440

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

**参数:**

| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | 是    | 需要删除的媒体文件资源的uri。 |
H
huweiqi 已提交
441
|callback |AsyncCallback\<void>| 是  |callback返回空。|
Z
zengyawen 已提交
442 443 444 445 446

**示例:**

```js
async function example() {
H
huweiqi 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  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 已提交
464
    }
H
huweiqi 已提交
465 466
  });
  fetchFileResult.close();
Z
zengyawen 已提交
467 468 469
}
```

P
panqiangbiao 已提交
470
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
471

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

P
panqiangbiao 已提交
474
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
475

H
huweiqi 已提交
476 477 478
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

P
panqiangbiao 已提交
479 480 481 482
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**参数:**

Z
zengyawen 已提交
483 484
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
H
huweiqi 已提交
485 486
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型。              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback返回公共目录路径。 |
P
panqiangbiao 已提交
487 488 489

**示例:**

490
```js
P
panqiangbiao 已提交
491
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
H
huweiqi 已提交
492
media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => {
H
huweiqi 已提交
493 494 495 496 497
  if (dicResult == 'Camera/') {
    console.info('getPublicDirectory DIR_CAMERA successfully');
  } else {
    console.error('getPublicDirectory DIR_CAMERA failed with error: ' + error);
  }
P
panqiangbiao 已提交
498 499 500
});
```

P
panqiangbiao 已提交
501
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
502

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

P
panqiangbiao 已提交
505
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
506

H
huweiqi 已提交
507 508 509
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

**参数:**

Z
zengyawen 已提交
514 515
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
H
huweiqi 已提交
516
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型。 |
P
panqiangbiao 已提交
517 518 519

**返回值:**

Z
zengyawen 已提交
520 521
| 类型             | 说明             |
| ---------------- | ---------------- |
H
huweiqi 已提交
522
| Promise\<string> | Promise对象,返回公共目录路径。 |
P
panqiangbiao 已提交
523 524 525

**示例:**

526
```js
P
panqiangbiao 已提交
527
async function example() {
H
huweiqi 已提交
528 529 530 531 532 533 534 535 536 537
  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 已提交
538 539 540
}
```

Z
zengyawen 已提交
541
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
542

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

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

H
huweiqi 已提交
547
> **说明:**
H
huweiqi 已提交
548 549 550
>
> - 此接口从API Version 9开始废弃。请使用[getAlbums](js-apis-photoAccessHelper.md#getalbums)替代。
> - 由于API version 10的SDK上relativePath和相册没有关联关系,在使用getAlbums时不支持relativePath作为查询条件,当前仅支持“Camera”和“ScreenShots”两类相册,变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。
H
huweiqi 已提交
551

P
panqiangbiao 已提交
552
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
553

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

H
huweiqi 已提交
556
**参数:**
P
panqiangbiao 已提交
557

Z
zengyawen 已提交
558 559
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
H
huweiqi 已提交
560 561
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册检索条件。                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | callback返回获取的Album结果集。 |
P
panqiangbiao 已提交
562 563 564

**示例:**

565
```js
H
huweiqi 已提交
566
async function example() {
H
huweiqi 已提交
567 568
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
H
huweiqi 已提交
569
    selectionArgs: ['Camera'],
H
huweiqi 已提交
570 571 572 573 574 575 576
  };
  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 已提交
577
  });
H
huweiqi 已提交
578
}
P
panqiangbiao 已提交
579 580
```

Z
zengyawen 已提交
581
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
582

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

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

H
huweiqi 已提交
587
> **说明:**
H
huweiqi 已提交
588 589 590
>
> - 此接口从API Version 9开始废弃。请使用[getAlbums](js-apis-photoAccessHelper.md#getalbums-2)替代。
> - 由于API version 10的SDK上relativePath和相册没有关联关系,在使用getAlbums时不支持relativePath作为查询条件.当前仅支持“Camera”和“ScreenShots”两类相册,变更详情请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。
H
huweiqi 已提交
591

P
panqiangbiao 已提交
592
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
593

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

P
panqiangbiao 已提交
596 597
**参数:**

Z
zengyawen 已提交
598 599
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
H
huweiqi 已提交
600
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件。 |
P
panqiangbiao 已提交
601 602 603

**返回值:**

Z
zengyawen 已提交
604 605
| 类型                             | 说明          |
| -------------------------------- | ------------- |
H
huweiqi 已提交
606
| Promise<Array<[Album](#album7)>> | Promise对象,返回获取的Album结果集。 |
P
panqiangbiao 已提交
607 608 609

**示例:**

610
```js
H
huweiqi 已提交
611
async function example() {
H
huweiqi 已提交
612 613
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
H
huweiqi 已提交
614
    selectionArgs: ['Camera'],
H
huweiqi 已提交
615 616 617 618 619 620
  };
  media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
    console.info('getAlbums successfully: ' + JSON.stringify(albumList));
  }).catch((error) => {
    console.error('getAlbums failed with error: ' + error);
  });
H
huweiqi 已提交
621
}
P
panqiangbiao 已提交
622 623
```

P
panqiangbiao 已提交
624
### release<sup>8+</sup>
P
panqiangbiao 已提交
625

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

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

H
huweiqi 已提交
631 632 633
> **说明:**
> 此接口从API Version 9开始废弃。请使用[release](js-apis-photoAccessHelper.md#release)替代。

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

P
panqiangbiao 已提交
636 637
**参数:**

H
HelloCrease 已提交
638 639
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
640
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。 |
P
panqiangbiao 已提交
641 642 643

**示例:**

644
```js
H
huweiqi 已提交
645
media.release(() => {
H
huweiqi 已提交
646
  // do something.
P
panqiangbiao 已提交
647
});
P
panqiangbiao 已提交
648 649
```

P
panqiangbiao 已提交
650
### release<sup>8+</sup>
P
panqiangbiao 已提交
651

P
panqiangbiao 已提交
652
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
653

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

H
huweiqi 已提交
657 658 659
> **说明:**
> 此接口从API Version 9开始废弃。请使用[release](js-apis-photoAccessHelper.md#release-1)替代。

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

P
panqiangbiao 已提交
662 663
**返回值:**

H
HelloCrease 已提交
664 665
| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
666
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
667 668 669

**示例:**

670
```js
H
huweiqi 已提交
671
media.release();
P
panqiangbiao 已提交
672 673
```

H
huweiqi 已提交
674
### storeMediaAsset
P
panqiangbiao 已提交
675 676 677 678 679

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

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

H
huweiqi 已提交
680 681 682 683
> **说明:**
>
> - 此接口为API Version 6开始支持,只支持FA模型使用。
> - 此接口从API Version 9开始废弃。请使用[save](js-apis-file-picker.md#save-1)接口替代。
P
panqiangbiao 已提交
684 685 686 687 688

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

**参数:**

H
HelloCrease 已提交
689 690 691
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
H
huweiqi 已提交
692
| callback | AsyncCallback&lt;string&gt;           | 是    | callback返回保存媒体资源成功后得到的URI。 |
P
panqiangbiao 已提交
693 694 695

**示例:**

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

H
huweiqi 已提交
712
### storeMediaAsset
P
panqiangbiao 已提交
713 714 715 716 717

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

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

H
huweiqi 已提交
718 719 720 721
> **说明:**
>
> - 此接口为API Version 6开始支持,只支持FA模型使用。
> - 此接口从API Version 9开始废弃。请使用[save](js-apis-file-picker.md#save)接口替代。
P
panqiangbiao 已提交
722 723 724 725 726

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

**参数:**

H
HelloCrease 已提交
727 728 729
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
730 731 732

**返回值:**

H
HelloCrease 已提交
733 734
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
H
huweiqi 已提交
735
| Promise&lt;string&gt; | Promise对象,返回保存媒体资源成功后得到的URI。 |
P
panqiangbiao 已提交
736 737 738

**示例:**

739
```js
P
panqiangbiao 已提交
740
let option = {
H
huweiqi 已提交
741 742 743
  src : '/data/storage/el2/base/haps/entry/image.png',
  mimeType : 'image/*',
  relativePath : 'Pictures/'
P
panqiangbiao 已提交
744 745
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
H
huweiqi 已提交
746 747
  console.info('Media resources stored.');
  // Obtain the URI that stores media resources.
H
huweiqi 已提交
748
}).catch((error) => {
H
huweiqi 已提交
749
  console.error('storeMediaAsset failed with error: ' + error);
P
panqiangbiao 已提交
750
});
751
```
P
panqiangbiao 已提交
752

H
huweiqi 已提交
753
### startImagePreview
P
panqiangbiao 已提交
754 755 756

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

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

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

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

**参数:**

H
HelloCrease 已提交
768 769
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
770
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI('https://','datashare://')列表。 |
H
HelloCrease 已提交
771
| index    | number                    | 是    | 开始显示的图片序号。                               |
H
huweiqi 已提交
772
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。                        |
P
panqiangbiao 已提交
773 774 775

**示例:**

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

H
huweiqi 已提交
797
### startImagePreview
P
panqiangbiao 已提交
798 799 800

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

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

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

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

**参数:**

H
HelloCrease 已提交
812 813
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
814
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI('https://','datashare://')列表。 |
H
huweiqi 已提交
815
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。                        |
P
panqiangbiao 已提交
816 817 818

**示例:**

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

H
huweiqi 已提交
839
### startImagePreview
P
panqiangbiao 已提交
840 841 842

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

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

H
huweiqi 已提交
845 846 847 848
> **说明:**
>
> - 此接口为API Version 6开始支持,只支持FA模型使用。
> - 此接口从API Version 9开始废弃。建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
849 850 851 852 853

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

**参数:**

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

**返回值:**

H
HelloCrease 已提交
861 862
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
H
huweiqi 已提交
863
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
864 865 866

**示例:**

867
```js
P
panqiangbiao 已提交
868
let images = [
H
huweiqi 已提交
869 870
  'file://media/xxxx/2',
  'file://media/xxxx/3'
P
panqiangbiao 已提交
871
];
H
huweiqi 已提交
872
/* 网络图片使用方式。
P
panqiangbiao 已提交
873
let images = [
H
huweiqi 已提交
874 875
  'https://media.xxxx.com/image1.jpg',
  'https://media.xxxx.com/image2.jpg'
P
panqiangbiao 已提交
876
];
H
HelloCrease 已提交
877
*/
P
panqiangbiao 已提交
878 879
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
H
huweiqi 已提交
880
  console.info('Succeeded in previewing the images.');
H
huweiqi 已提交
881
}).catch((error) => {
H
huweiqi 已提交
882
  console.error('startImagePreview failed with error: ' + error);
P
panqiangbiao 已提交
883
});
884
```
P
panqiangbiao 已提交
885

H
huweiqi 已提交
886
### startMediaSelect
P
panqiangbiao 已提交
887 888 889 890 891

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

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

H
huweiqi 已提交
892 893 894 895
> **说明:**
>
> - 此接口为API Version 6开始支持,只支持FA模型使用。
> - 此接口从API Version 9开始废弃。请使用[select](js-apis-file-picker.md#select-1)接口替代。
P
panqiangbiao 已提交
896 897 898 899 900

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

**参数:**

H
HelloCrease 已提交
901 902
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
H
huweiqi 已提交
903
| option   | [MediaSelectOption](#mediaselectoption)  | 是    | 媒体选择选项。                              |
H
huweiqi 已提交
904
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | callback返回选择的媒体URI列表。 |
P
panqiangbiao 已提交
905 906 907

**示例:**

908
```js
Z
zhang-daiyue 已提交
909
let option : mediaLibrary.MediaSelectOption = {
H
huweiqi 已提交
910 911
  type : 'media',
  count : 2
P
panqiangbiao 已提交
912
};
H
huweiqi 已提交
913
mediaLibrary.getMediaLibrary().startMediaSelect(option, (error, value) => {
H
huweiqi 已提交
914 915 916 917 918 919
  if (error) {
    console.error('startMediaSelect failed with error: ' + error);
    return;
  }
  console.info('Media resources selected.');
  // Obtain the media selection value.
P
panqiangbiao 已提交
920
});
921
```
P
panqiangbiao 已提交
922

H
huweiqi 已提交
923
### startMediaSelect
P
panqiangbiao 已提交
924 925 926 927 928

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

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

H
huweiqi 已提交
929 930 931 932
> **说明:**
>
> - 此接口为API Version 6开始支持,只支持FA模型使用。
> - 此接口从API Version 9开始废弃。请使用[select](js-apis-file-picker.md#select)接口替代。
P
panqiangbiao 已提交
933 934 935 936 937

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

**参数:**

H
HelloCrease 已提交
938 939
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
H
huweiqi 已提交
940
| option | [MediaSelectOption](#mediaselectoption) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
941 942 943

**返回值:**

H
HelloCrease 已提交
944 945
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
H
huweiqi 已提交
946
| Promise&lt;Array&lt;string&gt;&gt; | Promise对象,返回选择的媒体URI列表。 |
P
panqiangbiao 已提交
947 948 949

**示例:**

950
```js
Z
zhang-daiyue 已提交
951
let option : mediaLibrary.MediaSelectOption = {
H
huweiqi 已提交
952 953
  type : 'media',
  count : 2
P
panqiangbiao 已提交
954
};
Z
zhang-daiyue 已提交
955
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
H
huweiqi 已提交
956 957
  console.info('Media resources selected.');
  // Obtain the media selection value.
H
huweiqi 已提交
958
}).catch((error) => {
H
huweiqi 已提交
959
  console.error('startMediaSelect failed with error: ' + error);
P
panqiangbiao 已提交
960
});
Z
zengyawen 已提交
961
```
H
huweiqi 已提交
962

Z
zengyawen 已提交
963 964 965 966
### getActivePeers<sup>8+</sup>

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

H
huweiqi 已提交
967
获取在线对端设备的信息,使用Promise方式返回异步结果。
Z
zengyawen 已提交
968

H
huweiqi 已提交
969 970 971
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

Z
zengyawen 已提交
972 973 974 975 976 977 978 979 980 981
**系统接口**:此接口为系统接口。

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

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

**返回值:**

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

**示例:**

```js
async function example() {
H
huweiqi 已提交
988 989 990 991 992 993 994 995 996
  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 已提交
997 998 999 1000
}
```

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

Z
zengyawen 已提交
1002 1003 1004 1005
getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

H
huweiqi 已提交
1006 1007 1008
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

Z
zengyawen 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
**系统接口**:此接口为系统接口。

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
1019
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | callback返回获取的所有在线对端设备的PeerInfo对象。 |
Z
zengyawen 已提交
1020 1021 1022 1023 1024

**示例:**

```js
async function example() {
H
huweiqi 已提交
1025 1026 1027 1028 1029 1030 1031
  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 已提交
1032 1033 1034 1035 1036 1037 1038
}
```

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

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

H
huweiqi 已提交
1039
获取所有对端设备的信息,使用Promise方式返回异步结果。
Z
zengyawen 已提交
1040

H
huweiqi 已提交
1041 1042 1043
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

Z
zengyawen 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
**系统接口**:此接口为系统接口。

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
1054
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | Promise对象,返回获取的所有对端设备的PeerInfo。 |
Z
zengyawen 已提交
1055 1056 1057 1058 1059

**示例:**

```js
async function example() {
H
huweiqi 已提交
1060 1061 1062 1063 1064 1065 1066 1067 1068
  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 已提交
1069 1070 1071 1072
}
```

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

Z
zengyawen 已提交
1074 1075 1076 1077
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

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

H
huweiqi 已提交
1078 1079 1080
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

Z
zengyawen 已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
**系统接口**:此接口为系统接口。

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

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
1091
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | callback返回获取的所有对端设备的PeerInfo对象。 |
Z
zengyawen 已提交
1092 1093 1094 1095 1096

**示例:**

```js
async function example() {
H
huweiqi 已提交
1097 1098 1099 1100 1101 1102 1103
  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 已提交
1104
}
1105
```
P
panqiangbiao 已提交
1106

Z
zengyawen 已提交
1107
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
1108 1109 1110

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

zyjhandsome's avatar
zyjhandsome 已提交
1111
> **说明:**
H
huweiqi 已提交
1112
>
H
huweiqi 已提交
1113 1114 1115
> - title字段默认为去掉后缀的文件名,音频和视频文件会尝试解析文件内容,部分设备写入后在触发扫描时会被还原。
> - orientation字段部分设备可能不支持修改,建议使用image组件的[ModifyImageProperty](js-apis-image.md#modifyimageproperty9)接口。
> - 此接口从API Version 9开始废弃。请使用[PhotoAsset](js-apis-photoAccessHelper.md#photoasset)替代。
H
huweiqi 已提交
1116

Z
zengyawen 已提交
1117 1118 1119
### 属性

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

Z
zengyawen 已提交
1121 1122
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
H
huweiqi 已提交
1123 1124 1125 1126 1127 1128 1129
| 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 已提交
1130
| parent<sup>8+</sup>       | number                   | 是   | 否   | 父目录id。由于API version 10的SDK上Asset可以存在多个相册,该属性不兼容。获取值始终为0。                        |
H
huweiqi 已提交
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
| 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 已提交
1141 1142 1143
| 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 已提交
1144 1145

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

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

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

H
huweiqi 已提交
1151 1152 1153
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

P
panqiangbiao 已提交
1154
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1155

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

P
panqiangbiao 已提交
1158 1159
**参数:**

H
HelloCrease 已提交
1160 1161
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
H
huweiqi 已提交
1162
| callback | AsyncCallback&lt;boolean&gt; | 是    | callback返回boolean值,值为true则是目录,值为false则非目录。 |
P
panqiangbiao 已提交
1163 1164 1165

**示例:**

1166
```js
P
panqiangbiao 已提交
1167
async function example() {
H
huweiqi 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
  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 已提交
1185
}
P
panqiangbiao 已提交
1186 1187
```

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

P
panqiangbiao 已提交
1190
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1191 1192 1193

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

H
huweiqi 已提交
1194 1195 1196
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

P
panqiangbiao 已提交
1197
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1198

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

P
panqiangbiao 已提交
1201 1202
**返回值:**

H
HelloCrease 已提交
1203 1204
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
H
huweiqi 已提交
1205
| Promise&lt;boolean&gt; | Promise对象,返回boolean值,值为true则是目录,值为false则非目录。 |
P
panqiangbiao 已提交
1206 1207 1208

**示例:**

1209
```js
P
panqiangbiao 已提交
1210
async function example() {
H
huweiqi 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
  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 已提交
1226
}
P
panqiangbiao 已提交
1227 1228
```

P
panqiangbiao 已提交
1229
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1230

P
panqiangbiao 已提交
1231
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1232 1233 1234

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

H
huweiqi 已提交
1235
> **说明:**
H
huweiqi 已提交
1236 1237 1238
>
> - 此接口从API Version 9开始废弃。请使用[commitModify](js-apis-photoAccessHelper.md#commitmodify)替代。
> - 由于API version 10的SDK上audio没有orientation属性,在使用commitModify接口时将无法对audio资源的orientation属性进行修改。请参考[changelogs-mediaLibrary.md](../../../release-notes/changelogs/OpenHarmony_4.0.8.2/changelogs-mediaLibrary.md)。
H
huweiqi 已提交
1239

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

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

P
panqiangbiao 已提交
1244 1245
**参数:**

H
HelloCrease 已提交
1246 1247
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1248
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。 |
P
panqiangbiao 已提交
1249 1250 1251

**示例:**

1252
```js
P
panqiangbiao 已提交
1253
async function example() {
H
huweiqi 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
  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 已提交
1268
}
P
panqiangbiao 已提交
1269 1270
```

P
panqiangbiao 已提交
1271
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1272

P
panqiangbiao 已提交
1273
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1274 1275 1276

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

H
huweiqi 已提交
1277
> **说明:**
H
huweiqi 已提交
1278 1279
>
> - 此接口从API Version 9开始废弃。请使用[commitModify](js-apis-photoAccessHelper.md#commitmodify-1)替代。
H
huweiqi 已提交
1280 1281
> 由于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 已提交
1282
**需要权限**:ohos.permission.READ_MEDIA, 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
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
1291 1292 1293

**示例:**

1294
```js
P
panqiangbiao 已提交
1295
async function example() {
H
huweiqi 已提交
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
  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 已提交
1308
}
P
panqiangbiao 已提交
1309 1310
```

P
panqiangbiao 已提交
1311
### open<sup>8+</sup>
P
panqiangbiao 已提交
1312

P
panqiangbiao 已提交
1313
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1314 1315 1316

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

H
huweiqi 已提交
1317 1318 1319
> **说明:**
> 此接口从API Version 9开始废弃。请使用[open](js-apis-photoAccessHelper.md#open)替代。

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

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

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

H
huweiqi 已提交
1326
**参数:**
P
panqiangbiao 已提交
1327

H
HelloCrease 已提交
1328 1329
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
H
huweiqi 已提交
1330 1331
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
| callback | AsyncCallback&lt;number&gt; | 是    | callback返回文件描述符。                            |
P
panqiangbiao 已提交
1332 1333 1334

**示例:**

1335
```js
P
panqiangbiao 已提交
1336
async function example() {
H
huweiqi 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
  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 已提交
1348
}
P
panqiangbiao 已提交
1349 1350
```

P
panqiangbiao 已提交
1351
### open<sup>8+</sup>
P
panqiangbiao 已提交
1352

P
panqiangbiao 已提交
1353
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1354 1355 1356

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

H
huweiqi 已提交
1357 1358 1359
> **说明:**
> 此接口从API Version 9开始废弃。请使用[open](js-apis-photoAccessHelper.md#open-1)替代。

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

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

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

P
panqiangbiao 已提交
1366 1367
**参数:**

H
HelloCrease 已提交
1368 1369
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
H
huweiqi 已提交
1370
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写)。 |
P
panqiangbiao 已提交
1371 1372 1373

**返回值:**

H
HelloCrease 已提交
1374 1375
| 类型                    | 说明            |
| --------------------- | ------------- |
H
huweiqi 已提交
1376
| Promise&lt;number&gt; | Promise对象,返回文件描述符。 |
P
panqiangbiao 已提交
1377 1378 1379

**示例:**

1380
```js
P
panqiangbiao 已提交
1381
async function example() {
H
huweiqi 已提交
1382 1383 1384 1385 1386 1387 1388 1389 1390
  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 已提交
1391
}
P
panqiangbiao 已提交
1392 1393
```

P
panqiangbiao 已提交
1394
### close<sup>8+</sup>
P
panqiangbiao 已提交
1395

P
panqiangbiao 已提交
1396
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1397 1398 1399

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

H
huweiqi 已提交
1400 1401 1402
> **说明:**
> 此接口从API Version 9开始废弃。请使用[close](js-apis-photoAccessHelper.md#close)替代。

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

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

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

H
HelloCrease 已提交
1409 1410
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
H
huweiqi 已提交
1411 1412
| fd       | number                    | 是    | 文件描述符。 |
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。 |
P
panqiangbiao 已提交
1413 1414 1415

**示例:**

1416
```js
P
panqiangbiao 已提交
1417
async function example() {
H
huweiqi 已提交
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  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 已提交
1435
    });
H
huweiqi 已提交
1436 1437 1438 1439
  }).catch((error) => {
    console.error('File open failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1440
}
P
panqiangbiao 已提交
1441 1442
```

P
panqiangbiao 已提交
1443
### close<sup>8+</sup>
P
panqiangbiao 已提交
1444

P
panqiangbiao 已提交
1445
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1446 1447 1448

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

H
huweiqi 已提交
1449 1450 1451
> **说明:**
> 此接口从API Version 9开始废弃。请使用[close](js-apis-photoAccessHelper.md#close-1)替代。

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

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

P
panqiangbiao 已提交
1456 1457
**参数:**

H
HelloCrease 已提交
1458 1459
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
H
huweiqi 已提交
1460
| fd   | number | 是    | 文件描述符。 |
P
panqiangbiao 已提交
1461 1462 1463

**返回值:**

H
HelloCrease 已提交
1464 1465
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1466
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
1467 1468 1469

**示例:**

1470
```js
P
panqiangbiao 已提交
1471
async function example() {
H
huweiqi 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
  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 已提交
1487
    });
H
huweiqi 已提交
1488 1489 1490 1491
  }).catch((error) => {
    console.error('open File failed with error: ' + error);
  });
  fetchFileResult.close();
P
panqiangbiao 已提交
1492
}
P
panqiangbiao 已提交
1493 1494
```

P
panqiangbiao 已提交
1495
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1496

P
panqiangbiao 已提交
1497
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1498 1499 1500

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

H
huweiqi 已提交
1501 1502 1503
> **说明:**
> 此接口从API Version 9开始废弃。请使用[close](js-apis-photoAccessHelper.md#getThumbnail)替代。

P
panqiangbiao 已提交
1504
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1505

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

P
panqiangbiao 已提交
1508 1509
**参数:**

H
HelloCrease 已提交
1510 1511
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
H
huweiqi 已提交
1512
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | callback返回缩略图的PixelMap。 |
P
panqiangbiao 已提交
1513 1514 1515

**示例:**

1516
```js
P
panqiangbiao 已提交
1517
async function example() {
H
huweiqi 已提交
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
  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 已提交
1535
}
P
panqiangbiao 已提交
1536 1537
```

P
panqiangbiao 已提交
1538
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1539

P
panqiangbiao 已提交
1540
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1541 1542 1543

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

H
huweiqi 已提交
1544 1545 1546
> **说明:**
> 此接口从API Version 9开始废弃。请使用[close](js-apis-photoAccessHelper.md#getThumbnail-1)替代。

P
panqiangbiao 已提交
1547
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1548

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

P
panqiangbiao 已提交
1551 1552
**参数:**

H
HelloCrease 已提交
1553 1554
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
H
huweiqi 已提交
1555 1556
| size     | [Size](#size8)                      | 是    | 缩略图尺寸。            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | callback返回缩略图的PixelMap。 |
P
panqiangbiao 已提交
1557 1558 1559

**示例:**

1560
```js
P
panqiangbiao 已提交
1561
async function example() {
H
huweiqi 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
  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 已提交
1580
}
P
panqiangbiao 已提交
1581 1582
```

P
panqiangbiao 已提交
1583
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1584

P
panqiangbiao 已提交
1585
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1586 1587 1588

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

H
huweiqi 已提交
1589 1590 1591
> **说明:**
> 此接口从API Version 9开始废弃。请使用[close](js-apis-photoAccessHelper.md#getThumbnail-2)替代。

P
panqiangbiao 已提交
1592
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1593

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

P
panqiangbiao 已提交
1596 1597
**参数:**

H
HelloCrease 已提交
1598 1599
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
H
huweiqi 已提交
1600
| size | [Size](#size8) | 否    | 缩略图尺寸。 |
P
panqiangbiao 已提交
1601 1602 1603

**返回值:**

H
HelloCrease 已提交
1604 1605
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
H
huweiqi 已提交
1606
| Promise&lt;image.PixelMap&gt; | Promise对象,返回缩略图的PixelMap。 |
P
panqiangbiao 已提交
1607 1608 1609

**示例:**

1610
```js
P
panqiangbiao 已提交
1611
async function example() {
H
huweiqi 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
  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 已提交
1628
}
P
panqiangbiao 已提交
1629 1630
```

P
panqiangbiao 已提交
1631
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1632

P
panqiangbiao 已提交
1633
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1634 1635 1636

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

H
huweiqi 已提交
1637 1638 1639
> **说明:**
> 此接口从API Version 9开始废弃。请使用[setFavorite](js-apis-photoAccessHelper.md#setfavorite)替代。

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

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

P
panqiangbiao 已提交
1644 1645
**参数:**

H
HelloCrease 已提交
1646 1647
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
H
huweiqi 已提交
1648 1649
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
| callback   | AsyncCallback&lt;void&gt; | 是    | callback返回空。                              |
P
panqiangbiao 已提交
1650 1651 1652

**示例:**

1653
```js
P
panqiangbiao 已提交
1654
async function example() {
H
huweiqi 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
  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 已提交
1672
}
P
panqiangbiao 已提交
1673 1674
```

P
panqiangbiao 已提交
1675
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1676

P
panqiangbiao 已提交
1677
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1678 1679 1680

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

H
huweiqi 已提交
1681 1682 1683
> **说明:**
> 此接口从API Version 9开始废弃。请使用[setFavorite](js-apis-photoAccessHelper.md#setfavorite-1)替代。

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

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

P
panqiangbiao 已提交
1688 1689
**参数:**

H
HelloCrease 已提交
1690 1691
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
H
huweiqi 已提交
1692
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏。 |
P
panqiangbiao 已提交
1693 1694 1695

**返回值:**

H
HelloCrease 已提交
1696 1697
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1698
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
1699 1700 1701

**示例:**

1702
```js
P
panqiangbiao 已提交
1703
async function example() {
H
huweiqi 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
  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 已提交
1719
}
P
panqiangbiao 已提交
1720 1721
```

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

P
panqiangbiao 已提交
1724
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1725 1726 1727

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

H
huweiqi 已提交
1728 1729 1730
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

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

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

H
HelloCrease 已提交
1737 1738
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
H
huweiqi 已提交
1739
| callback | AsyncCallback&lt;boolean&gt; | 是    | callback返回boolean值,值为true则为已收藏,值为false则为未收藏。 |
P
panqiangbiao 已提交
1740 1741 1742

**示例:**

1743
```js
P
panqiangbiao 已提交
1744
async function example() {
H
huweiqi 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
  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 已提交
1762
}
P
panqiangbiao 已提交
1763 1764
```

P
panqiangbiao 已提交
1765
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1766

P
panqiangbiao 已提交
1767
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1768 1769 1770

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

H
huweiqi 已提交
1771 1772 1773
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

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

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

H
HelloCrease 已提交
1780 1781
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
H
huweiqi 已提交
1782
| Promise&lt;boolean&gt; | Promise对象,返回boolean值,值为true则为已收藏,值为false则为未收藏。 |
P
panqiangbiao 已提交
1783 1784 1785

**示例:**

1786
```js
P
panqiangbiao 已提交
1787
async function example() {
H
huweiqi 已提交
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
  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 已提交
1803
}
P
panqiangbiao 已提交
1804 1805
```

P
panqiangbiao 已提交
1806
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1807

A
AOL 已提交
1808
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1809 1810 1811

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

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

H
huweiqi 已提交
1814 1815 1816
> **说明:**
> 此接口从API Version 9开始废弃。请使用[deleteAssets](js-apis-photoAccessHelper.md#deleteAssets)替代。

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

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

P
panqiangbiao 已提交
1821 1822
**参数:**

H
HelloCrease 已提交
1823 1824
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
H
huweiqi 已提交
1825 1826
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件。 |
| callback | AsyncCallback&lt;void&gt; | 是    | callback返回空。     |
P
panqiangbiao 已提交
1827 1828 1829

**示例:**

1830
```js
P
panqiangbiao 已提交
1831
async function example() {
H
huweiqi 已提交
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
  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 已提交
1849
}
P
panqiangbiao 已提交
1850 1851
```

P
panqiangbiao 已提交
1852
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1853

A
AOL 已提交
1854
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1855 1856 1857

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

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

H
huweiqi 已提交
1860 1861 1862
> **说明:**
> 此接口从API Version 9开始废弃。请使用[deleteAssets](js-apis-photoAccessHelper.md#deleteAssets-1)替代。

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

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

P
panqiangbiao 已提交
1867 1868
**参数:**

H
HelloCrease 已提交
1869 1870
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
H
huweiqi 已提交
1871
| isTrash | boolean | 是    | 是否设置为垃圾文件。 |
P
panqiangbiao 已提交
1872 1873 1874

**返回值:**

H
HelloCrease 已提交
1875 1876
| 类型                  | 说明         |
| ------------------- | ---------- |
H
huweiqi 已提交
1877
| Promise&lt;void&gt; | Promise对象,返回空 |
P
panqiangbiao 已提交
1878 1879 1880

**示例:**

1881
```js
P
panqiangbiao 已提交
1882
async function example() {
H
huweiqi 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
  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 已提交
1898
}
P
panqiangbiao 已提交
1899 1900
```

P
panqiangbiao 已提交
1901
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1902

P
panqiangbiao 已提交
1903
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1904 1905 1906

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

H
huweiqi 已提交
1907 1908 1909
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

P
panqiangbiao 已提交
1910
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1911

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

P
panqiangbiao 已提交
1914 1915
**参数:**

H
HelloCrease 已提交
1916 1917
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
H
huweiqi 已提交
1918
| callback | AsyncCallback&lt;boolean&gt; | 是    | callback返回boolean值,值为true则为垃圾文件,值为false则为非垃圾文件。 |
P
panqiangbiao 已提交
1919 1920 1921

**示例:**

1922
```js
P
panqiangbiao 已提交
1923
async function example() {
H
huweiqi 已提交
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
  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 已提交
1941
}
P
panqiangbiao 已提交
1942 1943
```

P
panqiangbiao 已提交
1944
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1945

P
panqiangbiao 已提交
1946
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1947

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

H
huweiqi 已提交
1950 1951 1952
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

P
panqiangbiao 已提交
1953
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1954

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

P
panqiangbiao 已提交
1957 1958
**返回值:**

H
HelloCrease 已提交
1959 1960
| 类型                  | 说明                   |
| ------------------- | -------------------- |
H
huweiqi 已提交
1961
| Promise&lt;void&gt; | Promise对象,返回boolean值,值为true则为垃圾文件,值为false则为非垃圾文件。 |
P
panqiangbiao 已提交
1962 1963 1964

**示例:**

1965
```js
P
panqiangbiao 已提交
1966
async function example() {
H
huweiqi 已提交
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
  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 已提交
1982
}
P
panqiangbiao 已提交
1983 1984
```

Z
zengyawen 已提交
1985
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1986 1987 1988

文件检索结果集。

H
huweiqi 已提交
1989 1990 1991
> **说明:**
> 此接口从API Version 9开始废弃。请使用[FetchResult](js-apis-photoAccessHelper.md#fetchresult)替代。

Z
zengyawen 已提交
1992
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1993

P
panqiangbiao 已提交
1994
getCount(): number
P
panqiangbiao 已提交
1995 1996 1997

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

H
huweiqi 已提交
1998 1999 2000
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getCount](js-apis-photoAccessHelper.md#getcount)替代。

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

H
huweiqi 已提交
2003
**返回值:**
P
panqiangbiao 已提交
2004

H
HelloCrease 已提交
2005 2006
| 类型     | 说明       |
| ------ | -------- |
H
huweiqi 已提交
2007
| number | 检索到的文件总数。 |
P
panqiangbiao 已提交
2008

H
huweiqi 已提交
2009
**示例:**
P
panqiangbiao 已提交
2010

2011
```js
P
panqiangbiao 已提交
2012
async function example() {
H
huweiqi 已提交
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023
  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 已提交
2024
}
P
panqiangbiao 已提交
2025 2026
```

Z
zengyawen 已提交
2027
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
2028

P
panqiangbiao 已提交
2029
isAfterLast(): boolean
P
panqiangbiao 已提交
2030 2031 2032

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

H
huweiqi 已提交
2033 2034 2035
> **说明:**
> 此接口从API Version 9开始废弃。请使用[isAfterLast](js-apis-photoAccessHelper.md#isafterlast)替代。

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

H
huweiqi 已提交
2038
**返回值:**
P
panqiangbiao 已提交
2039

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

H
huweiqi 已提交
2044
**示例:**
P
panqiangbiao 已提交
2045

2046
```js
P
panqiangbiao 已提交
2047
async function example() {
H
huweiqi 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
  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 已提交
2065
    }
H
huweiqi 已提交
2066
  }
P
panqiangbiao 已提交
2067
}
P
panqiangbiao 已提交
2068 2069
```

Z
zengyawen 已提交
2070
### close<sup>7+</sup>
P
panqiangbiao 已提交
2071

P
panqiangbiao 已提交
2072
close(): void
P
panqiangbiao 已提交
2073 2074 2075

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

H
huweiqi 已提交
2076 2077 2078
> **说明:**
> 此接口从API Version 9开始废弃。请使用[close](js-apis-photoAccessHelper.md#close)替代。

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

H
huweiqi 已提交
2081
**示例:**
P
panqiangbiao 已提交
2082

2083
```js
P
panqiangbiao 已提交
2084
async function example() {
H
huweiqi 已提交
2085 2086 2087 2088 2089 2090 2091 2092 2093
  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 已提交
2094
}
P
panqiangbiao 已提交
2095 2096
```

Z
zengyawen 已提交
2097
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
2098

P
panqiangbiao 已提交
2099
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2100 2101 2102

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

H
huweiqi 已提交
2103 2104 2105
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getFirstObject](js-apis-photoAccessHelper.md#getfirstobject)替代。

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

H
huweiqi 已提交
2108
**参数:**
P
panqiangbiao 已提交
2109

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

H
huweiqi 已提交
2114
**示例:**
P
panqiangbiao 已提交
2115

2116
```js
P
panqiangbiao 已提交
2117
async function example() {
H
huweiqi 已提交
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
  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 已提交
2134
}
P
panqiangbiao 已提交
2135 2136
```

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

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

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

H
huweiqi 已提交
2143 2144 2145
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getFirstObject](js-apis-photoAccessHelper.md#getfirstobject-1)替代。

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

H
huweiqi 已提交
2148
**返回值:**
P
panqiangbiao 已提交
2149

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

H
huweiqi 已提交
2154
**示例:**
P
panqiangbiao 已提交
2155

2156
```js
P
panqiangbiao 已提交
2157
async function example() {
H
huweiqi 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171
  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 已提交
2172
}
P
panqiangbiao 已提交
2173 2174
```

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

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

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

H
huweiqi 已提交
2181 2182 2183 2184
> **说明:**
>
> - 在使用前需要先使用[getFirstObject](#getfirstobject7)接口获取第一个文件资产,然后使用[isAfterLast](#isafterlast7)确认文件检索集当前不是指向最后一个时方可使用此接口。
> - 此接口从API Version 9开始废弃。请使用[getNextObject](js-apis-photoAccessHelper.md#getnextobject)替代。
P
panqiangbiao 已提交
2185

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

H
huweiqi 已提交
2188
**参数:**
P
panqiangbiao 已提交
2189

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

H
huweiqi 已提交
2194
**示例:**
P
panqiangbiao 已提交
2195

2196
```js
P
panqiangbiao 已提交
2197
async function example() {
H
huweiqi 已提交
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
  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 已提交
2218
}
H
huweiqi 已提交
2219

P
panqiangbiao 已提交
2220 2221
```

Z
zengyawen 已提交
2222
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2223

H
huweiqi 已提交
2224
getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2225 2226 2227

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

H
huweiqi 已提交
2228 2229 2230 2231
> **说明:**
>
> - 在使用前需要先使用[getFirstObject](#getfirstobject7)接口获取第一个文件资产,然后使用[isAfterLast](#isafterlast7)确认文件检索集当前不是指向最后一个时方可使用此接口。
> - 此接口从API Version 9开始废弃。请使用[getNextObject](js-apis-photoAccessHelper.md#getnextobject-1)替代。
H
huweiqi 已提交
2232

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

H
huweiqi 已提交
2235
**返回值:**
P
panqiangbiao 已提交
2236

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

H
huweiqi 已提交
2241
**示例:**
P
panqiangbiao 已提交
2242

2243
```js
P
panqiangbiao 已提交
2244
async function example() {
H
huweiqi 已提交
2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
  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 已提交
2263
}
P
panqiangbiao 已提交
2264 2265
```

Z
zengyawen 已提交
2266
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2267

P
panqiangbiao 已提交
2268
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2269 2270 2271

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

H
huweiqi 已提交
2272 2273 2274
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getLastObject](js-apis-photoAccessHelper.md#getlastobject)替代。

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

H
huweiqi 已提交
2277
**参数:**
P
panqiangbiao 已提交
2278

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

H
huweiqi 已提交
2283
**示例:**
P
panqiangbiao 已提交
2284

2285
```js
P
panqiangbiao 已提交
2286
async function example() {
H
huweiqi 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
  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 已提交
2303
}
P
panqiangbiao 已提交
2304 2305
```

Z
zengyawen 已提交
2306
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2307

P
panqiangbiao 已提交
2308
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2309 2310 2311

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

H
huweiqi 已提交
2312 2313 2314
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getLastObject](js-apis-photoAccessHelper.md#getlastobject-1)替代。

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

H
huweiqi 已提交
2317
**返回值:**
P
panqiangbiao 已提交
2318

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

H
huweiqi 已提交
2323
**示例:**
P
panqiangbiao 已提交
2324

2325
```js
P
panqiangbiao 已提交
2326
async function example() {
H
huweiqi 已提交
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
  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 已提交
2341
}
P
panqiangbiao 已提交
2342 2343
```

Z
zengyawen 已提交
2344
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2345

P
panqiangbiao 已提交
2346
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2347 2348 2349

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

H
huweiqi 已提交
2350 2351 2352
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getObjectByPosition](js-apis-photoAccessHelper.md#getobjectbyposition)替代。

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

H
huweiqi 已提交
2355
**参数:**
P
panqiangbiao 已提交
2356

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

H
huweiqi 已提交
2362
**示例:**
P
panqiangbiao 已提交
2363

2364
```js
P
panqiangbiao 已提交
2365
async function example() {
H
huweiqi 已提交
2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
  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 已提交
2382
}
P
panqiangbiao 已提交
2383 2384
```

Z
zengyawen 已提交
2385
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2386

P
panqiangbiao 已提交
2387
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2388 2389 2390

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

H
huweiqi 已提交
2391 2392 2393
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getObjectByPosition](js-apis-photoAccessHelper.md#getobjectbyposition-1)替代。

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

H
huweiqi 已提交
2396
**参数:**
P
panqiangbiao 已提交
2397

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

H
huweiqi 已提交
2402
**返回值:**
P
panqiangbiao 已提交
2403

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

H
huweiqi 已提交
2408
**示例:**
P
panqiangbiao 已提交
2409

2410
```js
P
panqiangbiao 已提交
2411
async function example() {
H
huweiqi 已提交
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
  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 已提交
2426
}
P
panqiangbiao 已提交
2427 2428
```

Z
zengyawen 已提交
2429
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2430

P
panqiangbiao 已提交
2431
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2432 2433 2434

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

H
huweiqi 已提交
2435 2436 2437
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getAllObjects](js-apis-photoAccessHelper.md#getallobjects)替代。

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

H
huweiqi 已提交
2440
**参数:**
P
panqiangbiao 已提交
2441

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

H
huweiqi 已提交
2446
**示例:**
P
panqiangbiao 已提交
2447

2448
```js
P
panqiangbiao 已提交
2449
async function example() {
H
huweiqi 已提交
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
  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 已提交
2468
}
P
panqiangbiao 已提交
2469 2470
```

Z
zengyawen 已提交
2471
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2472

P
panqiangbiao 已提交
2473
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2474 2475 2476

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

H
huweiqi 已提交
2477 2478 2479
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getAllObjects](js-apis-photoAccessHelper.md#getallobjects-1)替代。

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

H
huweiqi 已提交
2482
**返回值:**
P
panqiangbiao 已提交
2483

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

H
huweiqi 已提交
2488
**示例:**
P
panqiangbiao 已提交
2489

2490
```js
P
panqiangbiao 已提交
2491
async function example() {
H
huweiqi 已提交
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
  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 已提交
2508
}
P
panqiangbiao 已提交
2509 2510
```

Z
zengyawen 已提交
2511
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2512 2513 2514

实体相册

H
huweiqi 已提交
2515 2516 2517
> **说明:**
> 此接口从API Version 9开始废弃。请使用[Album](js-apis-photoAccessHelper.md#album)替代。

Z
zengyawen 已提交
2518
### 属性
P
panqiangbiao 已提交
2519

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

Z
zengyawen 已提交
2522
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2523
| ------------ | ------ | ---- | ---- | ------- |
H
huweiqi 已提交
2524 2525 2526 2527 2528 2529 2530
| 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 已提交
2531

P
panqiangbiao 已提交
2532 2533 2534
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2535 2536 2537

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

H
huweiqi 已提交
2538 2539 2540
> **说明:**
> 此接口从API Version 9开始废弃。请使用[commitModify](js-apis-photoAccessHelper.md#commitmodify-2)替代。

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

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

H
huweiqi 已提交
2545
**参数:**
P
panqiangbiao 已提交
2546

Z
zengyawen 已提交
2547 2548
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
H
huweiqi 已提交
2549
| callback | AsyncCallback&lt;void&gt; | 是   | callback返回空。 |
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 2564 2565 2566 2567 2568
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册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 已提交
2569
  });
P
panqiangbiao 已提交
2570
}
P
panqiangbiao 已提交
2571 2572
```

P
panqiangbiao 已提交
2573
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2574

P
panqiangbiao 已提交
2575
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2576 2577 2578

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

H
huweiqi 已提交
2579 2580 2581
> **说明:**
> 此接口从API Version 9开始废弃。请使用[commitModify](js-apis-photoAccessHelper.md#commitmodify-3)替代。

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

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

H
huweiqi 已提交
2586
**返回值:**
P
panqiangbiao 已提交
2587

H
HelloCrease 已提交
2588 2589
| 类型                  | 说明           |
| ------------------- | ------------ |
H
huweiqi 已提交
2590
| Promise&lt;void&gt; | Promise对象,返回空。 |
P
panqiangbiao 已提交
2591

H
huweiqi 已提交
2592
**示例:**
P
panqiangbiao 已提交
2593

2594
```js
P
panqiangbiao 已提交
2595
async function example() {
H
huweiqi 已提交
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册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回调来返回文件结果集。

H
huweiqi 已提交
2618 2619 2620
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getAssets](js-apis-photoAccessHelper.md#getassets)替代。

H
huweiqi 已提交
2621 2622 2623 2624
**需要权限**:ohos.permission.READ_MEDIA

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

H
huweiqi 已提交
2625
**参数:**
H
huweiqi 已提交
2626 2627 2628

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

H
huweiqi 已提交
2631
**示例:**
H
huweiqi 已提交
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652

```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 已提交
2653
}
P
panqiangbiao 已提交
2654 2655
```

Z
zengyawen 已提交
2656
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2657

P
panqiangbiao 已提交
2658
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2659 2660 2661

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

H
huweiqi 已提交
2662 2663 2664
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getAssets](js-apis-photoAccessHelper.md#getassets)替代。

P
panqiangbiao 已提交
2665
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2666

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

H
huweiqi 已提交
2669
**参数:**
P
panqiangbiao 已提交
2670

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

H
huweiqi 已提交
2676
**示例:**
P
panqiangbiao 已提交
2677

2678
```js
P
panqiangbiao 已提交
2679
async function example() {
H
huweiqi 已提交
2680 2681 2682 2683 2684 2685 2686 2687
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
H
huweiqi 已提交
2688 2689
  };
  // 获取符合检索要求的相册,返回相册列表。
H
huweiqi 已提交
2690 2691
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
H
huweiqi 已提交
2692
  // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源。
H
huweiqi 已提交
2693 2694 2695 2696
  album.getFileAssets(fileNoArgsfetchOp, (error, fetchFileResult) => {
    if (error) {
      console.error('album getFileAssets failed with error: ' + error);
      return;
Z
zhang-daiyue 已提交
2697
    }
H
huweiqi 已提交
2698 2699 2700 2701
    let count = fetchFileResult.getCount();
    console.info('album getFileAssets successfully, count: ' + count);
    fetchFileResult.close();
  });
P
panqiangbiao 已提交
2702 2703 2704
}
```

Z
zengyawen 已提交
2705
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2706

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

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

H
huweiqi 已提交
2711 2712 2713
> **说明:**
> 此接口从API Version 9开始废弃。请使用[getAssets](js-apis-photoAccessHelper.md#getassets-1)替代。

P
panqiangbiao 已提交
2714
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2715

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

H
huweiqi 已提交
2718
**参数:**
P
panqiangbiao 已提交
2719

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

H
huweiqi 已提交
2724
**返回值:**
P
panqiangbiao 已提交
2725

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

H
huweiqi 已提交
2730
**示例:**
P
panqiangbiao 已提交
2731

2732
```js
P
panqiangbiao 已提交
2733
async function example() {
H
huweiqi 已提交
2734 2735 2736 2737 2738 2739 2740 2741 2742
  // 获取相册需要先预置相册和资源,示例代码为预置的新建相册1。
  let AlbumNoArgsfetchOp = {
    selections: mediaLibrary.FileKey.ALBUM_NAME + '= ?',
    selectionArgs: ['新建相册1'],
  };
  let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
  };
H
huweiqi 已提交
2743
  // 获取符合检索要求的相册,返回相册列表。
H
huweiqi 已提交
2744 2745
  const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
  const album = albumList[0];
H
huweiqi 已提交
2746
  // 取到相册列表中的一个相册,获取此相册中所有符合媒体检索选项的媒体资源。
H
huweiqi 已提交
2747 2748 2749 2750 2751 2752 2753
  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 已提交
2754
}
P
panqiangbiao 已提交
2755 2756
```

P
panqiangbiao 已提交
2757
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2758

P
panqiangbiao 已提交
2759
注册设备的信息。
2760

H
huweiqi 已提交
2761 2762 2763
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

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

Z
zengyawen 已提交
2768 2769
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
H
huweiqi 已提交
2770 2771 2772 2773
| deviceName | string                     | 是   | 否   | 注册设备的名称。   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID。 |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型。         |
| isOnline   | boolean                    | 是   | 否   | 是否在线。         |
P
panqiangbiao 已提交
2774

Z
zengyawen 已提交
2775
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2776 2777 2778

枚举,媒体类型。

H
huweiqi 已提交
2779 2780 2781
> **说明:**
> 此接口从API Version 9开始废弃。请使用[PhotoType](js-apis-photoAccessHelper.md#phototype)替代。

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

H
huweiqi 已提交
2784 2785
| 名称  |  值 |  说明 |
| ----- |  ---- | ---- |
H
huweiqi 已提交
2786 2787 2788 2789
| FILE  |  0 | 文件。 |
| IMAGE |  1 | 图片。 |
| VIDEO |  2 | 视频。|
| AUDIO |  3 | 音频。 |
P
panqiangbiao 已提交
2790

Z
zengyawen 已提交
2791
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2792 2793 2794

枚举,文件关键信息。

zyjhandsome's avatar
zyjhandsome 已提交
2795
> **说明:**
H
huweiqi 已提交
2796 2797 2798
>
> - bucket_id字段在文件重命名或移动后可能会发生变化,开发者使用前需要重新获取。
> - 此接口从API Version 9开始废弃。请使用[PhotoKeys](js-apis-photoAccessHelper.md#photokeys)替代。
H
huweiqi 已提交
2799

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

H
huweiqi 已提交
2802
| 名称          | 值              | 说明                                                       |
Z
zengyawen 已提交
2803
| ------------- | ------------------- | ---------------------------------------------------------- |
H
huweiqi 已提交
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
| 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 已提交
2823

Z
zengyawen 已提交
2824
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2825 2826 2827

枚举,目录类型。

H
huweiqi 已提交
2828 2829 2830
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

H
huweiqi 已提交
2833 2834
| 名称          | 值 |  说明               |
| ------------- | --- | ------------------ |
H
huweiqi 已提交
2835 2836 2837 2838 2839 2840
| DIR_CAMERA    |  0 | 表示Camera文件路径。 |
| DIR_VIDEO     |  1 |  表示视频路径。       |
| DIR_IMAGE     |  2 | 表示图片路径。       |
| DIR_AUDIO     |  3 | 表示音频路径。       |
| DIR_DOCUMENTS |  4 | 表示文档路径。       |
| DIR_DOWNLOAD  |  5 |  表示下载路径。       |
P
panqiangbiao 已提交
2841

Z
zengyawen 已提交
2842
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2843 2844

枚举,设备类型。
2845

H
huweiqi 已提交
2846 2847 2848
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

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

H
huweiqi 已提交
2853 2854
| 名称         |  值 | 说明       |
| ------------ | --- | ---------- |
H
huweiqi 已提交
2855 2856 2857 2858 2859 2860 2861
| TYPE_UNKNOWN |  0 | 未识别设备。 |
| TYPE_LAPTOP  |  1 | 笔记本电脑。 |
| TYPE_PHONE   |  2 | 手机。       |
| TYPE_TABLET  |  3 | 平板电脑。   |
| TYPE_WATCH   |  4 | 智能手表。   |
| TYPE_CAR     |  5 | 车载设备。   |
| TYPE_TV      |  6 | 电视设备。   |
P
panqiangbiao 已提交
2862

Z
zengyawen 已提交
2863
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2864 2865 2866

检索条件。

H
huweiqi 已提交
2867 2868 2869
> **说明:**
> 此接口从API Version 9开始废弃。请使用[FetchOptions](js-apis-photoAccessHelper.md#fetchoptions)替代。

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

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

P
panqiangbiao 已提交
2881
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2882 2883

图片尺寸。
2884

H
huweiqi 已提交
2885 2886 2887
> **说明:**
> 此接口从API Version 9开始废弃。请使用[image.Size](js-apis-image.md#size)替代。

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

H
HelloCrease 已提交
2890 2891
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
H
huweiqi 已提交
2892 2893
| width  | number | 是    | 是    | 宽(单位:像素)。 |
| height | number | 是    | 是    | 高(单位:像素)。 |
P
panqiangbiao 已提交
2894

H
huweiqi 已提交
2895
## MediaAssetOption
P
panqiangbiao 已提交
2896 2897 2898

媒体资源选项。

H
huweiqi 已提交
2899 2900 2901
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

H
huweiqi 已提交
2904 2905 2906 2907 2908
| 名称         | 类型   | 可读 | 可写 | 说明                                                         |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| 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 已提交
2909

H
huweiqi 已提交
2910
## MediaSelectOption
P
panqiangbiao 已提交
2911 2912 2913

媒体资源类型选项。

H
huweiqi 已提交
2914 2915 2916
> **说明:**
> 此接口从API Version 9开始废弃。无替代接口。

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

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