js-apis-medialibrary.md 65.2 KB
Newer Older
P
panqiangbiao 已提交
1 2
媒体库管理
==========
P
panqiangbiao 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
P
panqiangbiao 已提交
5

P
panqiangbiao 已提交
6 7 8 9
 导入模块
---------

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

P
panqiangbiao 已提交
13
## mediaLibrary.getMediaLibrary
P
panqiangbiao 已提交
14

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

P
panqiangbiao 已提交
17
获取媒体库的实例,用于访问和修改用户的个人数据信息。
P
panqiangbiao 已提交
18

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

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

| 参数名  | 类型    | 必填 | 说明                 |
| ------- | ------- | ---- | -------------------- |
P
panqiangbiao 已提交
25
| context | Context | 是 | API7以下接口可不传此参数,API 8以上接口此参数是必填参数,传入Ability实例的context,获取媒体库 |
P
panqiangbiao 已提交
26 27 28 29 30

**返回值:**

| 类型         | 说明   |
| ------------ | :----- |
P
panqiangbiao 已提交
31
| [MediaLibrary](#medialibrary) | 媒体库实例 |
P
panqiangbiao 已提交
32 33 34 35

**示例:**

```
P
panqiangbiao 已提交
36
import featureAbility from '@ohos.ability.featureAbility';
P
panqiangbiao 已提交
37

P
panqiangbiao 已提交
38
var context = featureAbility.getContext()
P
panqiangbiao 已提交
39
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
40
```
P
panqiangbiao 已提交
41
## MediaLibrary
P
panqiangbiao 已提交
42

P
panqiangbiao 已提交
43
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
44 45


P
panqiangbiao 已提交
46
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 
P
panqiangbiao 已提交
47 48 49

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

P
panqiangbiao 已提交
50
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
51

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

P
panqiangbiao 已提交
54 55
**参数:**

P
panqiangbiao 已提交
56 57 58
| 参数名   | 类型                                                | 必填 | 说明                              |
| -------- | --------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions8)            | 是   | 文件获取选项                      |
P
panqiangbiao 已提交
59
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult8)> | 是   | 异步获取FetchFileResult之后的回调 |
P
panqiangbiao 已提交
60 61 62 63

**示例:**

```
P
panqiangbiao 已提交
64 65 66 67 68 69
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
P
panqiangbiao 已提交
70
mediaLibrary.getFileAssets(imagesfetchOp, (error, fetchFileResult) => {
P
panqiangbiao 已提交
71
    if (fetchFileResult != undefined) {
P
panqiangbiao 已提交
72
        console.info('mediaLibraryTest : ASSET_CALLBACK fetchFileResult success');
P
panqiangbiao 已提交
73 74 75 76
        fetchFileResult.getAllObject((err, fileAssetList) => {
            if (fileAssetList != undefined) {
                fileAssetList.forEach(getAllObjectInfo);
            }
P
panqiangbiao 已提交
77 78
    	});
    }
P
panqiangbiao 已提交
79
});
P
panqiangbiao 已提交
80
```
P
panqiangbiao 已提交
81
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
82

P
panqiangbiao 已提交
83
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
84 85 86

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

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

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

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

