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

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

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


## getMediaLibrary

function getMediaLibrary(context: Context): MediaLibrary;

获取媒体库。

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

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

P
panqiangbiao 已提交
22 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 38
import featureAbility from '@ohos.ability.featureAbility';
var context = featureAbility.getContext()
P
panqiangbiao 已提交
39 40 41 42 43 44 45 46 47 48
var media = mediaLibrary.getMediaLibrary(context);
```

## medialibrary.getFileAssets


getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): void;    

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

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

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

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

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

**示例:**

```
P
panqiangbiao 已提交
63 64 65 66 67 68 69
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 已提交
70 71 72 73 74 75 76
    if (fetchFileResult != undefined) {
        console.info('MediaLibraryTest : ASSET_CALLBACK fetchFileResult success');
        fetchFileResult.getAllObject((err, fileAssetList) => {
            if (fileAssetList != undefined) {
                fileAssetList.forEach(getAllObjectInfo);
            }
    });
P
panqiangbiao 已提交
77
});
P
panqiangbiao 已提交
78 79 80 81 82 83 84
```
## medialibrary.getFileAssets

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

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

P
panqiangbiao 已提交
85
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
86

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

P
panqiangbiao 已提交
89 90
**参数:**

P
panqiangbiao 已提交
91 92 93
| 参数名  | 类型                                    | 必填 | 说明         |
| ------- | --------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#MediaFetchOptions) | 是   | 媒体检索选项 |
P
panqiangbiao 已提交
94 95 96

**返回值**

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

**示例:**

```
P
panqiangbiao 已提交
104 105 106 107 108 109 110
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 已提交
111 112 113 114
    console.info("getFileAssets successfully:"+ JSON.stringify(dir));
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
P
panqiangbiao 已提交
115 116 117 118
```

## medialibrary.on

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

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

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

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

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

| 参数名   | 类型                | 必填 | 说明                   |
| -------- | ---------------- | ---- | ------------------- |
P
panqiangbiao 已提交
131
| 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 已提交
132
| callback | callback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
133 134 135 136

**示例:**

```
P
panqiangbiao 已提交
137
medialibrary.on('imageChange', () => {
P
panqiangbiao 已提交
138 139 140 141 142
    this.sendNotify('image');
})
```
## medialibrary.off

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 148 149
变更通知类型包括:注册设备变更,相册变更,图片文件变更,音频文件变更,视频文件变更,文件变更,远端文件变更。

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

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

P
panqiangbiao 已提交
153 154 155 156
**参数:**

| 参数名   | 类型                | 必填 | 说明                   |
| -------- | ---------------- | ---- | ------------------- |
P
panqiangbiao 已提交
157
| 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 已提交
158
| callback | callback&lt;void&gt; | 否   | 回调返回空 |
P
panqiangbiao 已提交
159 160 161 162

**示例:**

```
P
panqiangbiao 已提交
163
medialibrary.off('imageChange', () => {
P
panqiangbiao 已提交
164 165 166 167 168 169 170 171 172 173
    this.sendNotify('image');
})
```

## medialibrary.createAsset

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

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

P
panqiangbiao 已提交
174
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
175

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

P
panqiangbiao 已提交
178 179
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
190 191 192 193
// 使用Callback方式创建Image类型文件
let mediaType = mediaLibrary.MediaType.IMAGE;
let path = "Pictures/";
medialibrary.createAsset(mediaType, “imageCallBack.jpg”, path, (err, albumFetchFileResult) => {
P
panqiangbiao 已提交
194 195 196 197 198
    if (albumFetchFileResult != undefined) {
        console.info('MediaLibraryTest : ALBUM_CALLBACK getFileAssets success');
        albumFetchFileResult.getAllObject((err, fileAssetList) => {
            if (fileAssetList != undefined) {
                fileAssetList.forEach(getAllObjectInfo);
P
panqiangbiao 已提交
199
            } else {
P
panqiangbiao 已提交
200 201
                console.info('getFileAssets fail, message = ' + err);
                console.info('getFileAssets getFileAssets :No data');
P
panqiangbiao 已提交
202 203
            }
        });
P
panqiangbiao 已提交
204 205 206 207
    } else {
        console.info('getFileAssets failed, message = ' + err);
    }
});
P
panqiangbiao 已提交
208 209 210 211 212 213 214 215
```

## medialibrary.createAsset

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

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

P
panqiangbiao 已提交
216
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
217

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

P
panqiangbiao 已提交
220 221
**参数:**

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

**返回值**

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

**示例:**

```
P
panqiangbiao 已提交
237 238 239
// 使用Promise方式创建Image类型文件
let mediaType = mediaLibrary.MediaType.IMAGE;
let path = "Pictures/";
P
panqiangbiao 已提交
240 241 242 243 244
medialibrary.createAsset(mediaType, "image01.jpg", path).then(function(asset){
    console.info("createAsset successfully:"+ JSON.stringify(asset));
}).catch(function(err){
    console.info("createAsset failed with error:"+ err);
});
P
panqiangbiao 已提交
245 246 247 248 249 250 251 252
```

## medialibrary.deleteAsset

deleteAsset(uri: string, callback: AsyncCallback&lt;void&gt;): void;

删除媒体资源,使用callback方式返回结果。

