js-apis-medialibrary.md 60.7 KB
Newer Older
P
panqiangbiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
媒体库管理
==========

 导入模块
---------

```
import medialibrary from '@ohos.multimedia.medialibrary';
```


## getMediaLibrary

function getMediaLibrary(context: Context): MediaLibrary;

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

P
panqiangbiao 已提交
18
**需要权限**:无
P
panqiangbiao 已提交
19

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

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

| 参数名  | 类型    | 必填 | 说明                 |
| ------- | ------- | ---- | -------------------- |
P
panqiangbiao 已提交
26
| context | Context | 是 | API 8接口此参数是必填参数,传入Ability实例的context,获取媒体库 |
P
panqiangbiao 已提交
27 28 29 30 31 32 33 34 35 36

**返回值:**

| 类型         | 说明   |
| ------------ | :----- |
| MediaLibrary | 媒体库实例 |

**示例:**

```
P
panqiangbiao 已提交
37
import featureAbility from '@ohos.ability.featureAbility';
P
panqiangbiao 已提交
38 39
import mediaLibrary from ‘ohos.multimedia.mediaLibrary';

P
panqiangbiao 已提交
40
var context = featureAbility.getContext()
P
panqiangbiao 已提交
41
var mediaLibrary = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
42 43 44 45 46
```

## medialibrary.getFileAssets


P
panqiangbiao 已提交
47
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): void;
P
panqiangbiao 已提交
48 49 50

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

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

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

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

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

**示例:**

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

getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;;

获取文件资源,使用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 95
| 参数名  | 类型                                    | 必填 | 说明         |
| ------- | --------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#MediaFetchOptions) | 是   | 媒体检索选项 |
P
panqiangbiao 已提交
96 97 98

**返回值**

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

**示例:**

```
P
panqiangbiao 已提交
106 107 108 109 110 111 112
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
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 119 120
```

## medialibrary.on

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
**需要权限**:无
P
panqiangbiao 已提交
126

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

P
panqiangbiao 已提交
129 130 131 132
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
139
medialibrary.on('imageChange', () => {
P
panqiangbiao 已提交
140
    // image file had changed, do something
P
panqiangbiao 已提交
141 142 143 144
})
```
## medialibrary.off

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

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

P
panqiangbiao 已提交
149 150 151
变更通知类型包括:注册设备变更,相册变更,图片文件变更,音频文件变更,视频文件变更,文件变更,远端文件变更。

**需要权限**:无
P
panqiangbiao 已提交
152

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

P
panqiangbiao 已提交
155 156 157 158
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
165
medialibrary.off('imageChange', () => {
P
panqiangbiao 已提交
166
    // stop listening success
P
panqiangbiao 已提交
167 168 169 170 171 172 173 174 175
})
```

## medialibrary.createAsset

createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void;

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

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

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

P
panqiangbiao 已提交
180 181
**参数:**

