js-apis-medialibrary.md 65.1 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
function 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 59
| 参数名   | 类型                                         | 必填 | 说明                              |
| -------- | -------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions8)     | 是   | 媒体获取选项                      |
| 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
// 使用Callback方式创建Image类型文件
let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
188 189 190
let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
const path = await media.getPublicDirectory(DIR_IMAGE);
mediaLibrary.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
P
panqiangbiao 已提交
191 192
    if (fileAsset != undefined) {
        console.info('createAsset successfully, message = ' + err);
P
panqiangbiao 已提交
193
    } else {
P
panqiangbiao 已提交
194
        console.info('createAsset failed, message = ' + err);
P
panqiangbiao 已提交
195 196
    }
});
P
panqiangbiao 已提交
197 198
```

P
panqiangbiao 已提交
199
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
200

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

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

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

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

P
panqiangbiao 已提交
209 210
**参数:**

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

**返回值**

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

**示例:**

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

P
panqiangbiao 已提交
237
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
238

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

P
panqiangbiao 已提交
241
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
242 243 244 245 246 247 248

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

**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
265
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
266

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

P
panqiangbiao 已提交
269
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
270 271 272 273 274 275 276

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

**参数:**

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

**返回值:**

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

**示例:**

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

P
panqiangbiao 已提交
299
### getAlbums<sup>8+</sup>
P
panqiangbiao 已提交
300

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

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

P
panqiangbiao 已提交
305
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
306

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

P
panqiangbiao 已提交
309 310
**参数**

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

**示例:**

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

P
panqiangbiao 已提交
334
### getAlbums<sup>8+</sup>
P
panqiangbiao 已提交
335

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

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

P
panqiangbiao 已提交
340
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
341

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

P
panqiangbiao 已提交
344 345
**参数:**

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

**返回值:**

P
panqiangbiao 已提交
352 353 354
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album8)>> | 返回Album列表 |
P
panqiangbiao 已提交
355 356 357 358

**示例:**

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

P
panqiangbiao 已提交
370
### release<sup>8+</sup>
P
panqiangbiao 已提交
371

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

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

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

P
panqiangbiao 已提交
379 380 381 382
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
388
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
389
media.release((err) => {
P
panqiangbiao 已提交
390
    // do something
P
panqiangbiao 已提交
391
});
P
panqiangbiao 已提交
392 393
```

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

P
panqiangbiao 已提交
396
release(): Promise&lt;void&gt;
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 405 406
**返回值:**

| 类型          | 说明                                |
| ------------- | ----------------------------------- |
P
panqiangbiao 已提交
407
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
408 409 410 411

**示例:**

```
P
panqiangbiao 已提交
412 413
var media = mediaLibrary.getMediaLibrary(context);
media.release()
P
panqiangbiao 已提交
414 415
```

P
panqiangbiao 已提交
416 417 418 419 420 421
## FileAsset<sup>8+</sup>

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

### **属性**