P
panqiangbiao 已提交
253
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
254

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

P
panqiangbiao 已提交
257 258 259 260
**参数:**

| 参数名   | 类型                | 必填 | 说明                   |
| -------- | ------------------- | ---- | ---------------------- |
P
panqiangbiao 已提交
261 262
| uri      | string              | 是   | 文件uri             |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
263 264 265 266

**示例:**

```
P
panqiangbiao 已提交
267 268 269 270 271 272 273 274 275 276
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();
medialibrary.deleteAsset(asset.uri, (deleteAssetErr, deleteRows) => {
P
panqiangbiao 已提交
277
            // do something
P
panqiangbiao 已提交
278
});
P
panqiangbiao 已提交
279 280 281 282 283 284 285 286
```

## medialibrary.deleteAsset

deleteAsset(uri: string): Promise&lt;void&gt;;

删除媒体资源,使用promise方式返回结果。

P
panqiangbiao 已提交
287
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
288

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

P
panqiangbiao 已提交
291 292
**参数:**

P
panqiangbiao 已提交
293 294 295
| 参数名 | 类型   | 必填 | 说明    |
| ------ | ------ | ---- | ------- |
| uri    | string | 是   | 文件uri |
P
panqiangbiao 已提交
296 297 298 299 300

**返回值:**

| 类型          | 说明                                              |
| ------------- | ------------------------------------------------- |
P
panqiangbiao 已提交
301
| Promise&lt;void&gt; | Promise实例,用于异步获取结果。本调用将返回空值 |
P
panqiangbiao 已提交
302 303 304 305

**示例:**

```
P
panqiangbiao 已提交
306 307 308 309 310 311 312 313 314
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 已提交
315 316 317 318 319
medialibrary.deleteAsset(asset.uri).then(()=>{
    console.info("deleteAsset successfully");
}).catch(function(err){
    console.info("deleteAsset failed with error:"+ err);
});
P
panqiangbiao 已提交
320 321
```

P
panqiangbiao 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
## medialibrary.getPublicDirectory

getPublicDirectory(type: DirectoryType, callback: AsyncCallback<string>): void;

获取公共目录路径

**需要权限**:无

**系统能力**: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 已提交
346
        console.info('MediaLibraryTest : getPublicDirectory passed');
P
panqiangbiao 已提交
347
    } else {
P
panqiangbiao 已提交
348
        console.info('MediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    }
});
```

## medialibrary.getPublicDirectory

getPublicDirectory(type: DirectoryType): Promise<string>;

获取公共目录路径

**需要权限**:无

**系统能力**: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 已提交
381
    console.info('MediaLibraryTest : getPublicDirectory passed');    
P
panqiangbiao 已提交
382
} else {
P
panqiangbiao 已提交
383
    console.info('MediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
384 385 386
}
```

P
panqiangbiao 已提交
387 388 389 390 391 392
## medialibrary.getAlbums

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

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

P
panqiangbiao 已提交
393
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
394

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

P
panqiangbiao 已提交
397 398
**参数**