P
panqiangbiao 已提交
182 183 184 185
| 参数名       | 类型                                   | 必填 | 说明                                                         |
| ------------ | -------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#MediaType)                | 是   | 媒体类型                                                     |
| displayName  | string                                 | 是   | 展示文件名                                                   |
P
panqiangbiao 已提交
186
| relativePath | string                                 | 是   | 相对公共目录路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
187
| callback     | AsyncCallback<[FileAsset](#FileAsset)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
188 189 190 191

**示例:**

```
P
panqiangbiao 已提交
192 193 194
// 使用Callback方式创建Image类型文件
let mediaType = mediaLibrary.MediaType.IMAGE;
let path = "Pictures/";
P
panqiangbiao 已提交
195 196 197
medialibrary.createAsset(mediaType, “imageCallBack.jpg”, path, (err, fileAsset) => {
    if (fileAsset != undefined) {
        console.info('createAsset successfully, message = ' + err);
P
panqiangbiao 已提交
198
    } else {
P
panqiangbiao 已提交
199
        console.info('createAsset failed, message = ' + err);
P
panqiangbiao 已提交
200 201
    }
});
P
panqiangbiao 已提交
202 203 204 205 206 207 208 209
```

## medialibrary.createAsset

createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;;

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

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

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

P
panqiangbiao 已提交
214 215
**参数:**

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

**返回值**

| 类型                    | 说明              |
| ----------------------- | ----------------- |
| [FileAsset](#FileAsset) | 媒体数据FileAsset |

**示例:**

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

P
panqiangbiao 已提交
241 242
## medialibrary.getPublicDirectory

P
panqiangbiao 已提交
243
getPublicDirectory(type: DirectoryType, callback: AsyncCallback&lt;string&gt;): void;
P
panqiangbiao 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

获取公共目录路径

**需要权限**:无

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

**参数:**

| 参数名 | 类型                            | 必填 | 说明         |
| ------ | ------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#DirectoryType) | 是   | 公共目录类型 |
| callback   |AsyncCallback&lt;string&gt; | 是   | callback 返回公共目录路径 |


**示例:**

```
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
media.getPublicDirectory(DIR_CAMERA,(err, dicResult) => {
    if (dicResult == 'camera/') {
P
panqiangbiao 已提交
265
        console.info('MediaLibraryTest : getPublicDirectory');
P
panqiangbiao 已提交
266
    } else {
P
panqiangbiao 已提交
267
        console.info('MediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
268 269 270 271 272 273
    }
});
```

## medialibrary.getPublicDirectory

P
panqiangbiao 已提交
274
getPublicDirectory(type: DirectoryType): Promise&lt;string&gt;;
P
panqiangbiao 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

获取公共目录路径

**需要权限**:无

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

**参数:**

| 参数名 | 类型                            | 必填 | 说明         |
| ------ | ------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#DirectoryType) | 是   | 公共目录类型 |

**返回值:**

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

**示例:**

```
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
const dicResult = await media.getPublicDirectory(DIR_CAMERA);
if (dicResult == 'camera/') {
P
panqiangbiao 已提交
300
    console.info('MediaLibraryTest : getPublicDirectory');
P
panqiangbiao 已提交
301
} else {
P
panqiangbiao 已提交
302
    console.info('MediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
303 304 305
}
```

P
panqiangbiao 已提交
306 307 308 309 310 311
## medialibrary.getAlbums

getAlbums(options: MediaFetchOptions, callback: AsyncCallback<Array&lt;Album&gt;>): void;

获取实体相册,使用callback 方式返回结果。

P
panqiangbiao 已提交
312
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
313

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

P
panqiangbiao 已提交
316 317
**参数**

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

**示例:**

```
P
panqiangbiao 已提交
326 327 328 329
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
P
panqiangbiao 已提交
330
medialibrary.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
331 332 333 334 335 336 337
    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 已提交
338
})
P
panqiangbiao 已提交
339 340 341 342 343 344 345 346
```

## medialibrary.getAlbums

getAlbums(options: MediaFetchOptions): Promise<Array&lt;Album&gt;>;

获取实体相册,使用 promise 方式返回结果。

P
panqiangbiao 已提交
347
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
348

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

P
panqiangbiao 已提交
351 352
**参数:**

P
panqiangbiao 已提交
353 354 355
| 参数名  | 类型                                    | 必填 | 说明             |
| ------- | --------------------------------------- | ---- | ---------------- |
| options | [MediaFetchOptions](#MediaFetchOptions) | 是   | 媒体文件获取条件 |
P
panqiangbiao 已提交
356 357 358 359 360 361 362 363 364 365

**返回值:**

| 类型                            | 说明          |
| ------------------------------- | ------------- |
| Promise<Array<[Album](#Album)>> | 返回Album列表 |

**示例:**

```
P
panqiangbiao 已提交
366 367 368 369
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
P
panqiangbiao 已提交
370 371 372 373 374
medialibrary.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
    console.info("getAlbums failed with error:"+ err);
});
P
panqiangbiao 已提交
375 376 377 378 379 380
```

## medialibrary.release

release(callback: AsyncCallback&lt;void&gt;): void;

P
panqiangbiao 已提交
381
释放MediaLibrary实例,当用户确认后续不再使用MediaLibrary实例中的方法后调用release方法释放MediaLibrary实例。
P
panqiangbiao 已提交
382

P
panqiangbiao 已提交
383
**需要权限**:无
P
panqiangbiao 已提交
384

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

P
panqiangbiao 已提交
387 388 389 390
**参数:**

| 参数名   | 类型                   | 必填 | 说明                   |
| -------- | ------------------- | ---- | ---------------------- |
P
panqiangbiao 已提交
391
| callback | AsyncCallback&lt;void&gt; | 是   | 回调表示成功还是失败 |
P
panqiangbiao 已提交
392 393 394 395 396 397

**示例:**

```
medialibrary.release((err, data) => {
    // do something
P
panqiangbiao 已提交
398
});
P
panqiangbiao 已提交
399 400 401 402 403 404
```

## medialibrary.release

release(): Promise&lt;void&gt;;

P
panqiangbiao 已提交
405
释放MediaLibrary实例,当用户确认后续不再使用MediaLibrary实例中的方法后调用release方法释放MediaLibrary实例。
P
panqiangbiao 已提交
406

P
panqiangbiao 已提交
407
**需要权限**:无
P
panqiangbiao 已提交
408

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

P
panqiangbiao 已提交
411 412 413 414
**返回值:**

| 类型          | 说明                                |
| ------------- | ----------------------------------- |
P
panqiangbiao 已提交
415
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428

**示例:**

```
medialibrary.release()
```

## FileAsset.isDirectory

isDirectory(callback: AsyncCallback&lt;boolean&gt;): void;

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

P
panqiangbiao 已提交
429
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
430

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

P
panqiangbiao 已提交
433 434 435 436 437 438 439 440 441
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
442 443 444 445 446 447 448 449 450
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
451 452
asset.isDirectory((err, isDirectory) => {
    // do something
P
panqiangbiao 已提交
453
});
P
panqiangbiao 已提交
454 455 456 457 458 459 460 461
```

## FileAsset.isDirectory

isDirectory():Promise&lt;boolean&gt;;

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

P
panqiangbiao 已提交
462
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
463

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

P
panqiangbiao 已提交
466 467 468 469
**返回值:**

| 类型             | 说明                                       |
| ---------------- | ------------------------------------------ |
P
panqiangbiao 已提交
470
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
471 472 473 474

**示例:**

```
P
panqiangbiao 已提交
475 476 477 478 479 480 481 482 483
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
484 485 486 487 488
asset.isDirectory().then(function(isDirectory){
    console.info("isDirectory result:"+ isDirectory);
}).catch(function(err){
    console.info("isDirectory failed with error:"+ err);
});
P
panqiangbiao 已提交
489 490 491 492 493 494 495 496
```

## FileAsset.commitModify

commitModify(callback: AsyncCallback&lt;void&gt;): void;

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

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

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

P
panqiangbiao 已提交
501 502 503 504 505 506 507 508 509
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
510 511 512 513 514 515 516 517 518
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
519
asset.title = ‘newtitle';
P
panqiangbiao 已提交
520
asset.commitModify(() => {
P
panqiangbiao 已提交
521
    console.info('commitModify success');
P
panqiangbiao 已提交
522
}
P
panqiangbiao 已提交
523 524 525 526 527 528 529 530
```

## FileAsset.commitModify

commitModify(): Promise&lt;void&gt;;

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

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

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

P
panqiangbiao 已提交
535 536 537 538
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
539
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
540 541 542 543

**示例:**

```
P
panqiangbiao 已提交
544 545 546 547 548 549 550 551 552
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
553 554
asset.title = ‘newtitle';
asset.commitModify();
P
panqiangbiao 已提交
555 556 557 558 559 560 561 562
```

## FileAsset.open

open(mode: string, callback: AsyncCallback&lt;number&gt;): void;

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

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

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

P
panqiangbiao 已提交
567 568 569 570
**参数**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
571
| mode     | string                 | 是   | 打开文件方式,如:'r', 'w', 'rw' |
P
panqiangbiao 已提交
572 573 574 575 576
| callback | AsyncCallback&lt;number&gt; | 是   | 回调返回文件句柄 |

**示例:**

```
P
panqiangbiao 已提交
577 578 579 580
let mediaType = mediaLibrary.MediaType.IMAGE;
let path = "Pictures/";
asset = await media.createAsset(mediaType, "image00003.jpg", path);
asset.open('rw', (openError, fd) => {
P
panqiangbiao 已提交
581 582 583 584 585
    if(fd > 0){
        asset.close(fd);
    }else{
        console.info('File Open Failed!' + openError);
    }
P
panqiangbiao 已提交
586
});
P
panqiangbiao 已提交
587 588 589 590 591 592 593 594
```

## FileAsset.open

open(mode: string): Promise&lt;number&gt;;

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

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
**参数:**

P
panqiangbiao 已提交
601 602 603
| 参数名 | 类型   | 必填 | 说明                             |
| ------ | ------ | ---- | -------------------------------- |
| mode   | string | 是   | 打开文件方式,如:'r', 'w', 'rw' |
P
panqiangbiao 已提交
604 605 606 607 608

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
609
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
610 611 612 613

**示例:**

```
P
panqiangbiao 已提交
614 615 616 617
let mediaType = mediaLibrary.MediaType.IMAGE;
let path = "Pictures/";
asset = await media.createAsset(mediaType, "image00003.jpg", path);
asset.open('rw').then((openError, fd) => {
P
panqiangbiao 已提交
618 619 620 621 622
    if(fd > 0){
        asset.close(fd);
    }else{
        console.info('File Open Failed!' + openError);
    }
P
panqiangbiao 已提交
623
});
P
panqiangbiao 已提交
624 625 626 627 628 629 630 631
```

## FileAsset.close

close(fd: number, callback: AsyncCallback&lt;void&gt;): void;

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

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

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

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

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
640
| fd       | number                 | 是   | 文件描述符          |
P
panqiangbiao 已提交
641 642 643 644 645
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |

**示例:**

```
P
panqiangbiao 已提交
646 647 648 649 650 651 652 653 654 655
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
asset.close(fd, (closeErr) => {
P
panqiangbiao 已提交
656 657 658 659 660 661
    if (closeErr != undefined) {
        console.info('MediaLibraryTest : close : FAIL ' + closeErr.message);
        console.info('MediaLibraryTest : ASSET_CALLBACK : FAIL');
    } else {
        console.info("=======asset.close success====>");
    }
P
panqiangbiao 已提交
662
});
P
panqiangbiao 已提交
663 664 665 666 667 668 669 670
```

## FileAsset.close

close(fd: number): Promise&lt;void&gt;;

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

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

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

P
panqiangbiao 已提交
675 676 677 678 679 680 681 682 683 684
**参数:**

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

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
685
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
686 687 688 689

**示例:**

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

    } else {
        console.info("=======asset.close success====>");
    }
P
panqiangbiao 已提交
707
});
P
panqiangbiao 已提交
708 709 710 711 712 713 714 715
```

## FileAsset.getThumbnail

getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void;

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

P
panqiangbiao 已提交
716
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
717

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

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

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

**示例:**

```
P
panqiangbiao 已提交
729 730 731 732 733 734 735 736 737 738
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
asset.getThumbnail((err, pixelmap) => {
P
panqiangbiao 已提交
739
	console.info('MediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
740
});
P
panqiangbiao 已提交
741 742 743 744 745 746 747 748
```

## FileAsset.getThumbnail

getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void;

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

P
panqiangbiao 已提交
749
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
750

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

P
panqiangbiao 已提交
753 754 755 756
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
757
| size     | [Size](#Size)          | 是   | 缩略图尺寸          |
P
panqiangbiao 已提交
758 759 760 761 762
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是   | 回调返回缩略图的PixelMap |

**示例:**

```
P
panqiangbiao 已提交
763 764 765 766 767 768 769 770 771 772
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
asset.getThumbnail(size, (err, pixelmap) => {
P
panqiangbiao 已提交
773
	console.info('MediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
774
});
P
panqiangbiao 已提交
775 776 777 778 779 780 781 782
```

## FileAsset.getThumbnail

getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;;

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

P
panqiangbiao 已提交
783
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
784

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

P
panqiangbiao 已提交
787 788 789 790 791 792 793 794 795 796
**参数:**

| 参数名 | 类型          | 必填 | 说明       |
| ------ | ------------- | ---- | ---------- |
| size   | [Size](#Size) | 否   | 缩略图尺寸 |

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
797
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
798 799 800 801

**示例:**

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

## FileAsset.favorite

favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void;

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

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

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

P
panqiangbiao 已提交
826 827 828 829
**参数:**

| 参数名     | 类型                | 必填 | 说明           |
| ---------- | ------------------- | ---- | -------------- |
P
panqiangbiao 已提交
830
| isFavorite | boolean             | 是   | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
831
| callback   | AsyncCallback&lt;void&gt; | 是   | 回调返回空    |
P
panqiangbiao 已提交
832 833 834 835

**示例:**

```
P
panqiangbiao 已提交
836 837 838 839 840 841 842 843 844
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
845 846 847
asset.favorite(true,function(err){
    // do something
});
P
panqiangbiao 已提交
848 849 850 851 852 853 854 855
```

## FileAsset.favorite

favorite(isFavorite: boolean): Promise&lt;void&gt;;

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

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

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

P
panqiangbiao 已提交
860 861
**参数:**

P
panqiangbiao 已提交
862 863 864
| 参数名     | 类型    | 必填 | 说明                                                      |
| ---------- | ------- | ---- | --------------------------------------------------------- |
| isFavorite | boolean | 是   | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
865 866 867 868 869

**返回值:**

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

**示例:**

```
P
panqiangbiao 已提交
875 876 877 878 879 880 881 882 883
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
884 885 886 887 888
asset.favorite(true).then(function() {
    console.info("favorite successfully");
}).catch(function(err){
    console.info("favorite failed with error:"+ err);
});
P
panqiangbiao 已提交
889 890 891 892 893 894 895 896
```

## FileAsset.isFavorite

isFavorite(callback: AsyncCallback&lt;boolean&gt;): void;

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

P
panqiangbiao 已提交
897
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
898

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

P
panqiangbiao 已提交
901 902 903 904 905 906 907 908 909
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
910 911 912 913 914 915 916 917 918
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
919 920 921 922 923 924
asset.isFavorite((err, isFavorite) => {
    if (isFavorite) {
        console.info('FileAsset is favorite');
    }else{
        console.info('FileAsset is not favorite');
    }
P
panqiangbiao 已提交
925
});
P
panqiangbiao 已提交
926 927 928 929 930 931 932 933
```

## FileAsset.isFavorite

isFavorite():Promise&lt;boolean&gt;;

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

P
panqiangbiao 已提交
934
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
935

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

P
panqiangbiao 已提交
938 939 940 941
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
942
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
943 944 945 946

**示例:**

```
P
panqiangbiao 已提交
947 948 949 950 951 952 953 954 955
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
956 957 958 959 960
asset.isFavorite().then(function(isFavorite){
    console.info("isFavorite result:"+ isFavorite);
}).catch(function(err){
    console.info("isFavorite failed with error:"+ err);
});
P
panqiangbiao 已提交
961 962 963 964 965 966 967 968
```

## FileAsset.trash

trash(isTrash: boolean, callback: AsyncCallback&lt;void&g;): void;

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

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

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

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

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

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
979
| isTrash  | boolean                | 是   | 是否设置为垃圾文件    |
P
panqiangbiao 已提交
980 981 982 983 984
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |

**示例:**

```
P
panqiangbiao 已提交
985 986 987 988 989 990 991 992 993
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
994 995
asset.trash(true, trashCallBack);
function trashCallBack(err, trash) {
P
panqiangbiao 已提交
996
    console.info('MediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
997
}
P
panqiangbiao 已提交
998 999 1000 1001 1002 1003 1004 1005
```

## FileAsset.trash

trash(isTrash: boolean,): Promise&lt;void&gt;;

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

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

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

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

P
panqiangbiao 已提交
1012 1013
**参数:**

P
panqiangbiao 已提交
1014 1015 1016
| 参数名  | 类型    | 必填 | 说明               |
| ------- | ------- | ---- | ------------------ |
| isTrash | boolean | 是   | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1017 1018 1019 1020 1021

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1022
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1023 1024 1025 1026

**示例:**

```
P
panqiangbiao 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1036 1037 1038 1039 1040
asset.trash(true).then(function() {
    console.info("trash successfully");
}).catch(function(err){
    console.info("trash failed with error:"+ err);
});
P
panqiangbiao 已提交
1041 1042 1043 1044 1045 1046 1047 1048
```

## FileAsset.isTrash

isTrash(callback: AsyncCallback&lt;boolean&gt;): void;

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

P
panqiangbiao 已提交
1049
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1050

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

P
panqiangbiao 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
1062 1063 1064 1065 1066 1067 1068 1069 1070
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1071 1072
asset.isTrash(isTrashCallBack);
function isTrashCallBack(err, isTrash) {
P
panqiangbiao 已提交
1073 1074 1075 1076 1077 1078 1079
    if (isTrash == true) {
        console.info('MediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK isTrash = ' + isTrash);
        asset.trash(true, trashCallBack);
    } else {
        console.info('MediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
        console.info('MediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');
    }
P
panqiangbiao 已提交
1080
}
P
panqiangbiao 已提交
1081 1082 1083 1084 1085 1086 1087 1088
```

## FileAsset.isTrash

isTrash():Promise&lt;boolean&gt;;

当文件被定位,设置文件为垃圾文件,使用promise方式返回异步结果。

P
panqiangbiao 已提交
1089
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1090

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

P
panqiangbiao 已提交
1093 1094 1095 1096
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1097
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1098 1099 1100 1101

**示例:**

```
P
panqiangbiao 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1111 1112 1113 1114 1115
asset.isTrash().then(function(isTrash){
    console.info("isTrash result:"+ isTrash);
}).catch(function(err){
    console.info("isTrash failed with error:"+ err);
});
P
panqiangbiao 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
```

**FetchFileResult**

文件检索结果集。

## FetchFileResult.getCount

getCount(): number;

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

P
panqiangbiao 已提交
1128
**需要权限**:无
P
panqiangbiao 已提交
1129

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

P
panqiangbiao 已提交
1132 1133
**返回值**

P
panqiangbiao 已提交
1134 1135 1136
| 类型   | 说明             |
| ------ | ---------------- |
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1137 1138 1139 1140

**示例**

```
P
panqiangbiao 已提交
1141 1142 1143 1144 1145 1146 1147 1148
let getFileCountOneOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [fileType.toString()],
    order: fileKeyObj.DATE_ADDED,
    extendArgs: "LIMIT 0,1",
};
let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1149 1150 1151 1152 1153 1154 1155 1156
```

## FetchFileResult.isAfterLast

isAfterLast(): boolean;

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

P
panqiangbiao 已提交
1157
**需要权限**:无
P
panqiangbiao 已提交
1158

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

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

| 类型    | 说明                                                         |
| ------- | ------------------------------------------------------------ |
P
panqiangbiao 已提交
1165
| boolean | 当读到最后一条记录后,再继续读就到结果集尾了,没有记录了,执行isAfterLast()方法就返回true,否则返回false |
P
panqiangbiao 已提交
1166 1167 1168 1169

**示例**

```
P
panqiangbiao 已提交
1170 1171 1172 1173 1174 1175 1176 1177
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
const fetchCount = fetchFileResult.getCount();
console.info('MediaLibraryTest : count:' + fetchCount);
let fileAsset = await fetchFileResult.getFirstObject();
for (var i = 1; i < fetchCount; i++) {
        fileAsset = await fetchFileResult.getNextObject();
        if(i == fetchCount - 1) {
          console.info('MediaLibraryTest : isLast');
          var result = fetchFileResult.isAfterLast();
          console.info('MediaLibraryTest : isAfterLast:' + result);
          console.info('MediaLibraryTest : isAfterLast end');
          fetchFileResult.close();
P
panqiangbiao 已提交
1189

P
panqiangbiao 已提交
1190 1191
        }
}
P
panqiangbiao 已提交
1192 1193 1194 1195 1196 1197 1198 1199
```

## FetchFileResult.close

close(): void;

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

P
panqiangbiao 已提交
1200
**需要权限**:无
P
panqiangbiao 已提交
1201

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

P
panqiangbiao 已提交
1204 1205 1206
**示例**

```
P
panqiangbiao 已提交
1207 1208 1209 1210 1211 1212 1213 1214
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1215
fetchFileResult.close();
P
panqiangbiao 已提交
1216 1217 1218 1219 1220 1221 1222 1223
```

## FetchFileResult.getFirstObject

getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void;

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

P
panqiangbiao 已提交
1224
**需要权限**:无
P
panqiangbiao 已提交
1225

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

P
panqiangbiao 已提交
1228 1229
**参数**

P
panqiangbiao 已提交
1230 1231 1232
| 参数名   | 类型                                         | 必填 | 说明                                        |
| -------- | -------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#FileAsset)&gt; | 是   | 异步获取结果集中第一个FileAsset完成后的回调 |
P
panqiangbiao 已提交
1233 1234 1235 1236

**示例**

```
P
panqiangbiao 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getFirstObject((err, value) => {
P
panqiangbiao 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getFirstObject

getFirstObject(): Promise&lt;FileAsset&gt;;

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

P
panqiangbiao 已提交
1260
**需要权限**:无
P
panqiangbiao 已提交
1261

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

P
panqiangbiao 已提交
1264 1265 1266 1267
**返回值**

| 类型                | 说明                                   |
| ------------------ | -------------------------------------- |
P
panqiangbiao 已提交
1268
| Promise&lt;[FileAsset](#FileAsset)&gt; | Promise方式返回FileAsset               |
P
panqiangbiao 已提交
1269 1270 1271 1272 1273


**示例**

```
P
panqiangbiao 已提交
1274 1275 1276 1277 1278 1279 1280 1281 1282
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getFirstObject().then(function(fileAsset){
P
panqiangbiao 已提交
1283 1284 1285 1286
    console.info("getFirstObject successfully:"+ JSON.stringify(fileAsset));
}).catch(function(err){
    console.info("getFirstObject failed with error:"+ err);
});
P
panqiangbiao 已提交
1287 1288 1289 1290 1291 1292 1293 1294
```

## FetchFileResult.getNextObject

 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void;

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

P
panqiangbiao 已提交
1295
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1296

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

P
panqiangbiao 已提交
1299 1300
**参数**

P
panqiangbiao 已提交
1301 1302 1303
| 参数名    | 类型                                         | 必填 | 说明                                      |
| --------- | -------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#FileAsset)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1304 1305 1306 1307

**示例**

```
P
panqiangbiao 已提交
1308 1309 1310 1311 1312 1313 1314 1315 1316
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getNextObject((err, value) => {
P
panqiangbiao 已提交
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getNextObject

 getNextObject(): Promise&lt;FileAsset&gt;;

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

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

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

P
panqiangbiao 已提交
1335 1336 1337 1338
**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1339
| Promise&lt;[FileAsset](#FileAsset)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1340 1341 1342 1343

**示例**

```
P
panqiangbiao 已提交
1344 1345 1346 1347 1348 1349 1350 1351
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1352 1353 1354
const fetchCount = fetchFileResult.getCount();
console.info('MediaLibraryTest : count:' + fetchCount);
fileAsset = await fetchFileResult.getNextObject();
P
panqiangbiao 已提交
1355 1356 1357 1358 1359 1360 1361 1362
```

## FetchFileResult.getLastObject

getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void;

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

P
panqiangbiao 已提交
1363
**需要权限**:无
P
panqiangbiao 已提交
1364

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

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

P
panqiangbiao 已提交
1369 1370 1371
| 参数     | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#FileAsset)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1372 1373 1374 1375

**示例**

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

## FetchFileResult.getLastObject

getLastObject(): Promise&lt;FileAsset&gt;;

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

P
panqiangbiao 已提交
1399
**需要权限**:无
P
panqiangbiao 已提交
1400

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

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

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

**示例**

```
P
panqiangbiao 已提交
1412 1413 1414 1415 1416 1417 1418 1419 1420
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
let lastObject = await fetchFileResult.getLastObject();
P
panqiangbiao 已提交
1421 1422 1423 1424 1425 1426 1427 1428
```

## FetchFileResult.getPositionObject

getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void;

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

P
panqiangbiao 已提交
1429
**需要权限**:无
P
panqiangbiao 已提交
1430

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

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

P
panqiangbiao 已提交
1435 1436
| 参数     | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
P
panqiangbiao 已提交
1437
| index    | number                                       | 是   | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
1438
| callback | AsyncCallback&lt;[FileAsset](#FileAsset)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1439 1440 1441 1442

**示例**

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

## FetchFileResult.getPositionObject

getPositionObject(index: number): Promise&lt;FileAsset&gt;;

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

P
panqiangbiao 已提交
1466
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1467

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

P
panqiangbiao 已提交
1470 1471 1472 1473
**参数**

| 参数  | 类型   | 必填 | 说明                 |
| ----- | ------ | ---- | -------------------- |
P
panqiangbiao 已提交
1474
| index | number | 是   | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
1475 1476 1477 1478 1479

**返回值**

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

**示例**

```
P
panqiangbiao 已提交
1485 1486 1487 1488 1489 1490 1491 1492 1493
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getPositionObject(1,(err, value) => {
P
panqiangbiao 已提交
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getAllObject

getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void;

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

P
panqiangbiao 已提交
1508
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1509

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

P
panqiangbiao 已提交
1512 1513
**参数**

P
panqiangbiao 已提交
1514 1515 1516
| 参数     | 类型                                          | 必填 | 说明                            |
| -------- | --------------------------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback<Array<[FileAsset](#FileAsset)>> | 是   | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
1517 1518 1519 1520

**示例**

```
P
panqiangbiao 已提交
1521 1522 1523 1524 1525 1526 1527 1528 1529
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
fetchFileResult.getAllObject((err, value) => {
P
panqiangbiao 已提交
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getAllObject

getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;;

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

P
panqiangbiao 已提交
1544
**需要权限**:无
P
panqiangbiao 已提交
1545

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

P
panqiangbiao 已提交
1548 1549
**返回值**

P
panqiangbiao 已提交
1550 1551 1552
| 类型                                    | 说明                  |
| --------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#FileAsset)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
1553 1554 1555 1556

**示例**

```
P
panqiangbiao 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565
let imageType = mediaLibrary.MediaType.IMAGE;
let getImageOp = {
  selections: fileKeyObj.MEDIA_TYPE + '= ?',
  selectionArgs: [imageType.toString()],
  order: fileKeyObj.DATE_ADDED,
  extendArgs: "LIMIT 0,10",
};
let fetchFileResult = await media.getFileAssets(getImageOp);
var data = fetchFileResult.getAllObject();
P
panqiangbiao 已提交
1566 1567 1568 1569 1570 1571 1572 1573
```

## Album.commitModify

commitModify(callback: AsyncCallback&lt;void&gt;): void;

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

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

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

P
panqiangbiao 已提交
1578 1579 1580 1581
**参数**

| 参数     | 类型                | 必填 | 说明                          |
| -------- | ------------------- | ---- | ----------------------------- |
P
panqiangbiao 已提交
1582
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空                  |
P
panqiangbiao 已提交
1583 1584 1585 1586

**示例**

```
P
panqiangbiao 已提交
1587 1588 1589 1590 1591 1592
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1593
album.albumName = 'hello';
P
panqiangbiao 已提交
1594
album.commitModify((err) => {
P
panqiangbiao 已提交
1595 1596 1597 1598
   if (err) {
	   console.error('Failed ');
	   return;
   }
P
panqiangbiao 已提交
1599
   console.log('Modify successful.');
P
panqiangbiao 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608
})
```

## Album.commitModify

commitModify(): Promise&lt;void&gt;;

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

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

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

P
panqiangbiao 已提交
1613 1614 1615 1616 1617 1618 1619 1620 1621
**返回值**

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

**示例**

```
P
panqiangbiao 已提交
1622 1623 1624 1625 1626 1627
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1628
album.albumName = 'hello';
P
panqiangbiao 已提交
1629 1630 1631 1632 1633
album.commitModify().then(function() {
    console.info("commitModify successfully");
}).catch(function(err){
    console.info("commitModify failed with error:"+ err);
});
P
panqiangbiao 已提交
1634 1635 1636 1637 1638 1639 1640 1641
```

## Album.getFileAssets

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

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

P
panqiangbiao 已提交
1642
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1643

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

P
panqiangbiao 已提交
1646 1647
**参数**

P
panqiangbiao 已提交
1648 1649 1650 1651
| 参数     | 类型                                                        | 必填 | 说明                              |
| -------- | ----------------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#MediaFetchOptions)                     | 是   | 媒体检索选项                      |
| callback | AsyncCallback<[FetchFileResult](#FetchFileResult.getCount)> | 是   | 异步返回FetchFileResult之后的回调 |
P
panqiangbiao 已提交
1652 1653 1654 1655

**示例**

```
P
panqiangbiao 已提交
1656 1657 1658 1659 1660 1661
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
})
function getFileAssetsCallBack(err, fetchFileResult) {
    // do something
}
```

## Album.getFileAssets

 getFileAssets(options?: MediaFetchOptions): Promise&lt;FetchFileResult&gt;;

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

P
panqiangbiao 已提交
1675
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1676

P
panqiangbiao 已提交
1677
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1678 1679 1680

**参数**

P
panqiangbiao 已提交
1681 1682 1683
| 参数    | 类型                                    | 必填 | 说明         |
| ------- | --------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#MediaFetchOptions) | 否   | 媒体检索选项 |
P
panqiangbiao 已提交
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693

**返回值**

| 类型                                                  | 说明                    |
| ----------------------------------------------------- | ----------------------- |
| Promise<[FetchFileResult](#FetchFileResult.getCount)> | 返回FetchFileResult对象 |

**示例**

```
P
panqiangbiao 已提交
1694 1695 1696 1697 1698 1699
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1700 1701
album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
    console.info("getFileAssets successfully:"+ JSON.stringify(albumFetchFileResult));
P
panqiangbiao 已提交
1702 1703 1704
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
P
panqiangbiao 已提交
1705 1706
```

P
panqiangbiao 已提交
1707 1708
## PeerInfo

P
panqiangbiao 已提交
1709
注册设备信息。
P
panqiangbiao 已提交
1710

P
panqiangbiao 已提交
1711 1712
| 名称       | 类型       | 可读 | 可写 | 说明           |
| ---------- | ---------- | ---- | ---- | -------------- |
P
panqiangbiao 已提交
1713 1714
| deviceName | string     | 是   | 否   | 注册设备名称   |
| networkId  | string     | 是   | 否   | 注册设备网络ID |
P
panqiangbiao 已提交
1715 1716
| deviceType | DeviceType | 是   | 否   | 设备类型       |
| isOnline   | boolean    | 是   | 否   | 是否在线       |
P
panqiangbiao 已提交
1717 1718 1719 1720 1721 1722

FileAsset
---------

**属性:**

P
panqiangbiao 已提交
1723 1724 1725 1726 1727 1728 1729 1730
| 名称         | 类型      | 可读 | 可写 | 说明                                                   |
| ------------ | --------- | ---- | ---- | ------------------------------------------------------ |
| id           | number    | 是   | 否   | 文件资源编号                                           |
| uri          | string    | 是   | 否   | 文件资源uri(如:dataability:///media/image/2)         |
| mimeType     | string    | 是   | 否   | 文件扩展属性                                           |
| mediaType    | MediaType | 是   | 否   | 媒体类型                                               |
| displayName  | string    | 是   | 是   | 显示文件名                                             |
| title        | string    | 是   | 是   | 文件标题                                               |
P
panqiangbiao 已提交
1731
| relativePath | string    | 是   | 是   | 相对公共目录路径                                               |
P
panqiangbiao 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
| 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 已提交
1746 1747 1748

Album
---------
P
panqiangbiao 已提交
1749

P
panqiangbiao 已提交
1750 1751 1752 1753 1754 1755
实体相册。

**属性**

| 名称         | 参数型 | 可读 | 可写 | 说明           |
| ------------ | ------ | ---- | ---- | -------------- |
P
panqiangbiao 已提交
1756 1757
| albumId      | number | 是   | 否   | 相册编号       |
| albumName    | string | 是   | 是   | 相册名称       |
P
panqiangbiao 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768
| albumUri     | string | 是   | 否   | 相册Uri        |
| dateModified | number | 是   | 否   | 修改日期       |
| count        | number | 是   | 否   | 相册中文件数量 |
| relativePath | string | 是   | 否   | 相对路径       |
| coverUri     | string | 是   | 否   | 封面文件Uri    |

MediaType
---------

枚举,媒体类型。

P
panqiangbiao 已提交
1769 1770 1771 1772 1773 1774
| 名称  | 默认值 | 描述 |
| ----- | ------ | ---- |
| FILE  | 0      | 文件 |
| IMAGE | 1      | 图片 |
| VIDEO | 2      | 视频 |
| AUDIO | 3      | 音频 |
P
panqiangbiao 已提交
1775 1776 1777 1778 1779 1780

FileKey
-------

枚举,文件关键信息。

P
panqiangbiao 已提交
1781 1782 1783
| 名称          | 默认值              | 描述                                                   |
| ------------- | ------------------- |  ------------------------------------------------------ |
| ID            | file_id             | 文件编号                                               |
P
panqiangbiao 已提交
1784
| RELATIVE_PATH | relative_path       |  相对公共目录路径                                               |
P
panqiangbiao 已提交
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
| 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 已提交
1802 1803 1804 1805 1806 1807

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

枚举,目录类型。

P
panqiangbiao 已提交
1808 1809 1810 1811 1812 1813 1814 1815
| 名称          | 默认值 | 描述               |
| ------------- | ------ | ------------------ |
| DIR_CAMERA    | 0      | 表示Camera文件路径 |
| DIR_VIDEO     | 1      | 表示视频路径       |
| DIR_IMAGE     | 2      | 表示图片路径       |
| DIR_AUDIO     | 3      | 表示音频路径       |
| DIR_DOCUMENTS | 4      | 表示文档路径       |
| DIR_DOWNLOAD  | 5      | 表示下载路径       |
P
panqiangbiao 已提交
1816

P
panqiangbiao 已提交
1817
DeviceType
P
panqiangbiao 已提交
1818 1819 1820 1821
-----------

枚举,设备类型。

P
panqiangbiao 已提交
1822 1823 1824 1825 1826 1827 1828 1829 1830
| 名称         | 默认值 | 描述       |
| ------------ | ------ | ---------- |
| TYPE_UNKNOWN | 0      | 未识别设备 |
| TYPE_LAPTOP  | 1      | 笔记本电脑 |
| TYPE_PHONE   | 2      | 手机       |
| TYPE_TABLET  | 3      | 平板电脑   |
| TYPE_WATCH   | 4      | 智能手表   |
| TYPE_CAR     | 5      | 车载设备   |
| TYPE_TV      | 6      | 电视设备   |
P
panqiangbiao 已提交
1831

P
panqiangbiao 已提交
1832
## MediaFetchOptions
P
panqiangbiao 已提交
1833 1834 1835

检索条件。

P
panqiangbiao 已提交
1836 1837 1838
| 名称          | 类型          | 可读 | 可写 |  必填 |说明             |
| ------------- | ------------- | ---- | ---- |  ---- |---------------- |
| selections    | string        | 是   | 是   |是   | 检索条件         |
P
panqiangbiao 已提交
1839
| selectionArgs | Array&lt;string&gt; | 是   | 是   |是   | 检索条件的值     |
P
panqiangbiao 已提交
1840 1841 1842 1843
| order         | string        | 是   | 是   | 否   |检索结果排序方式 |
| uri           | string        | 是   | 是   | 否   |文件URI          |
| networkId     | string        | 是   | 是   | 否   |注册设备网络ID   |
| extendArgs    | string        | 是   | 是   | 否   |扩展的检索参数   |
P
panqiangbiao 已提交
1844 1845 1846 1847 1848

## Size

图片尺寸。

P
panqiangbiao 已提交
1849 1850 1851 1852
| 名称   | 类型   | 可读 | 可写 | 说明             |
| ------ | ------ | ---- | ---- | ---------------- |
| width  | number | 是   | 是   | 宽(单位:像素) |
| height | number | 是   | 是   | 高(单位:像素) |
P
panqiangbiao 已提交
1853