P
panqiangbiao 已提交
93 94
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
P
panqiangbiao 已提交
95
| options | [MediaFetchOptions](#mediafetchoptions8) | 是   | 文件检索选项 |
P
panqiangbiao 已提交
96 97 98

**返回值**

P
panqiangbiao 已提交
99 100
| 类型                          | 说明           |
| ----------------------------- | -------------- |
P
panqiangbiao 已提交
101
| [FetchFileResult](#fetchfileresult8) | 文件数据结果集 |
P
panqiangbiao 已提交
102 103 104 105

**示例:**

```
P
panqiangbiao 已提交
106 107 108 109 110 111
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
P
panqiangbiao 已提交
112
mediaLibrary.getFileAssets(imagesfetchOp).then(function(fetchFileResult){
P
panqiangbiao 已提交
113 114 115 116
    console.info("getFileAssets successfully:"+ JSON.stringify(dir));
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
P
panqiangbiao 已提交
117 118
```

P
panqiangbiao 已提交
119
### on<sup>8+</sup>
P
panqiangbiao 已提交
120

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

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

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

P
panqiangbiao 已提交
127 128 129 130
**参数:**

| 参数名   | 类型                | 必填 | 说明                   |
| -------- | ---------------- | ---- | ------------------- |
P
panqiangbiao 已提交
131
| type  | string   | 是   | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'fileChange':     &nbsp;文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更 |
P
panqiangbiao 已提交
132
| callback | callback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
133 134 135 136

**示例:**

```
P
panqiangbiao 已提交
137
mediaLibrary.on('imageChange', () => {
P
panqiangbiao 已提交
138
    // image file had changed, do something
P
panqiangbiao 已提交
139 140
})
```
P
panqiangbiao 已提交
141
### off<sup>8+</sup>
P
panqiangbiao 已提交
142

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

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

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

P
panqiangbiao 已提交
149 150 151 152
**参数:**

| 参数名   | 类型                | 必填 | 说明                   |
| -------- | ---------------- | ---- | ------------------- |
P
panqiangbiao 已提交
153
| type  | string | 是   | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'fileChange':     &nbsp;文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更         |
P
panqiangbiao 已提交
154
| callback | callback&lt;void&gt; | 否   | 回调返回空 |
P
panqiangbiao 已提交
155 156 157 158

**示例:**

```
P
panqiangbiao 已提交
159
mediaLibrary.off('imageChange', () => {
P
panqiangbiao 已提交
160
    // stop listening success
P
panqiangbiao 已提交
161 162 163
})
```

P
panqiangbiao 已提交
164
### createAsset <sup>8+</sup>
P
panqiangbiao 已提交
165

P
panqiangbiao 已提交
166
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
167 168 169

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

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

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

P
panqiangbiao 已提交
174 175
**参数:**

P
panqiangbiao 已提交
176 177
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
P
panqiangbiao 已提交
178
| mediaType    | [MediaType](#mediatype)                 | 是   | 媒体类型                                                     |
P
panqiangbiao 已提交
179
| displayName  | string                                  | 是   | 展示文件名                                                   |
P
panqiangbiao 已提交
180
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
P
panqiangbiao 已提交
181
| callback     | AsyncCallback<[FileAsset](#fileasset8)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
182 183 184 185

**示例:**

```
P
panqiangbiao 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
    mediaLibrary.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
        if (fileAsset != undefined) {
            console.info('createAsset successfully, message = ' + err);
        } else {
            console.info('createAsset failed, message = ' + err);
        }
    });
}
P
panqiangbiao 已提交
199 200
```

P
panqiangbiao 已提交
201
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
202

P
panqiangbiao 已提交
203
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
204 205 206

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

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

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

P
panqiangbiao 已提交
211 212
**参数:**

P
panqiangbiao 已提交
213 214
| 参数名       | 类型                    | 必填 | 说明                                                         |
| ------------ | ----------------------- | ---- | ------------------------------------------------------------ |
P
panqiangbiao 已提交
215
| mediaType    | [MediaType](#mediatype) | 是   | 媒体类型                                                     |
P
panqiangbiao 已提交
216 217
| displayName  | string                  | 是   | 展示文件名                                                   |
| relativePath | string                  | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
218 219 220

**返回值**

P
panqiangbiao 已提交
221 222 223
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset8) | 媒体数据FileAsset |
P
panqiangbiao 已提交
224 225 226 227

**示例:**

```
P
panqiangbiao 已提交
228 229 230 231 232 233 234 235 236 237 238
async function example() {
    // 使用Promise方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
    mediaLibrary.createAsset(mediaType, "image01.jpg", path + 'myPicture/').then (function (asset) {
        console.info("createAsset successfully:"+ JSON.stringify(asset));
    }).catch(function(err){
        console.info("createAsset failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
239 240
```

P
panqiangbiao 已提交
241
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
242

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

P
panqiangbiao 已提交
245
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
246 247 248 249 250 251 252

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

**参数:**

| 参数名 | 类型                            | 必填 | 说明         |
| ------ | ------------------------------- | ---- | ------------ |
P
panqiangbiao 已提交
253
| type   | [DirectoryType](#directorytype) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
254 255 256 257 258 259
| callback   |AsyncCallback&lt;string&gt; | 是   | callback 返回公共目录路径 |

**示例:**

```
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
P
panqiangbiao 已提交
260
media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => {
P
panqiangbiao 已提交
261
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
262
        console.info('mediaLibraryTest : getPublicDirectory passed');
P
panqiangbiao 已提交
263
    } else {
P
panqiangbiao 已提交
264
        console.info('mediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
265 266 267 268
    }
});
```

P
panqiangbiao 已提交
269
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
270

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

P
panqiangbiao 已提交
273
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
274 275 276 277 278 279 280

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

**参数:**

| 参数名 | 类型                            | 必填 | 说明         |
| ------ | ------------------------------- | ---- | ------------ |
P
panqiangbiao 已提交
281
| type   | [DirectoryType](#directorytype) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
282 283 284 285 286 287 288 289 290 291

**返回值:**

| 类型            | 说明             |
| --------------- | ---------------- |
| Promise<string> | 返回公共目录路径 |

**示例:**

```
P
panqiangbiao 已提交
292
async function example() {
P
panqiangbiao 已提交
293 294
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
    const dicResult = await media.getPublicDirectory(DIR_CAMERA);
P
panqiangbiao 已提交
295
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
296 297 298 299
        console.info('MediaLibraryTest : getPublicDirectory');
    } else {
        console.info('MediaLibraryTest : getPublicDirectory failed');
    }
P
panqiangbiao 已提交
300 301 302
}
```

P
panqiangbiao 已提交
303
### getAlbums<sup>8+</sup>
P
panqiangbiao 已提交
304

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

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

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

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

P
panqiangbiao 已提交
313 314
**参数**

P
panqiangbiao 已提交
315 316
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
P
panqiangbiao 已提交
317
| options  | [MediaFetchOptions](#mediafetchoptions8)     | 是   | 相册获取条件                |
P
panqiangbiao 已提交
318
| callback | AsyncCallback&lt;Array<[Album](#album8)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
319 320 321 322

**示例:**

```
P
panqiangbiao 已提交
323 324 325 326
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
P
panqiangbiao 已提交
327
mediaLibrary.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
328 329 330 331 332 333 334
    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 已提交
335
})
P
panqiangbiao 已提交
336 337
```

P
panqiangbiao 已提交
338
### getAlbums<sup>8+</sup>
P
panqiangbiao 已提交
339

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

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

P
panqiangbiao 已提交
344
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
345

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

P
panqiangbiao 已提交
348 349
**参数:**

P
panqiangbiao 已提交
350 351 352
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions8) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
353 354 355

**返回值:**

P
panqiangbiao 已提交
356 357 358
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album8)>> | 返回Album列表 |
P
panqiangbiao 已提交
359 360 361 362

**示例:**

```
P
panqiangbiao 已提交
363 364 365 366
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
P
panqiangbiao 已提交
367
mediaLibrary.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
P
panqiangbiao 已提交
368 369 370 371
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
    console.info("getAlbums failed with error:"+ err);
});
P
panqiangbiao 已提交
372 373
```

P
panqiangbiao 已提交
374
### release<sup>8+</sup>
P
panqiangbiao 已提交
375

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

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

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

P
panqiangbiao 已提交
383 384 385 386
**参数:**

| 参数名   | 类型                   | 必填 | 说明                   |
| -------- | ------------------- | ---- | ---------------------- |
P
panqiangbiao 已提交
387
| callback | AsyncCallback&lt;void&gt; | 是   | 回调表示成功还是失败 |
P
panqiangbiao 已提交
388 389 390 391

**示例:**

```
P
panqiangbiao 已提交
392
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
393
media.release((err) => {
P
panqiangbiao 已提交
394
    // do something
P
panqiangbiao 已提交
395
});
P
panqiangbiao 已提交
396 397
```

P
panqiangbiao 已提交
398
### release<sup>8+</sup>
P
panqiangbiao 已提交
399

P
panqiangbiao 已提交
400
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
401

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

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

P
panqiangbiao 已提交
407 408 409 410
**返回值:**

| 类型          | 说明                                |
| ------------- | ----------------------------------- |
P
panqiangbiao 已提交
411
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
412 413 414 415

**示例:**

```
P
panqiangbiao 已提交
416 417
var media = mediaLibrary.getMediaLibrary(context);
media.release()
P
panqiangbiao 已提交
418 419
```

P
panqiangbiao 已提交
420 421 422 423 424 425
## FileAsset<sup>8+</sup>

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

### **属性**

P
panqiangbiao 已提交
426 427 428 429 430
| 名称         | 类型                    | 可读 | 可写 | 说明                                                   |
| ------------ | ----------------------- | ---- | ---- | ------------------------------------------------------ |
| id           | number                  | 是   | 否   | 文件资源编号                                           |
| uri          | string                  | 是   | 否   | 文件资源uri(如:dataability:///media/image/2)         |
| mimeType     | string                  | 是   | 否   | 文件扩展属性                                           |
P
panqiangbiao 已提交
431
| mediaType    | [MediaType](#mediatype) | 是   | 否   | 媒体类型                                               |
P
panqiangbiao 已提交
432
| displayName  | string                  | 是   | 是   | 显示文件名,包含后缀名                                 |
P
panqiangbiao 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
| title        | string                  | 是   | 是   | 文件标题                                               |
| relativePath | string                  | 是   | 是   | 相对公共目录路径                                       |
| parent       | number                  | 是   | 否   | 父目录id                                               |
| size         | number                  | 是   | 否   | 文件大小(单位:字节)                                 |
| dateAdded    | number                  | 是   | 否   | 添加日期(添加文件时间到1970年1月1日的秒数值)         |
| dateModified | number                  | 是   | 否   | 修改日期(修改文件时间到1970年1月1日的秒数值)         |
| dateTaken    | number                  | 是   | 否   | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)         |
| artist       | string                  | 是   | 否   | 作者                                                   |
| audioAlbum   | string                  | 是   | 否   | 专辑                                                   |
| width        | number                  | 是   | 否   | 图片宽度(单位:像素)                                 |
| height       | number                  | 是   | 否   | 图片高度(单位:像素)                                 |
| orientation  | number                  | 是   | 是   | 图片显示方向(顺时针旋转角度,如0,90,180  单位:度) |
| duration     | number                  | 是   | 否   | 持续时间(单位:秒)                                   |
| albumId      | number                  | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri     | string                  | 是   | 否   | 文件所归属相册uri                                      |
| albumName    | string                  | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
449 450 451


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

P
panqiangbiao 已提交
453
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
454 455 456

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

P
panqiangbiao 已提交
457
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
458

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

P
panqiangbiao 已提交
461 462 463 464 465 466 467 468 469
**参数:**

| 参数名   | 类型                   | 必填 | 说明                          |
| -------- | ---------------------- | ---- | ----------------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是   | 当前FileAsset是否是目录的回调 |

**示例:**

```
P
panqiangbiao 已提交
470
async function example() {
P
panqiangbiao 已提交
471 472 473 474
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
475 476
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
477 478 479 480 481 482 483
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
484 485
```

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

P
panqiangbiao 已提交
488
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
489 490 491

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

P
panqiangbiao 已提交
492
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
493

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

P
panqiangbiao 已提交
496 497 498 499
**返回值:**

| 类型             | 说明                                       |
| ---------------- | ------------------------------------------ |
P
panqiangbiao 已提交
500
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
501 502 503 504

**示例:**

```
P
panqiangbiao 已提交
505
async function example() {
P
panqiangbiao 已提交
506 507 508 509
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
510 511
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
512 513 514 515 516 517 518 519 520
    };
    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 已提交
521 522
```

P
panqiangbiao 已提交
523
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
524

P
panqiangbiao 已提交
525
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
526 527 528

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

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

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

P
panqiangbiao 已提交
533 534 535 536 537 538 539 540 541
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |

**示例:**

```
P
panqiangbiao 已提交
542
async function example() {
P
panqiangbiao 已提交
543 544 545 546
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
547 548
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
549 550 551
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
552
    asset.title = 'newtitle';
P
panqiangbiao 已提交
553 554
    asset.commitModify(() => {
        console.info('commitModify success');   
P
panqiangbiao 已提交
555
    });
P
panqiangbiao 已提交
556
}
P
panqiangbiao 已提交
557 558
```

P
panqiangbiao 已提交
559
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
560

P
panqiangbiao 已提交
561
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
562 563 564

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

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

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

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

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
573
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
574 575 576 577

**示例:**

```
P
panqiangbiao 已提交
578
async function example() {
P
panqiangbiao 已提交
579 580 581 582
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
583 584
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
585 586 587
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
588
    asset.title = 'newtitle';
P
panqiangbiao 已提交
589 590
    asset.commitModify();
}
P
panqiangbiao 已提交
591 592
```

P
panqiangbiao 已提交
593
### open<sup>8+</sup>
P
panqiangbiao 已提交
594

P
panqiangbiao 已提交
595
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
596 597 598

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

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

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

P
panqiangbiao 已提交
603 604 605 606
**参数**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
607
| mode     | string                 | 是   | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
608 609 610 611 612
| callback | AsyncCallback&lt;number&gt; | 是   | 回调返回文件句柄 |

**示例:**

```
P
panqiangbiao 已提交
613
async function example() {
P
panqiangbiao 已提交
614
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
615 616
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
P
panqiangbiao 已提交
617 618 619 620 621 622 623 624 625
    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 已提交
626 627
```

P
panqiangbiao 已提交
628
### open<sup>8+</sup>
P
panqiangbiao 已提交
629

P
panqiangbiao 已提交
630
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
631 632 633

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

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

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

P
panqiangbiao 已提交
638 639
**参数:**

P
panqiangbiao 已提交
640 641 642
| 参数名 | 类型   | 必填 | 说明                                                     |
| ------ | ------ | ---- | -------------------------------------------------------- |
| mode   | string | 是   | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
643 644 645 646 647

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
648
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
649 650 651 652

**示例:**

```
P
panqiangbiao 已提交
653
async function example() {
P
panqiangbiao 已提交
654
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
655 656
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
P
panqiangbiao 已提交
657
    asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
658 659 660 661 662 663 664
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
            console.info('File err!' + err);
        });
P
panqiangbiao 已提交
665
}
P
panqiangbiao 已提交
666 667
```

P
panqiangbiao 已提交
668
### close<sup>8+</sup>
P
panqiangbiao 已提交
669

P
panqiangbiao 已提交
670
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
671 672 673

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

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

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

P
panqiangbiao 已提交
678 679 680 681
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
682
| fd       | number                 | 是   | 文件描述符          |
P
panqiangbiao 已提交
683 684 685 686 687
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |

**示例:**

```
P
panqiangbiao 已提交
688
async function example() {
P
panqiangbiao 已提交
689 690 691 692
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
693 694
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
695 696 697 698 699
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.close(fd, (closeErr) => {
        if (closeErr != undefined) {
P
panqiangbiao 已提交
700 701
            console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
            console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
702 703 704 705 706
        } else {
            console.info("=======asset.close success====>");
        }
    });
}
P
panqiangbiao 已提交
707 708
```

P
panqiangbiao 已提交
709
### close<sup>8+</sup>
P
panqiangbiao 已提交
710

P
panqiangbiao 已提交
711
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
712 713 714

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

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

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

P
panqiangbiao 已提交
719 720 721 722 723 724 725 726 727 728
**参数:**

| 参数名 | 类型   | 必填 | 说明       |
| ------ | ------ | ---- | ---------- |
| fd     | number | 是   | 文件描述符 |

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
729
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
730 731 732 733

**示例:**

```
P
panqiangbiao 已提交
734
async function example() {
P
panqiangbiao 已提交
735 736 737 738
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
739 740
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
741 742 743 744 745
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.close(fd).then((closeErr) => {
        if (closeErr != undefined) {
P
panqiangbiao 已提交
746 747
            console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
            console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
748 749 750 751 752 753

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

P
panqiangbiao 已提交
756
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
757

P
panqiangbiao 已提交
758
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
759 760 761

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

P
panqiangbiao 已提交
762
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
763

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

P
panqiangbiao 已提交
766 767 768 769 770 771 772 773 774
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是   | 回调返回缩略图的PixelMap |

**示例:**

```
P
panqiangbiao 已提交
775
async function example() {
P
panqiangbiao 已提交
776 777 778 779
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
780 781
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
782 783 784 785
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
P
panqiangbiao 已提交
786
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
787 788
    });
}
P
panqiangbiao 已提交
789 790
```

P
panqiangbiao 已提交
791
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
792

P
panqiangbiao 已提交
793
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
794 795 796

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

P
panqiangbiao 已提交
797
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
798

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

P
panqiangbiao 已提交
801 802 803 804
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
805
| size     | [Size](#size8)        | 是   | 缩略图尺寸          |
P
panqiangbiao 已提交
806 807 808 809 810
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是   | 回调返回缩略图的PixelMap |

**示例:**

```
P
panqiangbiao 已提交
811
async function example() {
P
panqiangbiao 已提交
812 813 814 815
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
816 817
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
818 819 820 821
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
P
panqiangbiao 已提交
822
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
823 824
    });
}
P
panqiangbiao 已提交
825 826
```

P
panqiangbiao 已提交
827
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
828

P
panqiangbiao 已提交
829
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
830 831 832

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

P
panqiangbiao 已提交
833
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
834

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

P
panqiangbiao 已提交
837 838
**参数:**

P
panqiangbiao 已提交
839 840 841
| 参数名 | 类型           | 必填 | 说明       |
| ------ | -------------- | ---- | ---------- |
| size   | [Size](#size8) | 否   | 缩略图尺寸 |
P
panqiangbiao 已提交
842 843 844 845 846

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
847
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
848 849 850 851

**示例:**

```
P
panqiangbiao 已提交
852
async function example() {
P
panqiangbiao 已提交
853 854 855 856
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
857 858
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
859 860 861 862
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
P
panqiangbiao 已提交
863
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
864 865
    });
}
P
panqiangbiao 已提交
866 867
```

P
panqiangbiao 已提交
868
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
869

P
panqiangbiao 已提交
870
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
871 872 873

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

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

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

P
panqiangbiao 已提交
878 879 880 881
**参数:**

| 参数名     | 类型                | 必填 | 说明           |
| ---------- | ------------------- | ---- | -------------- |
P
panqiangbiao 已提交
882
| isFavorite | boolean             | 是   | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
883
| callback   | AsyncCallback&lt;void&gt; | 是   | 回调返回空    |
P
panqiangbiao 已提交
884 885 886 887

**示例:**

```
P
panqiangbiao 已提交
888
async function example() {
P
panqiangbiao 已提交
889 890 891 892
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
893 894
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
895 896 897 898 899 900 901
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
902 903
```

P
panqiangbiao 已提交
904
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
905

P
panqiangbiao 已提交
906
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
907 908 909

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

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

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

P
panqiangbiao 已提交
914 915
**参数:**

P
panqiangbiao 已提交
916 917 918
| 参数名     | 类型    | 必填 | 说明                                                       |
| ---------- | ------- | ---- | ---------------------------------------------------------- |
| isFavorite | boolean | 是   | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
919 920 921 922 923

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
924
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
925 926 927 928

**示例:**

```
P
panqiangbiao 已提交
929
async function example() {
P
panqiangbiao 已提交
930 931 932 933
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
934 935
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
936 937 938 939 940 941 942 943 944
    };
    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 已提交
945 946
```

P
panqiangbiao 已提交
947
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
948

P
panqiangbiao 已提交
949
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
950 951 952

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

P
panqiangbiao 已提交
953
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
954

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

P
panqiangbiao 已提交
957 958 959 960 961 962 963 964 965
**参数:**

| 参数名   | 类型                   | 必填 | 说明                   |
| -------- | ---------------------- | ---- | ---------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调表示是否为收藏文件 |

**示例:**

```
P
panqiangbiao 已提交
966
async function example() {
P
panqiangbiao 已提交
967 968 969 970
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
971 972
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
973 974 975 976 977 978 979 980 981 982 983
    };
    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 已提交
984 985
```

P
panqiangbiao 已提交
986
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
987

P
panqiangbiao 已提交
988
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
989 990 991

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

P
panqiangbiao 已提交
992
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
993

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

P
panqiangbiao 已提交
996 997 998 999
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1000
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1001 1002 1003 1004

**示例:**

```
P
panqiangbiao 已提交
1005
async function example() {
P
panqiangbiao 已提交
1006 1007 1008 1009
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1010 1011
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020
    };
    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 已提交
1021 1022
```

P
panqiangbiao 已提交
1023
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1024

P
panqiangbiao 已提交
1025
trash(isTrash: boolean, callback: AsyncCallback&lt;void&g;): void
P
panqiangbiao 已提交
1026 1027 1028

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

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

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

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

P
panqiangbiao 已提交
1035 1036 1037 1038
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
1039
| isTrash  | boolean                | 是   | 是否设置为垃圾文件    |
P
panqiangbiao 已提交
1040 1041 1042 1043 1044
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |

**示例:**

```
P
panqiangbiao 已提交
1045
async function example() {
P
panqiangbiao 已提交
1046 1047 1048 1049
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1050 1051
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1052 1053 1054 1055 1056
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1057
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1058
    }
P
panqiangbiao 已提交
1059
}
P
panqiangbiao 已提交
1060 1061
```

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

P
panqiangbiao 已提交
1064
trash(isTrash: boolean,): Promise&lt;void&gt;
P
panqiangbiao 已提交
1065 1066 1067

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

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

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

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

P
panqiangbiao 已提交
1074 1075
**参数:**

P
panqiangbiao 已提交
1076 1077 1078
| 参数名  | 类型    | 必填 | 说明               |
| ------- | ------- | ---- | ------------------ |
| isTrash | boolean | 是   | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1079 1080 1081 1082 1083

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1084
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1085 1086 1087 1088

**示例:**

```
P
panqiangbiao 已提交
1089
async function example() {
P
panqiangbiao 已提交
1090 1091 1092 1093
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1094 1095
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104
    };
    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 已提交
1105 1106
```

P
panqiangbiao 已提交
1107
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1108

P
panqiangbiao 已提交
1109
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1110 1111 1112

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

P
panqiangbiao 已提交
1113
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1114

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

P
panqiangbiao 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是   | 回调返回表示文件是否为垃圾文件 |

**示例:**

```
P
panqiangbiao 已提交
1126
async function example() {
P
panqiangbiao 已提交
1127 1128 1129 1130
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1131 1132
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1133 1134 1135 1136 1137 1138
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash(isTrashCallBack);
    function isTrashCallBack(err, isTrash) {
            if (isTrash == true) {
P
panqiangbiao 已提交
1139
                console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK isTrash = ' + isTrash);
P
panqiangbiao 已提交
1140 1141 1142
                asset.trash(true, trashCallBack);

            } else {
P
panqiangbiao 已提交
1143 1144
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');
P
panqiangbiao 已提交
1145 1146

            }
P
panqiangbiao 已提交
1147
    }
P
panqiangbiao 已提交
1148
}
P
panqiangbiao 已提交
1149 1150
```

P
panqiangbiao 已提交
1151
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1152

P
panqiangbiao 已提交
1153
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1154

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

P
panqiangbiao 已提交
1157
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1158

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

P
panqiangbiao 已提交
1161 1162 1163 1164
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1165
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1166 1167 1168 1169

**示例:**

```
P
panqiangbiao 已提交
1170
async function example() {
P
panqiangbiao 已提交
1171 1172 1173 1174
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1175 1176
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1177 1178 1179 1180 1181 1182 1183 1184 1185
    };
    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 已提交
1186 1187
```

P
panqiangbiao 已提交
1188
## FetchFileResult<sup>8+</sup>
P
panqiangbiao 已提交
1189 1190 1191

文件检索结果集。

P
panqiangbiao 已提交
1192
### getCount<sup>8+</sup>
P
panqiangbiao 已提交
1193

P
panqiangbiao 已提交
1194
getCount(): number
P
panqiangbiao 已提交
1195 1196 1197

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

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

P
panqiangbiao 已提交
1200 1201
**返回值**

P
panqiangbiao 已提交
1202 1203 1204
| 类型   | 说明             |
| ------ | ---------------- |
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1205 1206 1207 1208

**示例**

```
P
panqiangbiao 已提交
1209
async function example() {
P
panqiangbiao 已提交
1210 1211 1212
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1213 1214
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1215 1216 1217 1218
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1219 1220
```

P
panqiangbiao 已提交
1221
### isAfterLast<sup>8+</sup>
P
panqiangbiao 已提交
1222

P
panqiangbiao 已提交
1223
isAfterLast(): boolean
P
panqiangbiao 已提交
1224 1225 1226

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

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

P
panqiangbiao 已提交
1229 1230
**返回值**

P
panqiangbiao 已提交
1231 1232 1233
| 类型    | 说明                                                        |
| ------- | ----------------------------------------------------------- |
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1234 1235 1236 1237

**示例**

```
P
panqiangbiao 已提交
1238
async function example() {
P
panqiangbiao 已提交
1239 1240 1241 1242
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1243 1244
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1245 1246 1247
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1248
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1249 1250 1251 1252
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1253
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1254
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1255 1256
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1257
              fetchFileResult.close();
P
panqiangbiao 已提交
1258

P
panqiangbiao 已提交
1259 1260
            }
    }
P
panqiangbiao 已提交
1261
}
P
panqiangbiao 已提交
1262 1263
```

P
panqiangbiao 已提交
1264
### close<sup>8+</sup>
P
panqiangbiao 已提交
1265

P
panqiangbiao 已提交
1266
close(): void
P
panqiangbiao 已提交
1267 1268 1269

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

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

P
panqiangbiao 已提交
1272 1273 1274
**示例**

```
P
panqiangbiao 已提交
1275
async function example() {
P
panqiangbiao 已提交
1276 1277 1278 1279
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1280 1281
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1282 1283 1284 1285
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1286 1287
```

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

P
panqiangbiao 已提交
1290
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1291 1292 1293

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

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

P
panqiangbiao 已提交
1296 1297
**参数**

P
panqiangbiao 已提交
1298 1299 1300
| 参数名   | 类型                                          | 必填 | 说明                                        |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是   | 异步获取结果集中第一个FileAsset完成后的回调 |
P
panqiangbiao 已提交
1301 1302 1303 1304

**示例**

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

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

P
panqiangbiao 已提交
1326
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1327 1328 1329

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

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

P
panqiangbiao 已提交
1332 1333 1334 1335
**返回值**

| 类型                | 说明                                   |
| ------------------ | -------------------------------------- |
P
panqiangbiao 已提交
1336
| Promise&lt;[FileAsset](#fileasset8)&gt; | Promise方式返回FileAsset               |
P
panqiangbiao 已提交
1337 1338 1339 1340

**示例**

```
P
panqiangbiao 已提交
1341
async function example() {
P
panqiangbiao 已提交
1342 1343 1344 1345
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1346 1347
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1348 1349 1350 1351 1352 1353 1354 1355
    };
    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 已提交
1356 1357
```

P
panqiangbiao 已提交
1358
### getNextObject<sup>8+</sup>
P
panqiangbiao 已提交
1359

P
panqiangbiao 已提交
1360
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1361 1362 1363

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

P
panqiangbiao 已提交
1364
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1365

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

P
panqiangbiao 已提交
1368 1369
**参数**

P
panqiangbiao 已提交
1370 1371 1372
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1373 1374 1375 1376

**示例**

```
P
panqiangbiao 已提交
1377
async function example() {
P
panqiangbiao 已提交
1378 1379 1380 1381
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1382 1383
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getNextObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1394 1395
```

P
panqiangbiao 已提交
1396
### getNextObject<sup>8+</sup>
P
panqiangbiao 已提交
1397

P
panqiangbiao 已提交
1398
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1399 1400 1401

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

P
panqiangbiao 已提交
1402
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1403

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

P
panqiangbiao 已提交
1406 1407 1408 1409
**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1410
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1411 1412 1413 1414

**示例**

```
P
panqiangbiao 已提交
1415
async function example() {
P
panqiangbiao 已提交
1416 1417 1418 1419
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1420 1421
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1422 1423 1424
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1425
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1426 1427
    fileAsset = await fetchFileResult.getNextObject();
}
P
panqiangbiao 已提交
1428 1429
```

P
panqiangbiao 已提交
1430
### getLastObject<sup>8+</sup>
P
panqiangbiao 已提交
1431

P
panqiangbiao 已提交
1432
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1433 1434 1435

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

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

P
panqiangbiao 已提交
1438 1439
**参数**

P
panqiangbiao 已提交
1440 1441 1442
| 参数     | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1443 1444 1445 1446

**示例**

```
P
panqiangbiao 已提交
1447
async function example() {
P
panqiangbiao 已提交
1448 1449 1450 1451
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1452 1453
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getLastObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1464 1465
```

P
panqiangbiao 已提交
1466
### getLastObject<sup>8+</sup>
P
panqiangbiao 已提交
1467

P
panqiangbiao 已提交
1468
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1469 1470 1471

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

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

P
panqiangbiao 已提交
1474 1475 1476 1477
**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1478
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1479 1480 1481 1482

**示例**

```
P
panqiangbiao 已提交
1483
async function example() {
P
panqiangbiao 已提交
1484 1485 1486 1487
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1488 1489
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1490 1491 1492 1493
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
1494 1495
```

P
panqiangbiao 已提交
1496
### getPositionObject<sup>8+</sup>
P
panqiangbiao 已提交
1497

P
panqiangbiao 已提交
1498
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1499 1500 1501

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

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

P
panqiangbiao 已提交
1504 1505
**参数**

P
panqiangbiao 已提交
1506 1507 1508 1509
| 参数     | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
| index    | number                                        | 是   | 要获取的文件的索引,从0开始 |
| callback | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1510 1511 1512 1513

**示例**

```
P
panqiangbiao 已提交
1514
async function example() {
P
panqiangbiao 已提交
1515 1516 1517 1518
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1519 1520
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1521 1522
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1523
    fetchFileResult.getPositionObject(0, (err, value) => {
P
panqiangbiao 已提交
1524 1525 1526 1527 1528 1529 1530
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1531 1532
```

P
panqiangbiao 已提交
1533
### getPositionObject<sup>8+</sup>
P
panqiangbiao 已提交
1534

P
panqiangbiao 已提交
1535
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1536 1537 1538

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

P
panqiangbiao 已提交
1539
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1540

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

P
panqiangbiao 已提交
1543 1544 1545 1546
**参数**

| 参数  | 类型   | 必填 | 说明                 |
| ----- | ------ | ---- | -------------------- |
P
panqiangbiao 已提交
1547
| index | number | 是   | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
1548 1549 1550 1551 1552

**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1553
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1554 1555 1556 1557

**示例**

```
P
panqiangbiao 已提交
1558
async function example() {
P
panqiangbiao 已提交
1559 1560 1561 1562
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1563 1564
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1565 1566
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1567
    fetchFileResult.getPositionObject(1, (err, value) => {
P
panqiangbiao 已提交
1568 1569 1570 1571 1572 1573 1574
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1575 1576
```

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

P
panqiangbiao 已提交
1579
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
1580 1581 1582

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

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

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

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

P
panqiangbiao 已提交
1589 1590 1591
| 参数     | 类型                                           | 必填 | 说明                            |
| -------- | ---------------------------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback<Array<[FileAsset](#fileasset8)>> | 是   | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
1592 1593 1594 1595

**示例**

```
P
panqiangbiao 已提交
1596
async function example() {
P
panqiangbiao 已提交
1597 1598 1599 1600
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1601 1602
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getAllObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1613 1614
```

P
panqiangbiao 已提交
1615
### getAllObject<sup>8+</sup>
P
panqiangbiao 已提交
1616

P
panqiangbiao 已提交
1617
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
1618 1619 1620

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

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

P
panqiangbiao 已提交
1623 1624
**返回值**

P
panqiangbiao 已提交
1625 1626 1627
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset8)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
1628 1629 1630 1631

**示例**

```
P
panqiangbiao 已提交
1632
async function example() {
P
panqiangbiao 已提交
1633 1634 1635 1636
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1637 1638
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1639 1640 1641 1642
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
P
panqiangbiao 已提交
1643 1644
```

P
panqiangbiao 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
## Album<sup>8+</sup>

实体相册

### **属性**

| 名称         | 参数型 | 可读 | 可写 | 说明           |
| ------------ | ------ | ---- | ---- | -------------- |
| albumId      | number | 是   | 否   | 相册编号       |
| albumName    | string | 是   | 是   | 相册名称       |
| albumUri     | string | 是   | 否   | 相册Uri        |
| dateModified | number | 是   | 否   | 修改日期       |
| count        | number | 是   | 否   | 相册中文件数量 |
| relativePath | string | 是   | 否   | 相对路径       |
| coverUri     | string | 是   | 否   | 封面文件Uri    |
P
panqiangbiao 已提交
1660

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

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1664 1665 1666

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

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

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

P
panqiangbiao 已提交
1671 1672 1673 1674
**参数**

| 参数     | 类型                | 必填 | 说明                          |
| -------- | ------------------- | ---- | ----------------------------- |
P
panqiangbiao 已提交
1675
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空                  |
P
panqiangbiao 已提交
1676 1677 1678 1679

**示例**

```
P
panqiangbiao 已提交
1680
async function example() {
P
panqiangbiao 已提交
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
    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 已提交
1696 1697
```

P
panqiangbiao 已提交
1698
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1699

P
panqiangbiao 已提交
1700
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1701 1702 1703

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

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

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

P
panqiangbiao 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716
**返回值**

| 类型          | 说明                                                 |
| ------------- | ---------------------------------------------------- |
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

```
P
panqiangbiao 已提交
1717
async function example() {
P
panqiangbiao 已提交
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
    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 已提交
1731 1732
```

P
panqiangbiao 已提交
1733
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
1734

P
panqiangbiao 已提交
1735
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
1736 1737 1738

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

P
panqiangbiao 已提交
1739
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1740

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

P
panqiangbiao 已提交
1743 1744
**参数**

P
panqiangbiao 已提交
1745 1746 1747 1748
| 参数     | 类型                                         | 必填 | 说明                              |
| -------- | -------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions8)     | 是   | 媒体检索选项                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult8)> | 是   | 异步返回FetchFileResult之后的回调 |
P
panqiangbiao 已提交
1749 1750 1751 1752

**示例**

```
P
panqiangbiao 已提交
1753
async function example() {
P
panqiangbiao 已提交
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
    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 已提交
1764 1765 1766
}
```

P
panqiangbiao 已提交
1767
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
1768

P
panqiangbiao 已提交
1769
 getFileAssets(options?: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
1770 1771 1772

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

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

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

**参数**

P
panqiangbiao 已提交
1779 1780 1781
| 参数    | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions8) | 否   | 媒体检索选项 |
P
panqiangbiao 已提交
1782 1783 1784

**返回值**

P
panqiangbiao 已提交
1785 1786 1787
| 类型                                          | 说明                    |
| --------------------------------------------- | ----------------------- |
| Promise<[FetchFileResult](#fetchfileresult8)> | 返回FetchFileResult对象 |
P
panqiangbiao 已提交
1788 1789 1790 1791

**示例**

```
P
panqiangbiao 已提交
1792
async function example() {
P
panqiangbiao 已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804
    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 已提交
1805 1806
```

P
panqiangbiao 已提交
1807
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
1808

P
panqiangbiao 已提交
1809
注册设备的信息。
P
panqiangbiao 已提交
1810

P
panqiangbiao 已提交
1811 1812 1813 1814 1815 1816
| 名称       | 类型       | 可读 | 可写 | 说明             |
| ---------- | ---------- | ---- | ---- | ---------------- |
| deviceName | string     | 是   | 否   | 注册设备的名称   |
| networkId  | string     | 是   | 否   | 注册设备的网络ID |
| deviceType | DeviceType | 是   | 否   | 设备类型         |
| isOnline   | boolean    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
1817 1818 1819 1820 1821 1822 1823 1824



MediaType
---------

枚举,媒体类型。

P
panqiangbiao 已提交
1825 1826 1827 1828 1829 1830
| 名称  | 默认值 | 描述 |
| ----- | ------ | ---- |
| FILE  | 0      | 文件 |
| IMAGE | 1      | 图片 |
| VIDEO | 2      | 视频 |
| AUDIO | 3      | 音频 |
P
panqiangbiao 已提交
1831 1832 1833 1834 1835 1836

FileKey
-------

枚举,文件关键信息。

P
panqiangbiao 已提交
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
| 名称          | 默认值              | 描述                                                       |
| ------------- | ------------------- | ---------------------------------------------------------- |
| 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              | 图片高度(单位:像素)                                     |
| ORIENTATION   | orientation         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
| ALBUM_ID      | bucket_id           | 文件所归属的相册编号                                       |
| ALBUM_NAME    | bucket_display_name | 文件所归属相册名称                                         |
P
panqiangbiao 已提交
1858 1859 1860 1861 1862 1863

DirectoryType
-------------

枚举,目录类型。

P
panqiangbiao 已提交
1864 1865 1866 1867 1868 1869 1870 1871
| 名称          | 默认值 | 描述               |
| ------------- | ------ | ------------------ |
| DIR_CAMERA    | 0      | 表示Camera文件路径 |
| DIR_VIDEO     | 1      | 表示视频路径       |
| DIR_IMAGE     | 2      | 表示图片路径       |
| DIR_AUDIO     | 3      | 表示音频路径       |
| DIR_DOCUMENTS | 4      | 表示文档路径       |
| DIR_DOWNLOAD  | 5      | 表示下载路径       |
P
panqiangbiao 已提交
1872

P
panqiangbiao 已提交
1873
DeviceType
P
panqiangbiao 已提交
1874 1875 1876 1877
-----------

枚举,设备类型。

P
panqiangbiao 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886
| 名称         | 默认值 | 描述       |
| ------------ | ------ | ---------- |
| TYPE_UNKNOWN | 0      | 未识别设备 |
| TYPE_LAPTOP  | 1      | 笔记本电脑 |
| TYPE_PHONE   | 2      | 手机       |
| TYPE_TABLET  | 3      | 平板电脑   |
| TYPE_WATCH   | 4      | 智能手表   |
| TYPE_CAR     | 5      | 车载设备   |
| TYPE_TV      | 6      | 电视设备   |
P
panqiangbiao 已提交
1887

P
panqiangbiao 已提交
1888
## MediaFetchOptions<sup>8+</sup>
P
panqiangbiao 已提交
1889 1890 1891

检索条件。

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

P
panqiangbiao 已提交
1901
## Size<sup>8+</sup>
P
panqiangbiao 已提交
1902 1903 1904

图片尺寸。

P
panqiangbiao 已提交
1905 1906 1907 1908
| 名称   | 类型   | 可读 | 可写 | 说明             |
| ------ | ------ | ---- | ---- | ---------------- |
| width  | number | 是   | 是   | 宽(单位:像素) |
| height | number | 是   | 是   | 高(单位:像素) |
P
panqiangbiao 已提交
1909