P
panqiangbiao 已提交
399 400 401 402
| 参数名   | 类型                                        | 必填 | 说明                        |
| -------- | ------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#MediaFetchOptions)     | 是   | 媒体文件获取条件            |
| callback | AsyncCallback&lt;Array<[Album](#Album)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
403 404 405 406

**示例:**

```
P
panqiangbiao 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
medialibrary.AlbumNoArgsfetchOp, (err, albumList) => {
    if (albumList != undefined) {
        const album = albumList[0];
        console.info('album.albumName = ' + album.albumName);
        console.info('album.count = ' + album.count);
        done();
     } else {
        console.info('getAlbum fail, message = ' + err);
        done();
     }
});
P
panqiangbiao 已提交
422 423 424 425 426 427 428 429
```

## medialibrary.getAlbums

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

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

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

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

P
panqiangbiao 已提交
434 435
**参数:**

P
panqiangbiao 已提交
436 437 438
| 参数名  | 类型                                    | 必填 | 说明             |
| ------- | --------------------------------------- | ---- | ---------------- |
| options | [MediaFetchOptions](#MediaFetchOptions) | 是   | 媒体文件获取条件 |
P
panqiangbiao 已提交
439 440 441 442 443 444 445 446 447 448

**返回值:**

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

**示例:**

```
P
panqiangbiao 已提交
449 450 451 452
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
P
panqiangbiao 已提交
453 454 455 456 457
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 已提交
458 459 460 461 462 463
```

## medialibrary.getActivePeers

getActivePeers(callback: AsyncCallback<Array&lt;PeerInfo&gt;>): void;

P
panqiangbiao 已提交
464
获取活动注册设备
P
panqiangbiao 已提交
465

P
panqiangbiao 已提交
466
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
467

P
panqiangbiao 已提交
468
**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore
P
panqiangbiao 已提交
469

P
panqiangbiao 已提交
470 471 472 473
**参数:**

| 参数名   | 类型                            | 必填 | 说明                   |
| -------- | ------------------------------ | ---- | ---------------------- |
P
panqiangbiao 已提交
474
| callback | AsyncCallback<Array<[PeerInfo](#PeerInfo)>> | 是   | 回调表示成功还是失败 |
P
panqiangbiao 已提交
475 476 477 478 479 480

**示例:**

```
medialibrary.getActivePeers((err, data) => {
    // do something    
P
panqiangbiao 已提交
481
});
P
panqiangbiao 已提交
482 483 484 485 486 487
```

## medialibrary.getActivePeers

getActivePeers(): Promise<Array&lt;PeerInfo&gt;>;

P
panqiangbiao 已提交
488
获取活动注册设备
P
panqiangbiao 已提交
489

P
panqiangbiao 已提交
490
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
491

P
panqiangbiao 已提交
492
**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore
P
panqiangbiao 已提交
493

P
panqiangbiao 已提交
494 495 496 497
**参数:**

返回值:

P
panqiangbiao 已提交
498 499 500
| 类型                                  | 说明                              |
| ------------------------------------- | --------------------------------- |
| Promise<Array<[PeerInfo](#PeerInfo)>> | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
501 502 503 504 505 506 507 508 509 510 511

**示例:**

```
peerInfoList = medialibrary.getActivePeers();
```

## medialibrary.getAllPeers

getAllPeers(callback: AsyncCallback<Array&lt;PeerInfo&gt;>): void;

P
panqiangbiao 已提交
512
获取所有注册设备
P
panqiangbiao 已提交
513

P
panqiangbiao 已提交
514
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
515

P
panqiangbiao 已提交
516
**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore
P
panqiangbiao 已提交
517

P
panqiangbiao 已提交
518 519 520 521
**参数:**

| 参数名   | 类型                            | 必填 | 说明                   |
| -------- | ------------------------------ | ---- | ---------------------- |
P
panqiangbiao 已提交
522
| callback | AsyncCallback<Array<[PeerInfo](#PeerInfo)>> | 是   | 回调表示成功还是失败 |
P
panqiangbiao 已提交
523 524 525 526 527 528

**示例:**

```
medialibrary.getAllPeers((err, data) => {
    // do something
P
panqiangbiao 已提交
529
});
P
panqiangbiao 已提交
530 531 532 533 534 535
```

## medialibrary.getAllPeers

getAllPeers(): Promise<Array&lt;PeerInfo&gt;>;

P
panqiangbiao 已提交
536
获取所有注册设备
P
panqiangbiao 已提交
537

P
panqiangbiao 已提交
538
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
539

P
panqiangbiao 已提交
540
**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore
P
panqiangbiao 已提交
541

P
panqiangbiao 已提交
542 543
**返回值:**

P
panqiangbiao 已提交
544 545 546
| 类型                                  | 说明                              |
| ------------------------------------- | --------------------------------- |
| Promise<Array<[PeerInfo](#PeerInfo)>> | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
547 548 549 550 551 552 553 554 555 556 557

**示例:**

peerInfoList = media.getAllPeers();

## medialibrary.release

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

释放MediaLibrary实例

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

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

P
panqiangbiao 已提交
562 563 564 565
**参数:**

| 参数名   | 类型                   | 必填 | 说明                   |
| -------- | ------------------- | ---- | ---------------------- |
P
panqiangbiao 已提交
566
| callback | AsyncCallback&lt;void&gt; | 是   | 回调表示成功还是失败 |
P
panqiangbiao 已提交
567 568 569 570 571 572

**示例:**

```
medialibrary.release((err, data) => {
    // do something
P
panqiangbiao 已提交
573
});
P
panqiangbiao 已提交
574 575 576 577 578 579
```

## medialibrary.release

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

P
panqiangbiao 已提交
580
释放MediaLibrary实例 (补充说明)
P
panqiangbiao 已提交
581

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

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

P
panqiangbiao 已提交
586 587 588 589
**返回值:**

| 类型          | 说明                                |
| ------------- | ----------------------------------- |
P
panqiangbiao 已提交
590
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603

**示例:**

```
medialibrary.release()
```

## FileAsset.isDirectory

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

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

P
panqiangbiao 已提交
604
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
605

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

P
panqiangbiao 已提交
608 609 610 611 612 613 614 615 616
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
617 618 619 620 621 622 623 624 625
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 已提交
626 627
asset.isDirectory((err, isDirectory) => {
    // do something
P
panqiangbiao 已提交
628
});
P
panqiangbiao 已提交
629 630 631 632 633 634 635 636
```

## FileAsset.isDirectory

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

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

P
panqiangbiao 已提交
637
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
638

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

P
panqiangbiao 已提交
641 642 643 644
**返回值:**

| 类型             | 说明                                       |
| ---------------- | ------------------------------------------ |
P
panqiangbiao 已提交
645
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
646 647 648 649

**示例:**

```
P
panqiangbiao 已提交
650 651 652 653 654 655 656 657 658
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 已提交
659 660 661 662 663
asset.isDirectory().then(function(isDirectory){
    console.info("isDirectory result:"+ isDirectory);
}).catch(function(err){
    console.info("isDirectory failed with error:"+ err);
});
P
panqiangbiao 已提交
664 665 666 667 668 669 670 671
```

## FileAsset.commitModify

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

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

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

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

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

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

**示例:**

```
P
panqiangbiao 已提交
685 686 687 688 689 690 691 692 693
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 已提交
694
asset.title = ‘newtitle';
P
panqiangbiao 已提交
695
asset.commitModify(() => {
P
panqiangbiao 已提交
696
    console.info('commitModify success');   
P
panqiangbiao 已提交
697
}
P
panqiangbiao 已提交
698 699 700 701 702 703 704 705
```

## FileAsset.commitModify

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

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

P
panqiangbiao 已提交
706
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
707

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

P
panqiangbiao 已提交
710 711 712 713
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
714
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
715 716 717 718

**示例:**

```
P
panqiangbiao 已提交
719 720 721 722 723 724 725 726 727
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 已提交
728 729
asset.title = ‘newtitle';
asset.commitModify();
P
panqiangbiao 已提交
730 731 732 733 734 735 736 737
```

## FileAsset.open

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

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

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

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

P
panqiangbiao 已提交
742 743 744 745
**参数**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
746
| mode     | string                 | 是   | 打开文件方式,如:'r', 'w', 'rw' |
P
panqiangbiao 已提交
747 748 749 750 751
| callback | AsyncCallback&lt;number&gt; | 是   | 回调返回文件句柄 |

**示例:**

```
P
panqiangbiao 已提交
752 753 754 755 756 757 758 759 760
let mediaType = mediaLibrary.MediaType.IMAGE;
let path = "Pictures/";
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 已提交
761
});
P
panqiangbiao 已提交
762 763 764 765 766 767 768 769
```

## FileAsset.open

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

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

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

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

P
panqiangbiao 已提交
774 775
**参数:**

P
panqiangbiao 已提交
776 777 778
| 参数名 | 类型   | 必填 | 说明                             |
| ------ | ------ | ---- | -------------------------------- |
| mode   | string | 是   | 打开文件方式,如:'r', 'w', 'rw' |
P
panqiangbiao 已提交
779 780 781 782 783

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
784
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
785 786 787 788

**示例:**

```
P
panqiangbiao 已提交
789 790 791 792 793 794 795 796 797
let mediaType = mediaLibrary.MediaType.IMAGE;
let path = "Pictures/";
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 已提交
798
});
P
panqiangbiao 已提交
799 800 801 802 803 804 805 806
```

## FileAsset.close

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

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

P
panqiangbiao 已提交
807
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
808

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

P
panqiangbiao 已提交
811 812 813 814
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
815
| fd       | number                 | 是   | 文件描述符          |
P
panqiangbiao 已提交
816 817 818 819 820
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |

**示例:**

```
P
panqiangbiao 已提交
821 822 823 824 825 826 827 828 829 830
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 已提交
831 832 833 834 835 836
    if (closeErr != undefined) {
        console.info('MediaLibraryTest : close : FAIL ' + closeErr.message);
        console.info('MediaLibraryTest : ASSET_CALLBACK : FAIL');
    } else {
        console.info("=======asset.close success====>");
    }
P
panqiangbiao 已提交
837
});
P
panqiangbiao 已提交
838 839 840 841 842 843 844 845
```

## FileAsset.close

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

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

P
panqiangbiao 已提交
846
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
847

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

P
panqiangbiao 已提交
850 851 852 853 854 855 856 857 858 859
**参数:**

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

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
860
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
861 862 863 864

**示例:**

```
P
panqiangbiao 已提交
865 866 867 868 869 870 871 872 873 874
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 已提交
875 876 877 878 879 880 881
    if (closeErr != undefined) {
        console.info('MediaLibraryTest : close : FAIL ' + closeErr.message);
        console.info('MediaLibraryTest : ASSET_CALLBACK : FAIL');

    } else {
        console.info("=======asset.close success====>");
    }
P
panqiangbiao 已提交
882
});
P
panqiangbiao 已提交
883 884 885 886 887 888 889 890
```

## FileAsset.getThumbnail

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

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

P
panqiangbiao 已提交
891
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
892

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

P
panqiangbiao 已提交
895 896 897 898 899 900 901 902 903
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
904 905 906 907 908 909 910 911 912 913
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 已提交
914
	console.info('MediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
915
});
P
panqiangbiao 已提交
916 917 918 919 920 921 922 923
```