P
panqiangbiao 已提交
422 423 424 425 426
| 名称         | 类型                    | 可读 | 可写 | 说明                                                   |
| ------------ | ----------------------- | ---- | ---- | ------------------------------------------------------ |
| id           | number                  | 是   | 否   | 文件资源编号                                           |
| uri          | string                  | 是   | 否   | 文件资源uri(如:dataability:///media/image/2)         |
| mimeType     | string                  | 是   | 否   | 文件扩展属性                                           |
P
panqiangbiao 已提交
427
| mediaType    | [MediaType](#mediatype) | 是   | 否   | 媒体类型                                               |
P
panqiangbiao 已提交
428
| displayName  | string                  | 是   | 是   | 显示文件名,包含后缀名                                 |
P
panqiangbiao 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
| 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 已提交
445 446 447


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

P
panqiangbiao 已提交
449
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
450 451 452

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

P
panqiangbiao 已提交
453
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
454

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

P
panqiangbiao 已提交
457 458 459 460 461 462 463 464 465
**参数:**

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

**示例:**

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

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

P
panqiangbiao 已提交
484
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
485 486 487

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

P
panqiangbiao 已提交
488
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
489

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

P
panqiangbiao 已提交
492 493 494 495
**返回值:**

| 类型             | 说明                                       |
| ---------------- | ------------------------------------------ |
P
panqiangbiao 已提交
496
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
497 498 499 500

**示例:**

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

P
panqiangbiao 已提交
519
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
520

P
panqiangbiao 已提交
521
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
522 523 524

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

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

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

P
panqiangbiao 已提交
529 530 531 532 533 534 535 536 537
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
555
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
556

P
panqiangbiao 已提交
557
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
558 559 560

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

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

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

P
panqiangbiao 已提交
565 566 567 568
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
569
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
570 571 572 573

**示例:**

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

P
panqiangbiao 已提交
589
### open<sup>8+</sup>
P
panqiangbiao 已提交
590

P
panqiangbiao 已提交
591
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
592 593 594

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

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

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

P
panqiangbiao 已提交
599 600 601 602
**参数**

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

**示例:**

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

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

P
panqiangbiao 已提交
626
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
627 628 629

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

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

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

P
panqiangbiao 已提交
634 635
**参数:**

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

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
644
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
645 646 647 648

**示例:**

```
P
panqiangbiao 已提交
649
async function example() {
P
panqiangbiao 已提交
650
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
651 652
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
P
panqiangbiao 已提交
653 654 655 656 657 658 659 660 661
    asset = await media.createAsset(mediaType, "image00003.jpg", path);
    asset.open('rw').then((openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
P
panqiangbiao 已提交
662 663
```

P
panqiangbiao 已提交
664
### close<sup>8+</sup>
P
panqiangbiao 已提交
665

P
panqiangbiao 已提交
666
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
667 668 669

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

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

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

P
panqiangbiao 已提交
674 675 676 677
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
705
### close<sup>8+</sup>
P
panqiangbiao 已提交
706

P
panqiangbiao 已提交
707
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
708 709 710

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

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

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

P
panqiangbiao 已提交
715 716 717 718 719 720 721 722 723 724
**参数:**

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

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
725
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
726 727 728 729

**示例:**

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

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

P
panqiangbiao 已提交
752
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
753

P
panqiangbiao 已提交
754
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
755 756 757

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

P
panqiangbiao 已提交
758
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
759

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

P
panqiangbiao 已提交
762 763 764 765 766 767 768 769 770
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
787
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
788

P
panqiangbiao 已提交
789
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
790 791 792

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

P
panqiangbiao 已提交
793
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
794

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

P
panqiangbiao 已提交
797 798 799 800
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
823
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
824

P
panqiangbiao 已提交
825
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
826 827 828

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

P
panqiangbiao 已提交
829
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
830

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

P
panqiangbiao 已提交
833 834
**参数:**

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

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
843
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
844 845 846 847

**示例:**

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

P
panqiangbiao 已提交
864
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
865

P
panqiangbiao 已提交
866
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
867 868 869

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

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

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

P
panqiangbiao 已提交
874 875 876 877
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
900
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
901

P
panqiangbiao 已提交
902
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
903 904 905

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

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

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

P
panqiangbiao 已提交
910 911
**参数:**

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

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
920
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
921 922 923 924

**示例:**

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

P
panqiangbiao 已提交
943
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
944

P
panqiangbiao 已提交
945
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
946 947 948

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

P
panqiangbiao 已提交
949
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
950

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

P
panqiangbiao 已提交
953 954 955 956 957 958 959 960 961
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
982
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
983

P
panqiangbiao 已提交
984
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
985 986 987

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

P
panqiangbiao 已提交
988
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
989

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

P
panqiangbiao 已提交
992 993 994 995
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
996
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
997 998 999 1000

**示例:**

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

P
panqiangbiao 已提交
1019
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1020

P
panqiangbiao 已提交
1021
trash(isTrash: boolean, callback: AsyncCallback&lt;void&g;): void
P
panqiangbiao 已提交
1022 1023 1024

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

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

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

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

P
panqiangbiao 已提交
1031 1032 1033 1034
**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
1058
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1059

P
panqiangbiao 已提交
1060
trash(isTrash: boolean,): Promise&lt;void&gt;
P
panqiangbiao 已提交
1061 1062 1063

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

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

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

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

P
panqiangbiao 已提交
1070 1071
**参数:**

P
panqiangbiao 已提交
1072 1073 1074
| 参数名  | 类型    | 必填 | 说明               |
| ------- | ------- | ---- | ------------------ |
| isTrash | boolean | 是   | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1075 1076 1077 1078 1079

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1080
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1081 1082 1083 1084

**示例:**

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

P
panqiangbiao 已提交
1103
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1104

P
panqiangbiao 已提交
1105
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1106 1107 1108

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

P
panqiangbiao 已提交
1109
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1110

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

P
panqiangbiao 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121
**参数:**

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

**示例:**

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

            } else {
P
panqiangbiao 已提交
1139 1140
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');
P
panqiangbiao 已提交
1141 1142

            }
P
panqiangbiao 已提交
1143
    }
