js-apis-medialibrary.md 79.5 KB
Newer Older
Z
zengyawen 已提交
1 2
# 媒体库管理

P
panqiangbiao 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
P
panqiangbiao 已提交
5

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

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

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

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

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

P
panqiangbiao 已提交
19
**参数:** 
P
panqiangbiao 已提交
20

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

**返回值:**

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

**示例:**

```
P
panqiangbiao 已提交
34
import featureAbility from '@ohos.ability.featureAbility';
P
panqiangbiao 已提交
35

P
panqiangbiao 已提交
36
var context = featureAbility.getContext()
P
panqiangbiao 已提交
37
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
38
```
Z
zengyawen 已提交
39 40 41 42 43 44
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

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

Z
zengyawen 已提交
45
> **说明**: 从API Version 8开始,该接口不再维护,推荐使用新接口[mediaLibrary.getMediaLibrary<sup>8+</sup>](#medialibrarygetmedialibrary8)。
Z
zengyawen 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

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

**返回值:**

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

**示例:**

```js
var media = mediaLibrary.getMediaLibrary();
```

P
panqiangbiao 已提交
61
## MediaLibrary
P
panqiangbiao 已提交
62

Z
zengyawen 已提交
63
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
64 65


P
panqiangbiao 已提交
66
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 
P
panqiangbiao 已提交
67 68 69

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

P
panqiangbiao 已提交
70
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
71

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

P
panqiangbiao 已提交
74 75
**参数:**

Z
zengyawen 已提交
76 77 78 79
| 参数名   | 类型                                                | 必填 | 说明                              |
| -------- | --------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 文件获取选项                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步获取FetchFileResult之后的回调 |
P
panqiangbiao 已提交
80 81 82 83

**示例:**

```
P
panqiangbiao 已提交
84 85 86 87 88 89
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
P
panqiangbiao 已提交
90
mediaLibrary.getFileAssets(imagesfetchOp, (error, fetchFileResult) => {
P
panqiangbiao 已提交
91
    if (fetchFileResult != undefined) {
P
panqiangbiao 已提交
92
        console.info('mediaLibraryTest : ASSET_CALLBACK fetchFileResult success');
P
panqiangbiao 已提交
93 94 95 96
        fetchFileResult.getAllObject((err, fileAssetList) => {
            if (fileAssetList != undefined) {
                fileAssetList.forEach(getAllObjectInfo);
            }
P
panqiangbiao 已提交
97 98
    	});
    }
P
panqiangbiao 已提交
99
});
P
panqiangbiao 已提交
100
```
Z
zengyawen 已提交
101
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
102

P
panqiangbiao 已提交
103
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
104 105 106

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

P
panqiangbiao 已提交
107
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
108

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

P
panqiangbiao 已提交
111 112
**参数:**

Z
zengyawen 已提交
113 114 115
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 文件检索选项 |
P
panqiangbiao 已提交
116 117 118

**返回值**

Z
zengyawen 已提交
119 120 121
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | 文件数据结果集 |
P
panqiangbiao 已提交
122 123 124 125

**示例:**

```
P
panqiangbiao 已提交
126 127 128 129 130 131
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
P
panqiangbiao 已提交
132
mediaLibrary.getFileAssets(imagesfetchOp).then(function(fetchFileResult){
P
panqiangbiao 已提交
133 134 135 136
    console.info("getFileAssets successfully:"+ JSON.stringify(dir));
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
P
panqiangbiao 已提交
137 138
```

P
panqiangbiao 已提交
139
### on<sup>8+</sup>
P
panqiangbiao 已提交
140

P
panqiangbiao 已提交
141
on(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
142

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

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

P
panqiangbiao 已提交
147 148
**参数:**

H
HelloCrease 已提交
149 150 151 152
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | 是    | 媒体类型 <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; | 是    | 回调返回空                                    |
P
panqiangbiao 已提交
153 154 155 156

**示例:**

```
P
panqiangbiao 已提交
157
mediaLibrary.on('imageChange', () => {
P
panqiangbiao 已提交
158
    // image file had changed, do something
P
panqiangbiao 已提交
159 160
})
```
P
panqiangbiao 已提交
161
### off<sup>8+</sup>
P
panqiangbiao 已提交
162

P
panqiangbiao 已提交
163
off(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback?: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
164

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

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

P
panqiangbiao 已提交
169 170
**参数:**

H
HelloCrease 已提交
171 172 173 174
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | 是    | 媒体类型 <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; | 否    | 回调返回空                                    |
P
panqiangbiao 已提交
175 176 177 178

**示例:**

```
P
panqiangbiao 已提交
179
mediaLibrary.off('imageChange', () => {
P
panqiangbiao 已提交
180
    // stop listening success
P
panqiangbiao 已提交
181 182 183
})
```

P
panqiangbiao 已提交
184
### createAsset <sup>8+</sup>
P
panqiangbiao 已提交
185

P
panqiangbiao 已提交
186
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
187 188 189

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

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

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

P
panqiangbiao 已提交
194 195
**参数:**

Z
zengyawen 已提交
196 197 198 199 200 201
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型                                                     |
| displayName  | string                                  | 是   | 展示文件名                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
202 203 204 205

**示例:**

```
P
panqiangbiao 已提交
206 207 208 209 210
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
潘强标 已提交
211
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
P
panqiangbiao 已提交
212 213 214 215 216 217 218
        if (fileAsset != undefined) {
            console.info('createAsset successfully, message = ' + err);
        } else {
            console.info('createAsset failed, message = ' + err);
        }
    });
}
P
panqiangbiao 已提交
219 220
```

P
panqiangbiao 已提交
221
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
222

P
panqiangbiao 已提交
223
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
224 225 226

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

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

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

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

Z
zengyawen 已提交
233 234 235 236 237
| 参数名       | 类型                     | 必填 | 说明                                                         |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | 是   | 媒体类型                                                     |
| displayName  | string                   | 是   | 展示文件名                                                   |
| relativePath | string                   | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
238 239 240

**返回值**

Z
zengyawen 已提交
241 242 243
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | 媒体数据FileAsset |
P
panqiangbiao 已提交
244 245 246 247

**示例:**

```
P
panqiangbiao 已提交
248 249 250 251 252
async function example() {
    // 使用Promise方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
潘强标 已提交
253
    media.createAsset(mediaType, "image01.jpg", path + 'myPicture/').then (function (asset) {
P
panqiangbiao 已提交
254 255 256 257 258
        console.info("createAsset successfully:"+ JSON.stringify(asset));
    }).catch(function(err){
        console.info("createAsset failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
259 260
```

P
panqiangbiao 已提交
261
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
262

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

P
panqiangbiao 已提交
265
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
266 267 268 269 270

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

**参数:**

Z
zengyawen 已提交
271 272 273 274
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback 返回公共目录路径 |
P
panqiangbiao 已提交
275 276 277 278 279

**示例:**

```
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
P
panqiangbiao 已提交
280
media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => {
P
panqiangbiao 已提交
281
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
282
        console.info('mediaLibraryTest : getPublicDirectory passed');
P
panqiangbiao 已提交
283
    } else {
P
panqiangbiao 已提交
284
        console.info('mediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
285 286 287 288
    }
});
```

P
panqiangbiao 已提交
289
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
290

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

P
panqiangbiao 已提交
293
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
294 295 296 297 298

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

**参数:**

Z
zengyawen 已提交
299 300 301
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
302 303 304

**返回值:**

Z
zengyawen 已提交
305 306 307
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
308 309 310 311

**示例:**

```
P
panqiangbiao 已提交
312
async function example() {
P
panqiangbiao 已提交
313 314
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
    const dicResult = await media.getPublicDirectory(DIR_CAMERA);
P
panqiangbiao 已提交
315
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
316 317 318 319
        console.info('MediaLibraryTest : getPublicDirectory');
    } else {
        console.info('MediaLibraryTest : getPublicDirectory failed');
    }
P
panqiangbiao 已提交
320 321 322
}
```

Z
zengyawen 已提交
323
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
324

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

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

P
panqiangbiao 已提交
329
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
330

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

P
panqiangbiao 已提交
333 334
**参数**

Z
zengyawen 已提交
335 336 337 338
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册获取条件                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
339 340 341 342

**示例:**

```
P
panqiangbiao 已提交
343 344 345 346
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
P
panqiangbiao 已提交
347
mediaLibrary.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
348 349 350 351 352 353 354
    if (albumList != undefined) {
        const album = albumList[0];
        console.info('album.albumName = ' + album.albumName);
        console.info('album.count = ' + album.count);
     } else {
        console.info('getAlbum fail, message = ' + err);
     }
P
panqiangbiao 已提交
355
})
P
panqiangbiao 已提交
356 357
```

Z
zengyawen 已提交
358
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
359

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

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

P
panqiangbiao 已提交
364
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
365

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

P
panqiangbiao 已提交
368 369
**参数:**

Z
zengyawen 已提交
370 371 372
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
373 374 375

**返回值:**

Z
zengyawen 已提交
376 377 378
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
379 380 381 382

**示例:**

```
P
panqiangbiao 已提交
383 384 385 386
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
P
panqiangbiao 已提交
387
mediaLibrary.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
P
panqiangbiao 已提交
388 389 390 391
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
    console.info("getAlbums failed with error:"+ err);
});
P
panqiangbiao 已提交
392 393
```

P
panqiangbiao 已提交
394
### release<sup>8+</sup>
P
panqiangbiao 已提交
395

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

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

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

P
panqiangbiao 已提交
403 404
**参数:**

H
HelloCrease 已提交
405 406 407
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调表示成功还是失败 |
P
panqiangbiao 已提交
408 409 410 411

**示例:**

```
P
panqiangbiao 已提交
412
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
413
media.release((err) => {
P
panqiangbiao 已提交
414
    // do something
P
panqiangbiao 已提交
415
});
P
panqiangbiao 已提交
416 417
```

P
panqiangbiao 已提交
418
### release<sup>8+</sup>
P
panqiangbiao 已提交
419

P
panqiangbiao 已提交
420
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
421

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

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

P
panqiangbiao 已提交
427 428
**返回值:**

H
HelloCrease 已提交
429 430
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
431
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
432 433 434 435

**示例:**

```
P
panqiangbiao 已提交
436 437
var media = mediaLibrary.getMediaLibrary(context);
media.release()
P
panqiangbiao 已提交
438 439
```

P
panqiangbiao 已提交
440 441 442 443 444 445 446 447 448 449 450 451
### storeMediaAsset

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
452 453 454 455
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
| callback | AsyncCallback&lt;string&gt;           | 是    | 媒体资源保存回调,返回保存成功后得到的URI。 |
P
panqiangbiao 已提交
456 457 458 459 460

**示例:**

  ```
let option = {
潘强标 已提交
461 462 463
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (err, value) => {
    if (err) {
        console.log("An error occurred when storing media resources.");
        return;
    }
    console.log("Media resources stored. ");
    // Obtain the URI that stores media resources.
});
  ```


### storeMediaAsset

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
488 489 490
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
491 492 493

**返回值:**

H
HelloCrease 已提交
494 495
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
496 497 498 499 500 501
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

  ```
let option = {
潘强标 已提交
502 503 504
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
    console.log("Media resources stored.");
    // Obtain the URI that stores media resources.
}).catch((err) => {
    console.log("An error occurred when storing media resources.");
});
  ```


### startImagePreview

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

启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
527 528 529 530 531
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","dataability://")列表。 |
| index    | number                    | 是    | 开始显示的图片序号。                               |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
532 533 534 535 536

**示例:**

  ```
let images = [
潘强标 已提交
537 538
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
P
panqiangbiao 已提交
539
];
H
HelloCrease 已提交
540
/* 网络图片使用方式
P
panqiangbiao 已提交
541 542 543 544
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
545
*/
P
panqiangbiao 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
  ```


### startImagePreview

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

启动图片预览界面,可以预览列表中首张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
569 570 571 572
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","dataability://")列表。 |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
573 574 575 576 577

**示例:**

  ```
let images = [
潘强标 已提交
578 579
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
P
panqiangbiao 已提交
580
];
H
HelloCrease 已提交
581
/* 网络图片使用方式
P
panqiangbiao 已提交
582 583 584 585
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
586
*/
P
panqiangbiao 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
  ```


### startImagePreview

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

启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用Promise方式进行异步回调。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
609 610 611 612
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
| images | Array&lt;string&gt; | 是    | 预览的图片URI("https://","dataability://")列表。 |
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
613 614 615

**返回值:**

H
HelloCrease 已提交
616 617
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
P
panqiangbiao 已提交
618 619 620 621 622 623
| Promise&lt;void&gt; | Promise实例,用于异步获取预览结果,失败时返回错误信息。 |

**示例:**

  ```
let images = [
潘强标 已提交
624 625
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
P
panqiangbiao 已提交
626
];
H
HelloCrease 已提交
627
/* 网络图片使用方式
P
panqiangbiao 已提交
628 629 630 631
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
632
*/
P
panqiangbiao 已提交
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
    console.log("Succeeded in previewing the images.");
}).catch((err) => {
    console.log("An error occurred when previewing the images.");
});
  ```


### startMediaSelect

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
654 655 656 657
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
| option   | [MediaSelectOption](#mediaselectoption)  | 是    | 媒体选择选项。                              |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 媒体选择回调,返回选择的媒体URI(dataability://)列表。 |
P
panqiangbiao 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

**示例:**

  ```
let option = {
    type : "image",
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
    if (err) {
        console.log("An error occurred when selecting media resources.");
        return;
    }
    console.log("Media resources selected.");
    // Obtain the media selection value.
});
  ```


### startMediaSelect

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
689 690 691
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
| option | [MediaSelectOption](#mediaselectoption) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
692 693 694

**返回值:**

H
HelloCrease 已提交
695 696
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
P
panqiangbiao 已提交
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
| Promise&lt;Array&lt;string&gt;&gt; | Promise实例,用于异步获取选择的媒体URI(dataability://)列表。 |

**示例:**

  ```
let option = {
    type : "image",
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
    console.log("Media resources selected.");
    // Obtain the media selection value.
}).catch((err) => {
    console.log("An error occurred when selecting media resources.");
});

  ```

Z
zengyawen 已提交
715
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
716 717 718

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

Z
zengyawen 已提交
719 720 721
### 属性

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

Z
zengyawen 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| id                        | number                   | 是   | 否   | 文件资源编号                                           |
| uri                       | string                   | 是   | 否   | 文件资源uri(如:dataability:///media/image/2)         |
| mimeType                  | string                   | 是   | 否   | 文件扩展属性                                           |
| mediaType<sup>8+</sup>    | [MediaType](#mediatype8) | 是   | 否   | 媒体类型                                               |
| displayName               | string                   | 是   | 是   | 显示文件名,包含后缀名                                 |
| title                     | string                   | 是   | 是   | 文件标题                                               |
| relativePath<sup>8+</sup> | string                   | 是   | 是   | 相对公共目录路径                                       |
| parent<sup>8+</sup>       | number                   | 是   | 否   | 父目录id                                               |
| size                      | number                   | 是   | 否   | 文件大小(单位:字节)                                 |
| dateAdded                 | number                   | 是   | 否   | 添加日期(添加文件时间到1970年1月1日的秒数值)         |
| 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                   | 是   | 否   | 持续时间(单位:秒)                                   |
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
746 747 748


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

P
panqiangbiao 已提交
750
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
751 752 753

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

P
panqiangbiao 已提交
754
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
755

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

P
panqiangbiao 已提交
758 759
**参数:**

H
HelloCrease 已提交
760 761 762
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
763 764 765 766

**示例:**

```
P
panqiangbiao 已提交
767
async function example() {
P
panqiangbiao 已提交
768 769 770 771
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
772 773
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
774 775 776 777 778 779 780
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
781 782
```

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

P
panqiangbiao 已提交
785
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
786 787 788

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

P
panqiangbiao 已提交
789
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
790

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

P
panqiangbiao 已提交
793 794
**返回值:**

H
HelloCrease 已提交
795 796
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
797
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
798 799 800 801

**示例:**

```
P
panqiangbiao 已提交
802
async function example() {
P
panqiangbiao 已提交
803 804 805 806
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
807 808
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
809 810 811 812 813 814 815 816 817
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory().then(function(isDirectory){
        console.info("isDirectory result:"+ isDirectory);
    }).catch(function(err){
        console.info("isDirectory failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
818 819
```

P
panqiangbiao 已提交
820
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
821

P
panqiangbiao 已提交
822
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
823 824 825

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

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

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

P
panqiangbiao 已提交
830 831
**参数:**

H
HelloCrease 已提交
832 833 834
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
835 836 837 838

**示例:**

```
P
panqiangbiao 已提交
839
async function example() {
P
panqiangbiao 已提交
840 841 842 843
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
844 845
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
846 847 848
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
849
    asset.title = 'newtitle';
P
panqiangbiao 已提交
850 851
    asset.commitModify(() => {
        console.info('commitModify success');   
P
panqiangbiao 已提交
852
    });
P
panqiangbiao 已提交
853
}
P
panqiangbiao 已提交
854 855
```

P
panqiangbiao 已提交
856
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
857

P
panqiangbiao 已提交
858
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
859 860 861

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

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

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

P
panqiangbiao 已提交
866 867
**返回值:**

H
HelloCrease 已提交
868 869
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
870
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
871 872 873 874

**示例:**

```
P
panqiangbiao 已提交
875
async function example() {
P
panqiangbiao 已提交
876 877 878 879
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
880 881
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
882 883 884
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
885
    asset.title = 'newtitle';
P
panqiangbiao 已提交
886 887
    asset.commitModify();
}
P
panqiangbiao 已提交
888 889
```

P
panqiangbiao 已提交
890
### open<sup>8+</sup>
P
panqiangbiao 已提交
891

P
panqiangbiao 已提交
892
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
893 894 895

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

P
panqiangbiao 已提交
896
**需要权限**:ohos.permission.READ_MEDIA('r'模式打开),ohos.permission.WRITE_MEDIA('w'模式打开)
P
panqiangbiao 已提交
897

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

P
panqiangbiao 已提交
900 901
**参数**

H
HelloCrease 已提交
902 903 904 905
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件句柄                            |
P
panqiangbiao 已提交
906 907 908 909

**示例:**

```
P
panqiangbiao 已提交
910
async function example() {
P
panqiangbiao 已提交
911
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
912 913
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
P
panqiangbiao 已提交
914 915 916 917 918 919 920 921 922
    asset = await media.createAsset(mediaType, "image00003.jpg", path);
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
P
panqiangbiao 已提交
923 924
```

P
panqiangbiao 已提交
925
### open<sup>8+</sup>
P
panqiangbiao 已提交
926

P
panqiangbiao 已提交
927
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
928 929 930

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

P
panqiangbiao 已提交
931
**需要权限**:ohos.permission.READ_MEDIA('r'模式打开),ohos.permission.WRITE_MEDIA('w'模式打开)
P
panqiangbiao 已提交
932

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

P
panqiangbiao 已提交
935 936
**参数:**

H
HelloCrease 已提交
937 938 939
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
940 941 942

**返回值:**

H
HelloCrease 已提交
943 944
| 类型                    | 说明            |
| --------------------- | ------------- |
P
panqiangbiao 已提交
945
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
946 947 948 949

**示例:**

```
P
panqiangbiao 已提交
950
async function example() {
P
panqiangbiao 已提交
951
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
952 953
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
P
panqiangbiao 已提交
954
    asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
955 956 957 958 959 960 961
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
            console.info('File err!' + err);
        });
P
panqiangbiao 已提交
962
}
P
panqiangbiao 已提交
963 964
```

P
panqiangbiao 已提交
965
### close<sup>8+</sup>
P
panqiangbiao 已提交
966

P
panqiangbiao 已提交
967
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
968 969 970

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

P
panqiangbiao 已提交
971
**需要权限**:ohos.permission.READ_MEDIA('r'模式打开),ohos.permission.WRITE_MEDIA('w'模式打开)
P
panqiangbiao 已提交
972

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

P
panqiangbiao 已提交
975 976
**参数:**

H
HelloCrease 已提交
977 978 979 980
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
981 982 983 984

**示例:**

```
P
panqiangbiao 已提交
985
async function example() {
P
panqiangbiao 已提交
986 987 988 989
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
990 991
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
992 993 994 995 996
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.close(fd, (closeErr) => {
        if (closeErr != undefined) {
P
panqiangbiao 已提交
997 998
            console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
            console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
999 1000 1001 1002 1003
        } else {
            console.info("=======asset.close success====>");
        }
    });
}
P
panqiangbiao 已提交
1004 1005
```

P
panqiangbiao 已提交
1006
### close<sup>8+</sup>
P
panqiangbiao 已提交
1007

P
panqiangbiao 已提交
1008
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1009 1010 1011

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

P
panqiangbiao 已提交
1012
**需要权限**:ohos.permission.READ_MEDIA('r'模式打开),ohos.permission.WRITE_MEDIA('w'模式打开)
P
panqiangbiao 已提交
1013

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

P
panqiangbiao 已提交
1016 1017
**参数:**

H
HelloCrease 已提交
1018 1019 1020
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1021 1022 1023

**返回值:**

H
HelloCrease 已提交
1024 1025
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1026
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1027 1028 1029 1030

**示例:**

```
P
panqiangbiao 已提交
1031
async function example() {
P
panqiangbiao 已提交
1032 1033 1034 1035
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1036 1037
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1038 1039 1040 1041 1042
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.close(fd).then((closeErr) => {
        if (closeErr != undefined) {
P
panqiangbiao 已提交
1043 1044
            console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
            console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
1045 1046 1047 1048 1049 1050

        } else {
            console.info("=======asset.close success====>");
        }
    });
}
P
panqiangbiao 已提交
1051 1052
```

P
panqiangbiao 已提交
1053
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1054

P
panqiangbiao 已提交
1055
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1056 1057 1058

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

P
panqiangbiao 已提交
1059
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1060

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

P
panqiangbiao 已提交
1063 1064
**参数:**

H
HelloCrease 已提交
1065 1066 1067
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1068 1069 1070 1071

**示例:**

```
P
panqiangbiao 已提交
1072
async function example() {
P
panqiangbiao 已提交
1073 1074 1075 1076
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1077 1078
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1079 1080 1081 1082
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
P
panqiangbiao 已提交
1083
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
1084 1085
    });
}
P
panqiangbiao 已提交
1086 1087
```

P
panqiangbiao 已提交
1088
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1089

P
panqiangbiao 已提交
1090
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1091 1092 1093

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

P
panqiangbiao 已提交
1094
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1095

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

P
panqiangbiao 已提交
1098 1099
**参数:**

H
HelloCrease 已提交
1100 1101 1102 1103
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1104 1105 1106 1107

**示例:**

```
P
panqiangbiao 已提交
1108
async function example() {
P
panqiangbiao 已提交
1109 1110 1111 1112
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1113 1114
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1115 1116 1117 1118
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
P
panqiangbiao 已提交
1119
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
1120 1121
    });
}
P
panqiangbiao 已提交
1122 1123
```

P
panqiangbiao 已提交
1124
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1125

P
panqiangbiao 已提交
1126
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1127 1128 1129

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

P
panqiangbiao 已提交
1130
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1131

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

P
panqiangbiao 已提交
1134 1135
**参数:**

H
HelloCrease 已提交
1136 1137 1138
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1139 1140 1141

**返回值:**

H
HelloCrease 已提交
1142 1143
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1144
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1145 1146 1147 1148

**示例:**

```
P
panqiangbiao 已提交
1149
async function example() {
P
panqiangbiao 已提交
1150 1151
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
潘强标 已提交
1152 1153 1154 1155
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1156 1157 1158
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
潘强标 已提交
1159 1160
    asset.getThumbnail(size)
    .then((pixelmap) => {
P
panqiangbiao 已提交
1161
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
潘强标 已提交
1162 1163 1164
    })
    .catch((err) => {
        console.info('mediaLibraryTest : getThumbnail fail'+ err);
P
panqiangbiao 已提交
1165 1166
    });
}
P
panqiangbiao 已提交
1167 1168
```

P
panqiangbiao 已提交
1169
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1170

P
panqiangbiao 已提交
1171
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1172 1173 1174

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

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

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

P
panqiangbiao 已提交
1179 1180
**参数:**

H
HelloCrease 已提交
1181 1182 1183 1184
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1185 1186 1187 1188

**示例:**

```
P
panqiangbiao 已提交
1189
async function example() {
P
panqiangbiao 已提交
1190 1191 1192 1193
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1194 1195
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1196 1197 1198 1199 1200 1201 1202
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
1203 1204
```

P
panqiangbiao 已提交
1205
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1206

P
panqiangbiao 已提交
1207
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1208 1209 1210

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

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

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

P
panqiangbiao 已提交
1215 1216
**参数:**

H
HelloCrease 已提交
1217 1218 1219
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1220 1221 1222

**返回值:**

H
HelloCrease 已提交
1223 1224
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1225
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1226 1227 1228 1229

**示例:**

```
P
panqiangbiao 已提交
1230
async function example() {
P
panqiangbiao 已提交
1231 1232 1233 1234
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1235 1236
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true).then(function() {
        console.info("favorite successfully");
    }).catch(function(err){
        console.info("favorite failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1246 1247
```

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

P
panqiangbiao 已提交
1250
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1251 1252 1253

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

P
panqiangbiao 已提交
1254
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1255

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

P
panqiangbiao 已提交
1258 1259
**参数:**

H
HelloCrease 已提交
1260 1261 1262
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1263 1264 1265 1266

**示例:**

```
P
panqiangbiao 已提交
1267
async function example() {
P
panqiangbiao 已提交
1268 1269 1270 1271
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1272 1273
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite((err, isFavorite) => {
        if (isFavorite) {
            console.info('FileAsset is favorite');
        }else{
            console.info('FileAsset is not favorite');
        }
    });
}
P
panqiangbiao 已提交
1285 1286
```

P
panqiangbiao 已提交
1287
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1288

P
panqiangbiao 已提交
1289
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1290 1291 1292

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

P
panqiangbiao 已提交
1293
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1294

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

P
panqiangbiao 已提交
1297 1298
**返回值:**

H
HelloCrease 已提交
1299 1300
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1301
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1302 1303 1304 1305

**示例:**

```
P
panqiangbiao 已提交
1306
async function example() {
P
panqiangbiao 已提交
1307 1308 1309 1310
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1311 1312
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1313 1314 1315 1316 1317 1318 1319 1320 1321
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite().then(function(isFavorite){
        console.info("isFavorite result:"+ isFavorite);
    }).catch(function(err){
        console.info("isFavorite failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1322 1323
```

P
panqiangbiao 已提交
1324
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1325

A
AOL 已提交
1326
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1327 1328 1329

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

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

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

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

P
panqiangbiao 已提交
1336 1337
**参数:**

H
HelloCrease 已提交
1338 1339 1340 1341
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1342 1343 1344 1345

**示例:**

```
P
panqiangbiao 已提交
1346
async function example() {
P
panqiangbiao 已提交
1347 1348 1349 1350
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1351 1352
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1353 1354 1355 1356 1357
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1358
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1359
    }
P
panqiangbiao 已提交
1360
}
P
panqiangbiao 已提交
1361 1362
```

P
panqiangbiao 已提交
1363
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1364

A
AOL 已提交
1365
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1366 1367 1368

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

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

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

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

P
panqiangbiao 已提交
1375 1376
**参数:**

H
HelloCrease 已提交
1377 1378 1379
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1380 1381 1382

**返回值:**

H
HelloCrease 已提交
1383 1384
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1385
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1386 1387 1388 1389

**示例:**

```
P
panqiangbiao 已提交
1390
async function example() {
P
panqiangbiao 已提交
1391 1392 1393 1394
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1395 1396
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true).then(function() {
        console.info("trash successfully");
    }).catch(function(err){
        console.info("trash failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1406 1407
```

P
panqiangbiao 已提交
1408
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1409

P
panqiangbiao 已提交
1410
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1411 1412 1413

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

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

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

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

H
HelloCrease 已提交
1420 1421 1422
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1423 1424 1425 1426

**示例:**

```
P
panqiangbiao 已提交
1427
async function example() {
P
panqiangbiao 已提交
1428 1429 1430 1431
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1432 1433
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1434 1435 1436 1437 1438 1439
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash(isTrashCallBack);
    function isTrashCallBack(err, isTrash) {
            if (isTrash == true) {
P
panqiangbiao 已提交
1440
                console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK isTrash = ' + isTrash);
P
panqiangbiao 已提交
1441 1442 1443
                asset.trash(true, trashCallBack);

            } else {
P
panqiangbiao 已提交
1444 1445
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');
P
panqiangbiao 已提交
1446 1447

            }
P
panqiangbiao 已提交
1448
    }
P
panqiangbiao 已提交
1449
}
P
panqiangbiao 已提交
1450 1451
```

P
panqiangbiao 已提交
1452
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1453

P
panqiangbiao 已提交
1454
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1455

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

P
panqiangbiao 已提交
1458
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1459

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

P
panqiangbiao 已提交
1462 1463
**返回值:**

H
HelloCrease 已提交
1464 1465
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1466
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1467 1468 1469 1470

**示例:**

```
P
panqiangbiao 已提交
1471
async function example() {
P
panqiangbiao 已提交
1472 1473 1474 1475
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1476 1477
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1478 1479 1480 1481 1482 1483 1484 1485 1486
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash().then(function(isTrash){
        console.info("isTrash result:"+ isTrash);
    }).catch(function(err){
        console.info("isTrash failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1487 1488
```

Z
zengyawen 已提交
1489
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1490 1491 1492

文件检索结果集。

Z
zengyawen 已提交
1493
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1494

P
panqiangbiao 已提交
1495
getCount(): number
P
panqiangbiao 已提交
1496 1497 1498

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

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

P
panqiangbiao 已提交
1501 1502
**返回值**

H
HelloCrease 已提交
1503 1504
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1505
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1506 1507 1508 1509

**示例**

```
P
panqiangbiao 已提交
1510
async function example() {
P
panqiangbiao 已提交
1511 1512 1513
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1514 1515
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1516 1517 1518 1519
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1520 1521
```

Z
zengyawen 已提交
1522
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1523

P
panqiangbiao 已提交
1524
isAfterLast(): boolean
P
panqiangbiao 已提交
1525 1526 1527

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

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

P
panqiangbiao 已提交
1530 1531
**返回值**

H
HelloCrease 已提交
1532 1533
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1534
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1535 1536 1537 1538

**示例**

```
P
panqiangbiao 已提交
1539
async function example() {
P
panqiangbiao 已提交
1540 1541 1542 1543
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1544 1545
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1546 1547 1548
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1549
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1550 1551 1552 1553
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1554
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1555
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1556 1557
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1558
              fetchFileResult.close();
P
panqiangbiao 已提交
1559

P
panqiangbiao 已提交
1560 1561
            }
    }
P
panqiangbiao 已提交
1562
}
P
panqiangbiao 已提交
1563 1564
```

Z
zengyawen 已提交
1565
### close<sup>7+</sup>
P
panqiangbiao 已提交
1566

P
panqiangbiao 已提交
1567
close(): void
P
panqiangbiao 已提交
1568 1569 1570

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

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

P
panqiangbiao 已提交
1573 1574 1575
**示例**

```
P
panqiangbiao 已提交
1576
async function example() {
P
panqiangbiao 已提交
1577 1578 1579 1580
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1581 1582
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1583 1584 1585 1586
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1587 1588
```

Z
zengyawen 已提交
1589
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1590

P
panqiangbiao 已提交
1591
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1592 1593 1594

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

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

P
panqiangbiao 已提交
1597 1598
**参数**

Z
zengyawen 已提交
1599 1600 1601
| 参数名   | 类型                                          | 必填 | 说明                                        |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步获取结果集中第一个FileAsset完成后的回调 |
P
panqiangbiao 已提交
1602 1603 1604 1605

**示例**

```
P
panqiangbiao 已提交
1606
async function example() {
P
panqiangbiao 已提交
1607 1608 1609 1610
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1611 1612
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getFirstObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1623 1624
```

Z
zengyawen 已提交
1625
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1626

P
panqiangbiao 已提交
1627
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1628

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

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

P
panqiangbiao 已提交
1633 1634
**返回值**

Z
zengyawen 已提交
1635 1636
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1637
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1638 1639 1640 1641

**示例**

```
P
panqiangbiao 已提交
1642
async function example() {
P
panqiangbiao 已提交
1643 1644 1645 1646
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1647 1648
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1649 1650 1651 1652 1653 1654 1655 1656
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getFirstObject().then(function(fileAsset){
        console.info("getFirstObject successfully:"+ JSON.stringify(fileAsset));
    }).catch(function(err){
        console.info("getFirstObject failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1657 1658
```

Z
zengyawen 已提交
1659
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1660

P
panqiangbiao 已提交
1661
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1662 1663 1664

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

P
panqiangbiao 已提交
1665
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1666

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

P
panqiangbiao 已提交
1669 1670
**参数**

Z
zengyawen 已提交
1671 1672 1673
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1674 1675 1676 1677

**示例**

```
P
panqiangbiao 已提交
1678
async function example() {
P
panqiangbiao 已提交
1679 1680 1681 1682
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1683 1684
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getNextObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1695 1696
```

Z
zengyawen 已提交
1697
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1698

P
panqiangbiao 已提交
1699
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1700 1701 1702

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

P
panqiangbiao 已提交
1703
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1704

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

P
panqiangbiao 已提交
1707 1708
**返回值**

Z
zengyawen 已提交
1709 1710 1711
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1712 1713 1714 1715

**示例**

```
P
panqiangbiao 已提交
1716
async function example() {
P
panqiangbiao 已提交
1717 1718 1719 1720
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1721 1722
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1723 1724 1725
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1726
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1727 1728
    fileAsset = await fetchFileResult.getNextObject();
}
P
panqiangbiao 已提交
1729 1730
```

Z
zengyawen 已提交
1731
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
1732

P
panqiangbiao 已提交
1733
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1734 1735 1736

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

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

P
panqiangbiao 已提交
1739 1740
**参数**

Z
zengyawen 已提交
1741 1742
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
Z
zengyawen 已提交
1743
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1744 1745 1746 1747

**示例**

```
P
panqiangbiao 已提交
1748
async function example() {
P
panqiangbiao 已提交
1749 1750 1751 1752
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1753 1754
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getLastObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1765 1766
```

Z
zengyawen 已提交
1767
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
1768

P
panqiangbiao 已提交
1769
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1770 1771 1772

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

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

P
panqiangbiao 已提交
1775 1776
**返回值**

Z
zengyawen 已提交
1777 1778 1779
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1780 1781 1782 1783

**示例**

```
P
panqiangbiao 已提交
1784
async function example() {
P
panqiangbiao 已提交
1785 1786 1787 1788
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1789 1790
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1791 1792 1793 1794
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
1795 1796
```

Z
zengyawen 已提交
1797
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
1798

P
panqiangbiao 已提交
1799
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1800 1801 1802

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

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

P
panqiangbiao 已提交
1805 1806
**参数**

Z
zengyawen 已提交
1807
| 参数名       | 类型                                       | 必填   | 说明                 |
H
HelloCrease 已提交
1808 1809
| -------- | ---------------------------------------- | ---- | ------------------ |
| index    | number                                   | 是    | 要获取的文件的索引,从0开始     |
Z
zengyawen 已提交
1810
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是    | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1811 1812 1813 1814

**示例**

```
P
panqiangbiao 已提交
1815
async function example() {
P
panqiangbiao 已提交
1816 1817 1818 1819
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1820 1821
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1822 1823
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1824
    fetchFileResult.getPositionObject(0, (err, value) => {
P
panqiangbiao 已提交
1825 1826 1827 1828 1829 1830 1831
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1832 1833
```

Z
zengyawen 已提交
1834
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
1835

P
panqiangbiao 已提交
1836
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1837 1838 1839

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

P
panqiangbiao 已提交
1840
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1841

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

P
panqiangbiao 已提交
1844 1845
**参数**

Z
zengyawen 已提交
1846
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
1847 1848
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
1849 1850 1851

**返回值**

Z
zengyawen 已提交
1852 1853 1854
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1855 1856 1857 1858

**示例**

```
P
panqiangbiao 已提交
1859
async function example() {
P
panqiangbiao 已提交
1860 1861 1862 1863
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1864 1865
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1866 1867
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1868
    fetchFileResult.getPositionObject(1, (err, value) => {
P
panqiangbiao 已提交
1869 1870 1871 1872 1873 1874 1875
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1876 1877
```

Z
zengyawen 已提交
1878
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
1879

P
panqiangbiao 已提交
1880
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
1881 1882 1883

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

P
panqiangbiao 已提交
1884
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1885

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

P
panqiangbiao 已提交
1888 1889
**参数**

Z
zengyawen 已提交
1890
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
1891
| -------- | ---------------------------------------- | ---- | -------------------- |
Z
zengyawen 已提交
1892
| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
1893 1894 1895 1896

**示例**

```
P
panqiangbiao 已提交
1897
async function example() {
P
panqiangbiao 已提交
1898 1899 1900 1901
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1902 1903
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getAllObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1914 1915
```

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

P
panqiangbiao 已提交
1918
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
1919 1920 1921

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

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

P
panqiangbiao 已提交
1924 1925
**返回值**

Z
zengyawen 已提交
1926 1927 1928
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
1929 1930 1931 1932

**示例**

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

Z
zengyawen 已提交
1946
## Album<sup>7+</sup>
P
panqiangbiao 已提交
1947 1948 1949 1950 1951

实体相册

### **属性**

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

Z
zengyawen 已提交
1954
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
1955
| ------------ | ------ | ---- | ---- | ------- |
Z
zengyawen 已提交
1956 1957 1958
| albumId | number | 是    | 否    | 相册编号    |
| albumName | string | 是    | 是    | 相册名称    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri   |
H
HelloCrease 已提交
1959
| dateModified | number | 是    | 否    | 修改日期    |
Z
zengyawen 已提交
1960 1961 1962
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
1963

P
panqiangbiao 已提交
1964 1965 1966
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1967 1968 1969

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

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

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

P
panqiangbiao 已提交
1974 1975
**参数**

Z
zengyawen 已提交
1976 1977 1978
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
1979 1980 1981 1982

**示例**

```
P
panqiangbiao 已提交
1983
async function example() {
P
panqiangbiao 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify((err) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log('Modify successful.');
    })
}
P
panqiangbiao 已提交
1999 2000
```

P
panqiangbiao 已提交
2001
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2002

P
panqiangbiao 已提交
2003
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2004 2005 2006

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

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

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

P
panqiangbiao 已提交
2011 2012
**返回值**

H
HelloCrease 已提交
2013 2014
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2015 2016 2017 2018 2019
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

```
P
panqiangbiao 已提交
2020
async function example() {
P
panqiangbiao 已提交
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify().then(function() {
        console.info("commitModify successfully");
    }).catch(function(err){
        console.info("commitModify failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2034 2035
```

Z
zengyawen 已提交
2036
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2037

P
panqiangbiao 已提交
2038
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2039 2040 2041

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

P
panqiangbiao 已提交
2042
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2043

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

P
panqiangbiao 已提交
2046 2047
**参数**

Z
zengyawen 已提交
2048
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2049
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2050 2051
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2052 2053 2054 2055

**示例**

```
P
panqiangbiao 已提交
2056
async function example() {
P
panqiangbiao 已提交
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
P
panqiangbiao 已提交
2067 2068 2069
}
```

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

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

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

P
panqiangbiao 已提交
2076
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2077

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

**参数**

Z
zengyawen 已提交
2082
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2083
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2084
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2085 2086 2087

**返回值**

Z
zengyawen 已提交
2088 2089
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2090
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2091 2092 2093 2094

**示例**

```
P
panqiangbiao 已提交
2095
async function example() {
P
panqiangbiao 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
        console.info("getFileAssets successfully:"+ JSON.stringify(albumFetchFileResult));
    }).catch(function(err){
        console.info("getFileAssets failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2108 2109
```

P
panqiangbiao 已提交
2110
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2111

P
panqiangbiao 已提交
2112
注册设备的信息。
P
panqiangbiao 已提交
2113

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

Z
zengyawen 已提交
2116 2117 2118 2119 2120 2121
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2122 2123 2124



Z
zengyawen 已提交
2125
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2126 2127 2128

枚举,媒体类型。

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

Z
zengyawen 已提交
2131 2132
| 名称  | 默认值 | 说明 |
| ----- | ------ | ---- |
潘强标 已提交
2133 2134 2135 2136
| FILE  | 1      | 文件 |
| IMAGE | 3      | 图片 |
| VIDEO | 4      | 视频 |
| AUDIO | 5      | 音频 |
P
panqiangbiao 已提交
2137

Z
zengyawen 已提交
2138
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2139 2140 2141

枚举,文件关键信息。

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

Z
zengyawen 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
| 名称          | 默认值              | 说明                                                       |
| ------------- | ------------------- | ---------------------------------------------------------- |
| ID            | file_id             | 文件编号                                                   |
| RELATIVE_PATH | relative_path       | 相对公共目录路径                                           |
| DISPLAY_NAME  | display_name        | 显示名字                                                   |
| PARENT        | parent              | 父目录id                                                   |
| MIME_TYPE     | mime_type           | 文件扩展属性                                               |
| MEDIA_TYPE    | media_type          | 媒体类型                                                   |
| SIZE          | size                | 文件大小(单位:字节)                                     |
| DATE_ADDED    | date_added          | 添加日期(添加文件时间到1970年1月1日的秒数值)             |
| 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              | 图片高度(单位:像素)                                     |
P
panqiangbiao 已提交
2162
| ORIENTATION   | orientation         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
Z
zengyawen 已提交
2163 2164
| ALBUM_ID      | bucket_id           | 文件所归属的相册编号                                       |
| ALBUM_NAME    | bucket_display_name | 文件所归属相册名称                                         |
P
panqiangbiao 已提交
2165

Z
zengyawen 已提交
2166
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2167 2168 2169

枚举,目录类型。

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

Z
zengyawen 已提交
2172 2173 2174 2175 2176 2177 2178 2179
| 名称          | 默认值 | 说明               |
| ------------- | ------ | ------------------ |
| DIR_CAMERA    | 0      | 表示Camera文件路径 |
| DIR_VIDEO     | 1      | 表示视频路径       |
| DIR_IMAGE     | 2      | 表示图片路径       |
| DIR_AUDIO     | 3      | 表示音频路径       |
| DIR_DOCUMENTS | 4      | 表示文档路径       |
| DIR_DOWNLOAD  | 5      | 表示下载路径       |
P
panqiangbiao 已提交
2180

Z
zengyawen 已提交
2181
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2182 2183 2184

枚举,设备类型。

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

Z
zengyawen 已提交
2187 2188 2189 2190 2191 2192 2193 2194 2195
| 名称         | 默认值 | 说明       |
| ------------ | ------ | ---------- |
| TYPE_UNKNOWN | 0      | 未识别设备 |
| TYPE_LAPTOP  | 1      | 笔记本电脑 |
| TYPE_PHONE   | 2      | 手机       |
| TYPE_TABLET  | 3      | 平板电脑   |
| TYPE_WATCH   | 4      | 智能手表   |
| TYPE_CAR     | 5      | 车载设备   |
| TYPE_TV      | 6      | 电视设备   |
P
panqiangbiao 已提交
2196

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

检索条件。

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

Z
zengyawen 已提交
2203 2204 2205 2206 2207 2208 2209 2210
| 名称                    | 类型                | 可读 | 可写 | 必填 | 说明                                                         |
| ----------------------- | ------------------- | ---- | ---- | ---- | ------------------------------------------------------------ |
| selections              | string              | 是   | 是   | 是   | 检索条件,使用[FileKey](#filekey8)中的枚举值作为检索条件的列名。示例:<br />selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR' +mediaLibrary.FileKey.MEDIA_TYPE + '= ?‘, |
| selectionArgs           | Array&lt;string&gt; | 是   | 是   | 是   | 检索条件的值,对应selections中检索条件列的值。<br />示例:<br />selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
| order<sup>8+</sup>      | string              | 是   | 是   | 否   | 检索结果排序方式,使用[FileKey](#filekey8)中的枚举值作为检索结果排序的列,可以用升序或降序排列。示例:<br />升序排列:order: mediaLibrary.FileKey.DATE_ADDED + " AESC"<br />降序排列:order: mediaLibrary.FileKey.DATE_ADDED + " DESC" |
| uri<sup>8+</sup>        | string              | 是   | 是   | 否   | 文件URI                                                      |
| networkId<sup>8+</sup>  | string              | 是   | 是   | 否   | 注册设备网络ID                                               |
| extendArgs<sup>8+</sup> | string              | 是   | 是   | 否   | 扩展的检索参数,目前没有扩展检索参数                         |
P
panqiangbiao 已提交
2211

P
panqiangbiao 已提交
2212
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2213 2214 2215

图片尺寸。

H
HelloCrease 已提交
2216 2217 2218 2219
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2220

P
panqiangbiao 已提交
2221 2222 2223 2224 2225 2226 2227 2228 2229
## MediaAssetOption

媒体资源选项。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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


H
HelloCrease 已提交
2230 2231
| 名称           | 类型     | 必填   | 描述                                       |
| ------------ | ------ | ---- | ---------------------------------------- |
潘强标 已提交
2232 2233 2234
| 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 已提交
2235 2236 2237 2238 2239 2240 2241 2242 2243

## MediaSelectOption

媒体资源类型选项。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

H
HelloCrease 已提交
2244 2245
| 名称    | 类型     | 必填   | 描述                   |
| ----- | ------ | ---- | -------------------- |
潘强标 已提交
2246 2247
| type  | string | 是    | 媒体类型,包括:image, video, media,当前仅支持media类型 |
| count | number | 是    | 媒体选择,count = 1表示单选,count大于1表示多选。            |