## FileAsset.getThumbnail

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

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

P
panqiangbiao 已提交
924
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
925

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

P
panqiangbiao 已提交
928 929 930 931
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
932
| size     | [Size](#Size)          | 是   | 缩略图尺寸          |
P
panqiangbiao 已提交
933 934 935 936 937
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是   | 回调返回缩略图的PixelMap |

**示例:**

```
P
panqiangbiao 已提交
938 939 940 941 942 943 944 945 946 947
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 已提交
948
	console.info('MediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
949
});
P
panqiangbiao 已提交
950 951 952 953 954 955 956 957
```

## FileAsset.getThumbnail

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

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

P
panqiangbiao 已提交
958
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
959

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

P
panqiangbiao 已提交
962 963 964 965 966 967 968 969 970 971
**参数:**

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

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
972
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
973 974 975 976

**示例:**

```
P
panqiangbiao 已提交
977 978 979 980 981 982 983 984 985 986
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 已提交
987
	console.info('MediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
988
});
P
panqiangbiao 已提交
989 990 991 992 993 994 995 996
```

## FileAsset.favorite

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

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

P
panqiangbiao 已提交
997
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
998

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

P
panqiangbiao 已提交
1001 1002 1003 1004
**参数:**

| 参数名     | 类型                | 必填 | 说明           |
| ---------- | ------------------- | ---- | -------------- |
P
panqiangbiao 已提交
1005
| isFavorite | boolean             | 是   | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1006
| callback   | AsyncCallback&lt;void&gt; | 是   | 回调返回空    |
P
panqiangbiao 已提交
1007 1008 1009 1010

**示例:**

```
P
panqiangbiao 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019
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 已提交
1020 1021 1022
asset.favorite(true,function(err){
    // do something
});
P
panqiangbiao 已提交
1023 1024 1025 1026 1027 1028 1029 1030
```

## FileAsset.favorite

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

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

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

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

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

P
panqiangbiao 已提交
1037 1038 1039
| 参数名     | 类型    | 必填 | 说明                                                      |
| ---------- | ------- | ---- | --------------------------------------------------------- |
| isFavorite | boolean | 是   | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1040 1041 1042 1043 1044

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1045
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1046 1047 1048 1049

**示例:**

```
P
panqiangbiao 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058
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 已提交
1059 1060 1061 1062 1063
asset.favorite(true).then(function() {
    console.info("favorite successfully");
}).catch(function(err){
    console.info("favorite failed with error:"+ err);
});
P
panqiangbiao 已提交
1064 1065 1066 1067 1068 1069 1070 1071
```

## FileAsset.isFavorite

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

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

P
panqiangbiao 已提交
1072
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1073

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

P
panqiangbiao 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084
**参数:**

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

**示例:**

```
P
panqiangbiao 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093
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 已提交
1094 1095 1096 1097 1098 1099
asset.isFavorite((err, isFavorite) => {
    if (isFavorite) {
        console.info('FileAsset is favorite');
    }else{
        console.info('FileAsset is not favorite');
    }
P
panqiangbiao 已提交
1100
});
P
panqiangbiao 已提交
1101 1102 1103 1104 1105 1106 1107 1108
```

## FileAsset.isFavorite

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

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

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
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1117
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1118 1119 1120 1121

**示例:**

```
P
panqiangbiao 已提交
1122 1123 1124 1125 1126 1127 1128 1129 1130
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 已提交
1131 1132 1133 1134 1135
asset.isFavorite().then(function(isFavorite){
    console.info("isFavorite result:"+ isFavorite);
}).catch(function(err){
    console.info("isFavorite failed with error:"+ err);
});
P
panqiangbiao 已提交
1136 1137 1138 1139 1140 1141 1142 1143
```

## FileAsset.trash

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

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

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

P
panqiangbiao 已提交
1146
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
1147

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

P
panqiangbiao 已提交
1150 1151 1152 1153
**参数:**

| 参数名   | 类型                   | 必填 | 说明                 |
| -------- | ---------------------- | ---- | -------------------- |
P
panqiangbiao 已提交
1154
| isTrash  | boolean                | 是   | 是否设置为垃圾文件    |
P
panqiangbiao 已提交
1155 1156 1157 1158 1159
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |

**示例:**

```
P
panqiangbiao 已提交
1160 1161 1162 1163 1164 1165 1166 1167 1168
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 已提交
1169 1170
asset.trash(true, trashCallBack);
function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1171
    console.info('MediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1172
}
P
panqiangbiao 已提交
1173 1174 1175 1176 1177 1178 1179 1180
```

## FileAsset.trash

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

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

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

P
panqiangbiao 已提交
1183
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
1184

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

P
panqiangbiao 已提交
1187 1188
**参数:**

P
panqiangbiao 已提交
1189 1190 1191
| 参数名  | 类型    | 必填 | 说明               |
| ------- | ------- | ---- | ------------------ |
| isTrash | boolean | 是   | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1192 1193 1194 1195 1196

**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1197
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1198 1199 1200 1201

**示例:**

```
P
panqiangbiao 已提交
1202 1203 1204 1205 1206 1207 1208 1209 1210
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 已提交
1211 1212 1213 1214 1215
asset.trash(true).then(function() {
    console.info("trash successfully");
}).catch(function(err){
    console.info("trash failed with error:"+ err);
});
P
panqiangbiao 已提交
1216 1217 1218 1219 1220 1221 1222 1223
```

## FileAsset.isTrash

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

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

P
panqiangbiao 已提交
1224
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1225

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

P
panqiangbiao 已提交
1228 1229 1230 1231 1232 1233 1234 1235 1236
**参数:**

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

**示例:**

```
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",
};
const fetchFileResult = await media.getFileAssets(getImageOp);
const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
asset.isTrash(isTrashCallBack);
function isTrashCallBack(err, isTrash) {
        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 已提交
1258 1259 1260 1261 1262 1263 1264 1265
```

## FileAsset.isTrash

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

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

P
panqiangbiao 已提交
1266
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1267

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

P
panqiangbiao 已提交
1270 1271 1272 1273
**返回值:**

| 类型          | 说明                          |
| ------------- | ----------------------------- |
P
panqiangbiao 已提交
1274
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1275 1276 1277 1278

**示例:**

```
P
panqiangbiao 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287
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 已提交
1288 1289 1290 1291 1292
asset.isTrash().then(function(isTrash){
    console.info("isTrash result:"+ isTrash);
}).catch(function(err){
    console.info("isTrash failed with error:"+ err);
});
P
panqiangbiao 已提交
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
```

**FetchFileResult**

文件检索结果集。

## FetchFileResult.getCount

getCount(): number;

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

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

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

P
panqiangbiao 已提交
1309 1310
**返回值**

P
panqiangbiao 已提交
1311 1312 1313
| 类型   | 说明             |
| ------ | ---------------- |
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1314 1315 1316 1317

**示例**

```
P
panqiangbiao 已提交
1318 1319 1320 1321 1322 1323 1324 1325
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 已提交
1326 1327 1328 1329 1330 1331 1332 1333
```

## FetchFileResult.isAfterLast

isAfterLast(): boolean;

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

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

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

P
panqiangbiao 已提交
1338 1339 1340 1341
**返回值**

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

**示例**

```
P
panqiangbiao 已提交
1347 1348 1349 1350 1351 1352 1353 1354
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 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
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 已提交
1369 1370 1371 1372 1373 1374 1375 1376
```

## FetchFileResult.close

close(): void;

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

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

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

P
panqiangbiao 已提交
1381 1382 1383
**示例**

```
P
panqiangbiao 已提交
1384 1385 1386 1387 1388 1389 1390 1391
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 已提交
1392
fetchFileResult.close();
P
panqiangbiao 已提交
1393 1394 1395 1396 1397 1398 1399 1400
```

## FetchFileResult.getFirstObject

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

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

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

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

P
panqiangbiao 已提交
1405 1406
**参数**

P
panqiangbiao 已提交
1407 1408 1409
| 参数名   | 类型                                         | 必填 | 说明                                        |
| -------- | -------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#FileAsset)&gt; | 是   | 异步获取结果集中第一个FileAsset完成后的回调 |
P
panqiangbiao 已提交
1410 1411 1412 1413

**示例**

```
P
panqiangbiao 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422
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 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getFirstObject

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

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

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

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

P
panqiangbiao 已提交
1441 1442 1443 1444
**返回值**

| 类型                | 说明                                   |
| ------------------ | -------------------------------------- |
P
panqiangbiao 已提交
1445
| Promise&lt;[FileAsset](#FileAsset)&gt; | Promise方式返回FileAsset               |
P
panqiangbiao 已提交
1446 1447 1448 1449 1450


**示例**

```
P
panqiangbiao 已提交
1451 1452 1453 1454 1455 1456 1457 1458 1459
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 已提交
1460 1461 1462 1463
    console.info("getFirstObject successfully:"+ JSON.stringify(fileAsset));
}).catch(function(err){
    console.info("getFirstObject failed with error:"+ err);
});
P
panqiangbiao 已提交
1464 1465 1466 1467 1468 1469 1470 1471
```

## FetchFileResult.getNextObject

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

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

P
panqiangbiao 已提交
1472
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1473

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

P
panqiangbiao 已提交
1476 1477
**参数**

P
panqiangbiao 已提交
1478 1479 1480
| 参数名    | 类型                                         | 必填 | 说明                                      |
| --------- | -------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&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.getNextObject((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.getNextObject

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

获取文件检索结果中的下一个文件资产。此方法使用promise方式来异步返回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 1514 1515
**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1516
| Promise&lt;[FileAsset](#FileAsset)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1517 1518 1519 1520

**示例**

```
P
panqiangbiao 已提交
1521 1522 1523 1524 1525 1526 1527 1528
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 已提交
1529 1530 1531
const fetchCount = fetchFileResult.getCount();
console.info('MediaLibraryTest : count:' + fetchCount);
fileAsset = await fetchFileResult.getNextObject();
P
panqiangbiao 已提交
1532 1533 1534 1535 1536 1537 1538 1539
```

## FetchFileResult.getLastObject

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

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

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

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

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

P
panqiangbiao 已提交
1546 1547 1548
| 参数     | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#FileAsset)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1549 1550 1551 1552

**示例**

```
P
panqiangbiao 已提交
1553 1554 1555 1556 1557 1558 1559 1560 1561
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 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getLastObject

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

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

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

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