P
panqiangbiao 已提交
1144
}
P
panqiangbiao 已提交
1145 1146
```

P
panqiangbiao 已提交
1147
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1148

P
panqiangbiao 已提交
1149
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1150

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

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

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

P
panqiangbiao 已提交
1157 1158 1159 1160
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1161
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1162 1163 1164 1165

**示例:**

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

P
panqiangbiao 已提交
1184
## FetchFileResult<sup>8+</sup>
P
panqiangbiao 已提交
1185 1186 1187

文件检索结果集。

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

P
panqiangbiao 已提交
1190
getCount(): number
P
panqiangbiao 已提交
1191 1192 1193

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

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

P
panqiangbiao 已提交
1196 1197
**返回值**

P
panqiangbiao 已提交
1198 1199 1200
| 类型   | 说明             |
| ------ | ---------------- |
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1201 1202 1203 1204

**示例**

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

P
panqiangbiao 已提交
1217
### isAfterLast<sup>8+</sup>
P
panqiangbiao 已提交
1218

P
panqiangbiao 已提交
1219
isAfterLast(): boolean
P
panqiangbiao 已提交
1220 1221 1222

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

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

P
panqiangbiao 已提交
1225 1226
**返回值**

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

**示例**

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

P
panqiangbiao 已提交
1255 1256
            }
    }
P
panqiangbiao 已提交
1257
}
P
panqiangbiao 已提交
1258 1259
```

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

P
panqiangbiao 已提交
1262
close(): void
P
panqiangbiao 已提交
1263 1264 1265

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

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

P
panqiangbiao 已提交
1268 1269 1270
**示例**

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

P
panqiangbiao 已提交
1284
### getFirstObject<sup>8+</sup>
P
panqiangbiao 已提交
1285

P
panqiangbiao 已提交
1286
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1287 1288 1289

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

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

P
panqiangbiao 已提交
1292 1293
**参数**

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

**示例**

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

P
panqiangbiao 已提交
1320
### getFirstObject<sup>8+</sup>
P
panqiangbiao 已提交
1321

P
panqiangbiao 已提交
1322
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1323 1324 1325

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

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

P
panqiangbiao 已提交
1328 1329 1330 1331
**返回值**

| 类型                | 说明                                   |
| ------------------ | -------------------------------------- |
P
panqiangbiao 已提交
1332
| Promise&lt;[FileAsset](#fileasset8)&gt; | Promise方式返回FileAsset               |
P
panqiangbiao 已提交
1333 1334 1335 1336

**示例**

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

P
panqiangbiao 已提交
1354
### getNextObject<sup>8+</sup>
P
panqiangbiao 已提交
1355

P
panqiangbiao 已提交
1356
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1357 1358 1359

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

P
panqiangbiao 已提交
1360
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1361

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

P
panqiangbiao 已提交
1364 1365
**参数**

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

**示例**

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

P
panqiangbiao 已提交
1392
### getNextObject<sup>8+</sup>
P
panqiangbiao 已提交
1393

P
panqiangbiao 已提交
1394
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1395 1396 1397

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

P
panqiangbiao 已提交
1398
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1399

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

P
panqiangbiao 已提交
1402 1403 1404 1405
**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1406
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1407 1408 1409 1410

**示例**

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

P
panqiangbiao 已提交
1426
### getLastObject<sup>8+</sup>
P
panqiangbiao 已提交
1427

P
panqiangbiao 已提交
1428
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1429 1430 1431

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

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

P
panqiangbiao 已提交
1434 1435
**参数**

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

**示例**

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

P
panqiangbiao 已提交
1462
### getLastObject<sup>8+</sup>
P
panqiangbiao 已提交
1463

P
panqiangbiao 已提交
1464
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1465 1466 1467

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

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

P
panqiangbiao 已提交
1470 1471 1472 1473
**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1474
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1475 1476 1477 1478

**示例**

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

P
panqiangbiao 已提交
1492
### getPositionObject<sup>8+</sup>
P
panqiangbiao 已提交
1493

P
panqiangbiao 已提交
1494
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1495 1496 1497

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

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

P
panqiangbiao 已提交
1500 1501
**参数**

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

**示例**

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

P
panqiangbiao 已提交
1529
### getPositionObject<sup>8+</sup>
P
panqiangbiao 已提交
1530

P
panqiangbiao 已提交
1531
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1532 1533 1534

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

P
panqiangbiao 已提交
1535
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1536

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

P
panqiangbiao 已提交
1539 1540 1541 1542
**参数**

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

**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1549
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1550 1551 1552 1553

**示例**

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

P
panqiangbiao 已提交
1573
### getAllObject<sup>8+</sup>
P
panqiangbiao 已提交
1574

P
panqiangbiao 已提交
1575
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
1576 1577 1578

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

P
panqiangbiao 已提交
1579
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1580

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

P
panqiangbiao 已提交
1583 1584
**参数**

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

**示例**

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

P
panqiangbiao 已提交
1611
### getAllObject<sup>8+</sup>
P
panqiangbiao 已提交
1612

P
panqiangbiao 已提交
1613
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
1614 1615 1616

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

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

P
panqiangbiao 已提交
1619 1620
**返回值**

P
panqiangbiao 已提交
1621 1622 1623
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset8)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
1624 1625 1626 1627