P
panqiangbiao 已提交
1580 1581 1582 1583
**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1584
| Promise&lt;[FileAsset](#FileAsset)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1585 1586 1587 1588

**示例**

```
P
panqiangbiao 已提交
1589 1590 1591 1592 1593 1594 1595 1596 1597
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 已提交
1598 1599 1600 1601 1602 1603 1604 1605
```

## FetchFileResult.getPositionObject

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

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

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

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

P
panqiangbiao 已提交
1610 1611
**参数**

P
panqiangbiao 已提交
1612 1613
| 参数     | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
P
panqiangbiao 已提交
1614
| index    | number                                       | 是   | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
1615
| callback | AsyncCallback&lt;[FileAsset](#FileAsset)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1616 1617 1618 1619

**示例**

```
P
panqiangbiao 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628
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 已提交
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getPositionObject

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

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

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

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

P
panqiangbiao 已提交
1647 1648 1649 1650
**参数**

| 参数  | 类型   | 必填 | 说明                 |
| ----- | ------ | ---- | -------------------- |
P
panqiangbiao 已提交
1651
| index | number | 是   | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
1652 1653 1654 1655 1656

**返回值**

| 类型                             | 说明                |
| -------------------------------- | ------------------- |
P
panqiangbiao 已提交
1657
| Promise&lt;[FileAsset](#FileAsset)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1658 1659 1660 1661

**示例**

```
P
panqiangbiao 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670
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 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
   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 已提交
1685
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1686

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

P
panqiangbiao 已提交
1689 1690
**参数**

P
panqiangbiao 已提交
1691 1692 1693
| 参数     | 类型                                          | 必填 | 说明                            |
| -------- | --------------------------------------------- | ---- | ------------------------------- |
| callback | AsyncCallback<Array<[FileAsset](#FileAsset)>> | 是   | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
1694 1695 1696 1697

**示例**

```
P
panqiangbiao 已提交
1698 1699 1700 1701 1702 1703 1704 1705 1706
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 已提交
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
   if (err) {
	   console.error('Failed ');
	   return;
   }
   console.log(value);
})
```

## FetchFileResult.getAllObject

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

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

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

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

P
panqiangbiao 已提交
1725 1726
**返回值**

P
panqiangbiao 已提交
1727 1728 1729
| 类型                                    | 说明                  |
| --------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#FileAsset)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
1730 1731 1732 1733

**示例**

```
P
panqiangbiao 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742
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 已提交
1743 1744 1745 1746 1747 1748 1749 1750
```

## Album.commitModify

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

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

P
panqiangbiao 已提交
1751
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
1752

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

P
panqiangbiao 已提交
1755 1756 1757 1758
**参数**

| 参数     | 类型                | 必填 | 说明                          |
| -------- | ------------------- | ---- | ----------------------------- |
P
panqiangbiao 已提交
1759
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空                  |
P
panqiangbiao 已提交
1760 1761 1762 1763

**示例**

```
P
panqiangbiao 已提交
1764 1765 1766 1767 1768 1769
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1770
album.albumName = 'hello';
P
panqiangbiao 已提交
1771
album.commitModify((err) => {
P
panqiangbiao 已提交
1772 1773 1774 1775
   if (err) {
	   console.error('Failed ');
	   return;
   }
P
panqiangbiao 已提交
1776
   console.log('Modify successful.');
P
panqiangbiao 已提交
1777 1778 1779 1780 1781 1782 1783 1784 1785
})
```

## Album.commitModify

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

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

P
panqiangbiao 已提交
1786
**需要权限**:ohos.permission.READ_MEDIA,ohos.permission.MEDIA_WRITE
P
panqiangbiao 已提交
1787

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

P
panqiangbiao 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798
**返回值**

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

**示例**

```
P
panqiangbiao 已提交
1799 1800 1801 1802 1803 1804
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1805
album.albumName = 'hello';
P
panqiangbiao 已提交
1806 1807 1808 1809 1810
album.commitModify().then(function() {
    console.info("commitModify successfully");
}).catch(function(err){
    console.info("commitModify failed with error:"+ err);
});
P
panqiangbiao 已提交
1811 1812 1813 1814 1815 1816 1817 1818
```

## Album.getFileAssets

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

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

P
panqiangbiao 已提交
1819
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1820

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

P
panqiangbiao 已提交
1823 1824
**参数**

P
panqiangbiao 已提交
1825 1826 1827 1828
| 参数     | 类型                                                        | 必填 | 说明                              |
| -------- | ----------------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#MediaFetchOptions)                     | 是   | 媒体检索选项                      |
| callback | AsyncCallback<[FetchFileResult](#FetchFileResult.getCount)> | 是   | 异步返回FetchFileResult之后的回调 |
P
panqiangbiao 已提交
1829 1830 1831 1832

**示例**

```
P
panqiangbiao 已提交
1833 1834 1835 1836 1837 1838
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851
album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
})
function getFileAssetsCallBack(err, fetchFileResult) {
    // do something
}
```

## Album.getFileAssets

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

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

P
panqiangbiao 已提交
1852
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1853

P
panqiangbiao 已提交
1854
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1855 1856 1857

**参数**

P
panqiangbiao 已提交
1858 1859 1860
| 参数    | 类型                                    | 必填 | 说明         |
| ------- | --------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#MediaFetchOptions) | 否   | 媒体检索选项 |
P
panqiangbiao 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870

**返回值**

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

**示例**

```
P
panqiangbiao 已提交
1871 1872 1873 1874 1875 1876
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
const album = albumList[0];
P
panqiangbiao 已提交
1877 1878
album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
    console.info("getFileAssets successfully:"+ JSON.stringify(albumFetchFileResult));
P
panqiangbiao 已提交
1879 1880 1881
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
P
panqiangbiao 已提交
1882 1883
```

P
panqiangbiao 已提交
1884 1885
## PeerInfo

P
panqiangbiao 已提交
1886
注册设备信息。
P
panqiangbiao 已提交
1887

P
panqiangbiao 已提交
1888 1889
| 名称       | 类型       | 可读 | 可写 | 说明           |
| ---------- | ---------- | ---- | ---- | -------------- |
P
panqiangbiao 已提交
1890 1891
| deviceName | string     | 是   | 否   | 注册设备名称   |
| networkId  | string     | 是   | 否   | 注册设备网络ID |
P
panqiangbiao 已提交
1892 1893
| deviceType | DeviceType | 是   | 否   | 设备类型       |
| isOnline   | boolean    | 是   | 否   | 是否在线       |
P
panqiangbiao 已提交
1894 1895 1896 1897 1898 1899

FileAsset
---------

**属性:**

P
panqiangbiao 已提交
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922
| 名称         | 类型      | 可读 | 可写 | 说明                                                   |
| ------------ | --------- | ---- | ---- | ------------------------------------------------------ |
| id           | number    | 是   | 否   | 文件资源编号                                           |
| uri          | string    | 是   | 否   | 文件资源uri(如:dataability:///media/image/2)         |
| mimeType     | string    | 是   | 否   | 文件扩展属性                                           |
| mediaType    | MediaType | 是   | 否   | 媒体类型                                               |
| displayName  | string    | 是   | 是   | 显示文件名                                             |
| 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 已提交
1923 1924 1925

Album
---------
P
panqiangbiao 已提交
1926

P
panqiangbiao 已提交
1927 1928 1929 1930 1931 1932
实体相册。

**属性**

| 名称         | 参数型 | 可读 | 可写 | 说明           |
| ------------ | ------ | ---- | ---- | -------------- |
P
panqiangbiao 已提交
1933 1934
| albumId      | number | 是   | 否   | 相册编号       |
| albumName    | string | 是   | 是   | 相册名称       |
P
panqiangbiao 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
| albumUri     | string | 是   | 否   | 相册Uri        |
| dateModified | number | 是   | 否   | 修改日期       |
| count        | number | 是   | 否   | 相册中文件数量 |
| relativePath | string | 是   | 否   | 相对路径       |
| coverUri     | string | 是   | 否   | 封面文件Uri    |

MediaType
---------

枚举,媒体类型。

P
panqiangbiao 已提交
1946 1947 1948 1949 1950 1951
| 名称  | 默认值 | 描述 |
| ----- | ------ | ---- |
| FILE  | 0      | 文件 |
| IMAGE | 1      | 图片 |
| VIDEO | 2      | 视频 |
| AUDIO | 3      | 音频 |
P
panqiangbiao 已提交
1952 1953 1954 1955 1956 1957

FileKey
-------

枚举,文件关键信息。

P
panqiangbiao 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
| 名称          | 默认值              | 描述                                                   |
| ------------- | ------------------- |  ------------------------------------------------------ |
| 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 已提交
1979 1980 1981 1982 1983 1984

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

枚举,目录类型。

P
panqiangbiao 已提交
1985 1986 1987 1988 1989 1990 1991 1992
| 名称          | 默认值 | 描述               |
| ------------- | ------ | ------------------ |
| DIR_CAMERA    | 0      | 表示Camera文件路径 |
| DIR_VIDEO     | 1      | 表示视频路径       |
| DIR_IMAGE     | 2      | 表示图片路径       |
| DIR_AUDIO     | 3      | 表示音频路径       |
| DIR_DOCUMENTS | 4      | 表示文档路径       |
| DIR_DOWNLOAD  | 5      | 表示下载路径       |
P
panqiangbiao 已提交
1993 1994 1995 1996 1997 1998

DeviceType 
-----------

枚举,设备类型。

P
panqiangbiao 已提交
1999 2000 2001 2002 2003 2004 2005 2006 2007
| 名称         | 默认值 | 描述       |
| ------------ | ------ | ---------- |
| TYPE_UNKNOWN | 0      | 未识别设备 |
| TYPE_LAPTOP  | 1      | 笔记本电脑 |
| TYPE_PHONE   | 2      | 手机       |
| TYPE_TABLET  | 3      | 平板电脑   |
| TYPE_WATCH   | 4      | 智能手表   |
| TYPE_CAR     | 5      | 车载设备   |
| TYPE_TV      | 6      | 电视设备   |
P
panqiangbiao 已提交
2008 2009 2010 2011 2012

## MediaFetchOptions  

检索条件。

P
panqiangbiao 已提交
2013 2014 2015 2016 2017 2018 2019 2020
| 名称          | 类型          | 可读 | 可写 |  必填 |说明             |
| ------------- | ------------- | ---- | ---- |  ---- |---------------- |
| selections    | string        | 是   | 是   |是   | 检索条件         |
| selectionArgs | Array<string> | 是   | 是   |是   | 检索条件的值     |
| order         | string        | 是   | 是   | 否   |检索结果排序方式 |
| uri           | string        | 是   | 是   | 否   |文件URI          |
| networkId     | string        | 是   | 是   | 否   |注册设备网络ID   |
| extendArgs    | string        | 是   | 是   | 否   |扩展的检索参数   |
P
panqiangbiao 已提交
2021 2022 2023 2024 2025

## Size

图片尺寸。

P
panqiangbiao 已提交
2026 2027 2028 2029
| 名称   | 类型   | 可读 | 可写 | 说明             |
| ------ | ------ | ---- | ---- | ---------------- |
| width  | number | 是   | 是   | 宽(单位:像素) |
| height | number | 是   | 是   | 高(单位:像素) |
P
panqiangbiao 已提交
2030