**示例**

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

P
panqiangbiao 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
## Album<sup>8+</sup>

实体相册

### **属性**

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

P
panqiangbiao 已提交
1657 1658 1659
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1660 1661 1662

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

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

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

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

| 参数     | 类型                | 必填 | 说明                          |
| -------- | ------------------- | ---- | ----------------------------- |
P
panqiangbiao 已提交
1671
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空                  |
P
panqiangbiao 已提交
1672 1673 1674 1675

**示例**

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

P
panqiangbiao 已提交
1694
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1695

P
panqiangbiao 已提交
1696
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1697 1698 1699

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

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

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

P
panqiangbiao 已提交
1704 1705 1706 1707 1708 1709 1710 1711 1712
**返回值**

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

**示例**

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

P
panqiangbiao 已提交
1729
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
1730

P
panqiangbiao 已提交
1731
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
1732 1733 1734

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

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

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

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

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

**示例**

```
P
panqiangbiao 已提交
1749
async function example() {
P
panqiangbiao 已提交
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
    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 已提交
1760 1761 1762
}
```

P
panqiangbiao 已提交
1763
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
1764

P
panqiangbiao 已提交
1765
 getFileAssets(options?: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
1766 1767 1768

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

P
panqiangbiao 已提交
1769
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1770

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

**参数**

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

**返回值**

P
panqiangbiao 已提交
1781 1782 1783
| 类型                                          | 说明                    |
| --------------------------------------------- | ----------------------- |
| Promise<[FetchFileResult](#fetchfileresult8)> | 返回FetchFileResult对象 |
P
panqiangbiao 已提交
1784 1785 1786 1787

**示例**

```
P
panqiangbiao 已提交
1788
async function example() {
P
panqiangbiao 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
    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 已提交
1801 1802
```

P
panqiangbiao 已提交
1803
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
1804

P
panqiangbiao 已提交
1805
注册设备的信息。
P
panqiangbiao 已提交
1806

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



MediaType
---------

枚举,媒体类型。

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

FileKey
-------

枚举,文件关键信息。

P
panqiangbiao 已提交
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
| 名称          | 默认值              | 描述                                                       |
| ------------- | ------------------- | ---------------------------------------------------------- |
| 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 已提交
1854 1855 1856 1857 1858 1859

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

枚举,目录类型。

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

P
panqiangbiao 已提交
1869
DeviceType
P
panqiangbiao 已提交
1870 1871 1872 1873
-----------

枚举,设备类型。

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

P
panqiangbiao 已提交
1884
## MediaFetchOptions<sup>8+</sup>
P
panqiangbiao 已提交
1885 1886 1887

检索条件。

P
panqiangbiao 已提交
1888 1889
| 名称          | 类型          | 可读 | 可写 |  必填 |说明             |
| ------------- | ------------- | ---- | ---- |  ---- |---------------- |
P
panqiangbiao 已提交
1890 1891 1892
| 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 已提交
1893 1894
| uri           | string        | 是   | 是   | 否   |文件URI          |
| networkId     | string        | 是   | 是   | 否   |注册设备网络ID   |
P
panqiangbiao 已提交
1895
| extendArgs    | string        | 是   | 是   | 否   |扩展的检索参数,目前没有扩展检索参数   |
P
panqiangbiao 已提交
1896

P
panqiangbiao 已提交
1897
## Size<sup>8+</sup>
P
panqiangbiao 已提交
1898 1899 1900

图片尺寸。

P
panqiangbiao 已提交
1901 1902 1903 1904
| 名称   | 类型   | 可读 | 可写 | 说明             |
| ------ | ------ | ---- | ---- | ---------------- |
| width  | number | 是   | 是   | 宽(单位:像素) |
| height | number | 是   | 是   | 高(单位:像素) |
P
panqiangbiao 已提交
1905