js-apis-medialibrary.md 83.1 KB
Newer Older
W
wusongqing 已提交
1
# MediaLibrary
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5 6 7 8
> This component is supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version.

## Modules to Import
```
W
wusongqing 已提交
9
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
W
wusongqing 已提交
10 11
```

W
wusongqing 已提交
12
## mediaLibrary.getMediaLibrary<sup>8+</sup>
W
wusongqing 已提交
13 14 15

getMediaLibrary(context: Context): MediaLibrary

W
wusongqing 已提交
16
Obtains a **MediaLibrary** instance, which is used to access and modify personal media data such as audios, videos, images, and documents.
W
wusongqing 已提交
17 18 19 20 21

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
22 23 24
| Name | Type   | Mandatory| Description                      |
| ------- | ------- | ---- | -------------------------- |
| context | Context | Yes  | Context of the ability.|
W
wusongqing 已提交
25 26 27

**Return value**

W
wusongqing 已提交
28 29
| Type                           | Description   |
| ----------------------------- | :---- |
W
wusongqing 已提交
30 31
| [MediaLibrary](#medialibrary) | **MediaLibrary** instance.|

W
wusongqing 已提交
32 33 34 35 36 37 38
**Example (from API version 9)**

```
var media = mediaLibrary.getMediaLibrary(this.context);
```

**Example (API version 8)**
W
wusongqing 已提交
39 40 41 42 43 44 45

```
import featureAbility from '@ohos.ability.featureAbility';

var context = featureAbility.getContext()
var media = mediaLibrary.getMediaLibrary(context);
```
W
wusongqing 已提交
46 47 48 49 50 51
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

Obtains a **MediaLibrary** instance, which is used to access and modify personal media data such as audios, videos, images, and documents.

W
wusongqing 已提交
52 53 54
> **NOTE**
>
> This API is no longer maintained since API version 8. You are advised to use [mediaLibrary.getMediaLibrary<sup>8+</sup>](#medialibrarygetmedialibrary8) instead.
W
wusongqing 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

| Type                         | Description      |
| ----------------------------- | :--------- |
| [MediaLibrary](#medialibrary) | **MediaLibrary** instance.|

**Example**

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

W
wusongqing 已提交
70 71
## MediaLibrary

W
wusongqing 已提交
72
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
73 74 75 76 77 78 79 80 81 82 83 84


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

Obtains file assets (also called files). This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
85 86 87 88
| Name  | Type                                               | Mandatory| Description                             |
| -------- | --------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)            | Yes  | Options for fetching the files.                     |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes  | Asynchronous callback of **FetchFileResult**.|
W
wusongqing 已提交
89 90 91 92 93 94 95 96 97 98

**Example**

```
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
W
wusongqing 已提交
99
media.getFileAssets(imagesfetchOp, (error, fetchFileResult) => {
W
wusongqing 已提交
100 101 102 103
    if (fetchFileResult != undefined) {
        console.info('mediaLibraryTest : ASSET_CALLBACK fetchFileResult success');
        fetchFileResult.getAllObject((err, fileAssetList) => {
            if (fileAssetList != undefined) {
W
wusongqing 已提交
104 105 106
                fileAssetList.forEach(function(getAllObjectInfo){
                    console.info("getAllObjectInfo.displayName :" + getAllObjectInfo.displayName);
                });
W
wusongqing 已提交
107 108 109 110 111
            }
    	});
    }
});
```
W
wusongqing 已提交
112
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
113 114 115 116 117 118 119 120 121 122 123

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

Obtains file assets. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
124 125 126
| Name | Type                                    | Mandatory| Description        |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | Yes  | Options for fetching the files.|
W
wusongqing 已提交
127 128 129

**Return value**

W
wusongqing 已提交
130 131 132
| Type                                | Description          |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | Result set of the file retrieval operation.|
W
wusongqing 已提交
133 134 135 136 137 138 139 140 141 142

**Example**

```
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
W
wusongqing 已提交
143
media.getFileAssets(imagesfetchOp).then(function(fetchFileResult){
W
wusongqing 已提交
144
    console.info("getFileAssets successfully: image number is "+ fetchFileResult.getCount());
W
wusongqing 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
```

### on<sup>8+</sup>

on(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback: Callback&lt;void&gt;): void

Subscribes to the media library changes. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
160 161 162 163
| Name     | Type                  | Mandatory  | Description                                      |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | Yes   | Media type.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device|
| callback | callback&lt;void&gt; | Yes   | Void callback.                                   |
W
wusongqing 已提交
164 165 166 167

**Example**

```
W
wusongqing 已提交
168
media.on('imageChange', () => {
W
wusongqing 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181
    // image file had changed, do something
})
```
### off<sup>8+</sup>

off(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback?: Callback&lt;void&gt;): void

Unsubscribes from the media library changes. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
182 183 184 185
| Name     | Type                  | Mandatory  | Description                                      |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | Yes   | Media type.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device|
| callback | callback&lt;void&gt; | No   | Void callback.                                   |
W
wusongqing 已提交
186 187 188 189

**Example**

```
W
wusongqing 已提交
190
media.off('imageChange', () => {
W
wusongqing 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    // stop listening success
})
```

### createAsset <sup>8+</sup>

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

Creates a media asset. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
207 208 209 210 211 212
| Name      | Type                                   | Mandatory| Description                                                        |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | Yes  | Media type.                                                    |
| displayName  | string                                  | Yes  | Display file name.                                                  |
| relativePath | string                                  | Yes  | Path for storing the file. You can use [getPublicDirectory](#getpublicdirectory8) to obtain the paths for storing different types of files.|
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | Yes  | Asynchronous callback for **FileAsset**.                         |
W
wusongqing 已提交
213 214 215 216 217 218 219 220 221

**Example**

```
async function example() {
    // Create an image file in callback mode.
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
W
wusongqing 已提交
222
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
W
wusongqing 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
        if (fileAsset != undefined) {
            console.info('createAsset successfully, message = ' + err);
        } else {
            console.info('createAsset failed, message = ' + err);
        }
    });
}
```

### createAsset<sup>8+</sup>

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

Creates a media asset. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
244 245 246 247 248
| Name      | Type                    | Mandatory| Description                                                        |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | Yes  | Media type.                                                    |
| displayName  | string                   | Yes  | Display file name.                                                  |
| relativePath | string                   | Yes  | Relative path. You can use [getPublicDirectory](#getpublicdirectory8) to obtain the relative path of the level-1 directory of different types of media files.|
W
wusongqing 已提交
249 250 251

**Return value**

W
wusongqing 已提交
252 253 254
| Type                    | Description             |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | Media data (FileAsset).|
W
wusongqing 已提交
255 256 257 258 259

**Example**

```
async function example() {
W
wusongqing 已提交
260
    // Create an image file in promise mode.
W
wusongqing 已提交
261 262 263
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
W
wusongqing 已提交
264
    media.createAsset(mediaType, "image01.jpg", path + 'myPicture/').then (function (asset) {
W
wusongqing 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
        console.info("createAsset successfully:"+ JSON.stringify(asset));
    }).catch(function(err){
        console.info("createAsset failed with error:"+ err);
    });
}
```

### getPublicDirectory<sup>8+</sup>

getPublicDirectory(type: DirectoryType, callback: AsyncCallback&lt;string&gt;): void

Obtains a public directory. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
282 283 284 285
| Name  | Type                            | Mandatory| Description                     |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | Yes  | Type of the public directory.             |
| callback | AsyncCallback&lt;string&gt;      | Yes  | Callback used to return the public directory.|
W
wusongqing 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

**Example**

```
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => {
    if (dicResult == 'Camera/') {
        console.info('mediaLibraryTest : getPublicDirectory passed');
    } else {
        console.info('mediaLibraryTest : getPublicDirectory failed');
    }
});
```

### getPublicDirectory<sup>8+</sup>

getPublicDirectory(type: DirectoryType): Promise&lt;string&gt;

Obtains a public directory. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
310 311 312
| Name| Type                            | Mandatory| Description        |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | Yes  | Type of the public directory.|
W
wusongqing 已提交
313 314 315

**Return value**

W
wusongqing 已提交
316 317 318
| Type            | Description            |
| ---------------- | ---------------- |
| Promise\<string> | Promise used to return the public directory.|
W
wusongqing 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

**Example**

```
async function example() {
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
    const dicResult = await media.getPublicDirectory(DIR_CAMERA);
    if (dicResult == 'Camera/') {
        console.info('MediaLibraryTest : getPublicDirectory');
    } else {
        console.info('MediaLibraryTest : getPublicDirectory failed');
    }
}
```

W
wusongqing 已提交
334
### getAlbums<sup>7+</sup>
W
wusongqing 已提交
335 336 337 338 339 340 341 342 343 344 345

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

Obtains the albums. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
346 347 348 349
| Name  | Type                                        | Mandatory| Description                       |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | Yes  | Options for fetching the albums.               |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | Yes  | Callback used to return the albums.|
W
wusongqing 已提交
350 351 352 353 354 355 356 357

**Example**

```
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
W
wusongqing 已提交
358
media.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
W
wusongqing 已提交
359 360 361 362 363 364 365 366 367 368
    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);
     }
})
```

W
wusongqing 已提交
369
### getAlbums<sup>7+</sup>
W
wusongqing 已提交
370 371 372 373 374 375 376 377 378 379 380

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

Obtains the albums. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
381 382 383
| Name | Type                                    | Mandatory| Description        |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | Yes  | Options for fetching the albums.|
W
wusongqing 已提交
384 385 386

**Return value**

W
wusongqing 已提交
387 388 389
| Type                            | Description         |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | Promise used to return the albums.|
W
wusongqing 已提交
390 391 392 393 394 395 396 397

**Example**

```
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
W
wusongqing 已提交
398
media.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
W
wusongqing 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
    console.info("getAlbums failed with error:"+ err);
});
```

### release<sup>8+</sup>

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

Releases this **MediaLibrary** instance.
Call this API when you no longer need to use the APIs in the **MediaLibrary** instance.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
416 417 418
| Name     | Type                       | Mandatory  | Description        |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback used to return the execution result.|
W
wusongqing 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

**Example**

```
var media = mediaLibrary.getMediaLibrary(context);
media.release((err) => {
    // do something
});
```

### release<sup>8+</sup>

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

Releases this **MediaLibrary** instance.
Call this API when you no longer need to use the APIs in the **MediaLibrary** instance.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
440 441
| Type                 | Description                  |
| ------------------- | -------------------- |
W
wusongqing 已提交
442 443 444 445 446 447 448 449
| Promise&lt;void&gt; | Promise used to return the execution result.|

**Example**

```
media.release()
```

W
wusongqing 已提交
450
### storeMediaAsset<sup>(deprecated)</sup>
W
wusongqing 已提交
451 452 453 454 455

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

Stores a media asset. This API uses an asynchronous callback to return the URI that stores the media asset.

W
wusongqing 已提交
456 457 458
> **NOTE**
>
> This API is deprecated since API version 9.
W
wusongqing 已提交
459 460 461 462 463

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
464 465 466 467
| Name     | Type                                   | Mandatory  | Description                     |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | Yes   | Media asset option.                |
| callback | AsyncCallback&lt;string&gt;           | Yes   | Callback used to return the URI that stores the media asset.|
W
wusongqing 已提交
468 469 470 471 472

**Example**

  ```
let option = {
W
wusongqing 已提交
473 474 475
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
W
wusongqing 已提交
476 477 478 479 480 481
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (err, value) => {
    if (err) {
        console.log("An error occurred when storing the media asset.");
        return;
    }
W
wusongqing 已提交
482
    console.log("Media asset stored.");
W
wusongqing 已提交
483 484 485 486 487
    // Obtain the URI that stores the media asset.
});
  ```


W
wusongqing 已提交
488
### storeMediaAsset<sup>(deprecated)</sup>
W
wusongqing 已提交
489 490 491 492 493

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

Stores a media asset. This API uses a promise to return the URI that stores the media asset.

W
wusongqing 已提交
494 495 496
> **NOTE**
>
> This API is deprecated since API version 9.
W
wusongqing 已提交
497 498 499 500 501

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
502 503 504
| Name   | Type                                   | Mandatory  | Description     |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | Yes   | Media asset option.|
W
wusongqing 已提交
505 506 507

**Return value**

W
wusongqing 已提交
508 509
| Type                   | Description                          |
| --------------------- | ---------------------------- |
W
wusongqing 已提交
510 511 512 513 514 515
| Promise&lt;string&gt; | Promise used to return the URI that stores the media asset.|

**Example**

  ```
let option = {
W
wusongqing 已提交
516 517 518
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
W
wusongqing 已提交
519 520 521 522 523 524 525 526 527 528
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
    console.log("Media asset stored.");
    // Obtain the URI that stores the media asset.
}).catch((err) => {
    console.log("An error occurred when storing the media assets.");
});
  ```


W
wusongqing 已提交
529
### startImagePreview<sup>(deprecated)</sup>
W
wusongqing 已提交
530 531 532 533 534

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

Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **dataability://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result.

W
wusongqing 已提交
535 536 537
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
W
wusongqing 已提交
538 539 540 541 542

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
543 544
| Name     | Type                       | Mandatory  | Description                                      |
| -------- | ------------------------- | ---- | ---------------------------------------- |
W
wusongqing 已提交
545
| images   | Array&lt;string&gt;       | Yes   | URIs of the images to preview. The value can start with either **dataability://** or **https://**.|
W
wusongqing 已提交
546 547
| index    | number                    | Yes   | Index of the first image to preview.                              |
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback used to return the image preview result. If the preview fails, an error message is returned.                       |
W
wusongqing 已提交
548 549 550 551 552

**Example**

  ```
let images = [
W
wusongqing 已提交
553 554
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
W
wusongqing 已提交
555
];
W
wusongqing 已提交
556
/* Preview online images.
W
wusongqing 已提交
557 558 559 560
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
W
wusongqing 已提交
561
*/
W
wusongqing 已提交
562 563 564 565 566 567 568 569 570 571 572
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
  ```


W
wusongqing 已提交
573
### startImagePreview<sup>(deprecated)</sup>
W
wusongqing 已提交
574 575 576 577 578

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

Starts image preview. This API can be used to preview local images whose URIs start with **dataability://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result.

W
wusongqing 已提交
579 580 581
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **<Image\>** component can be used to render and display local images and network images.
W
wusongqing 已提交
582 583 584 585 586

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
587 588 589 590
| Name     | Type                       | Mandatory  | Description                                      |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| images   | Array&lt;string&gt;       | Yes   | URIs of the images to preview. The value can start with either **https://** or **dataability://**.|
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback used to return the image preview result. If the preview fails, an error message is returned.                       |
W
wusongqing 已提交
591 592 593 594 595

**Example**

  ```
let images = [
W
wusongqing 已提交
596 597
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
W
wusongqing 已提交
598
];
W
wusongqing 已提交
599
/* Preview online images.
W
wusongqing 已提交
600 601 602 603
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
W
wusongqing 已提交
604
*/
W
wusongqing 已提交
605 606 607 608 609 610 611 612 613 614
mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
  ```


W
wusongqing 已提交
615
### startImagePreview<sup>(deprecated)</sup>
W
wusongqing 已提交
616 617 618 619 620

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

Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with dataability:// or online images whose URIs start with https://. It uses a promise to return the execution result.

W
wusongqing 已提交
621 622 623
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **<Image\>** component can be used to render and display local images and network images.
W
wusongqing 已提交
624 625 626 627 628

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
629 630
| Name   | Type                 | Mandatory  | Description                                      |
| ------ | ------------------- | ---- | ---------------------------------------- |
W
wusongqing 已提交
631
| images | Array&lt;string&gt; | Yes   | URIs of the images to preview. The value can start with either **dataability://** or **https://**.|
W
wusongqing 已提交
632
| index  | number              | No   | Index of the first image to preview. If this parameter is not specified, the default value **0** is used.                     |
W
wusongqing 已提交
633 634 635

**Return value**

W
wusongqing 已提交
636 637
| Type                 | Description                             |
| ------------------- | ------------------------------- |
W
wusongqing 已提交
638 639 640 641 642 643
| Promise&lt;void&gt; | Promise used to return the image preview result. If the preview fails, an error message is returned.|

**Example**

  ```
let images = [
W
wusongqing 已提交
644 645
    "dataability:///media/xxxx/2",
    "dataability:///media/xxxx/3"
W
wusongqing 已提交
646
];
W
wusongqing 已提交
647
/* Preview online images.
W
wusongqing 已提交
648 649 650 651
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
W
wusongqing 已提交
652
*/
W
wusongqing 已提交
653 654 655 656 657 658 659 660 661
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
    console.log("Succeeded in previewing the images.");
}).catch((err) => {
    console.log("An error occurred when previewing the images.");
});
  ```


W
wusongqing 已提交
662
### startMediaSelect<sup>(deprecated)</sup>
W
wusongqing 已提交
663 664 665 666 667

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

Starts media selection. This API uses an asynchronous callback to return the list of URIs that store the selected media assets.

W
wusongqing 已提交
668 669 670
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
W
wusongqing 已提交
671 672 673 674 675

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
676 677 678 679
| Name     | Type                                      | Mandatory  | Description                                  |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
| option   | [MediaSelectOption](#mediaselectoption)  | Yes   | Media selection option.                             |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes   | Callback used to return the list of URIs (starting with **dataability://**) that store the selected media assets.|
W
wusongqing 已提交
680 681 682 683 684

**Example**

  ```
let option = {
W
wusongqing 已提交
685
    type : "media",
W
wusongqing 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
    if (err) {
        console.log("An error occurred when selecting the media asset.");
        return;
    }
    console.log("Media asset selected.");
    // Obtain the media selection value.
});
  ```


W
wusongqing 已提交
699
### startMediaSelect<sup>(deprecated)</sup>
W
wusongqing 已提交
700 701 702 703 704

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

Starts media selection. This API uses a promise to return the list of URIs that store the selected media assets.

W
wusongqing 已提交
705 706 707
> **NOTE**
>
> This API is deprecated since API version 9. You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
W
wusongqing 已提交
708 709 710 711 712

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
713 714 715
| Name   | Type                                     | Mandatory  | Description     |
| ------ | --------------------------------------- | ---- | ------- |
| option | [MediaSelectOption](#mediaselectoption) | Yes   | Media selection option.|
W
wusongqing 已提交
716 717 718

**Return value**

W
wusongqing 已提交
719 720
| Type                                | Description                                      |
| ---------------------------------- | ---------------------------------------- |
W
wusongqing 已提交
721 722 723 724 725 726
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the list of URIs (starting with **dataability://**) that store the selected media assets.|

**Example**

  ```
let option = {
W
wusongqing 已提交
727
    type : "media",
W
wusongqing 已提交
728 729 730 731 732 733 734 735 736 737 738
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
    console.log("Media asset selected.");
    // Obtain the media selection value.
}).catch((err) => {
    console.log("An error occurred when selecting the media assets.");
});

  ```

W
wusongqing 已提交
739
## FileAsset<sup>7+</sup>
W
wusongqing 已提交
740 741 742 743 744 745 746

Provides APIs for encapsulating file asset attributes.

### Attributes

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
747
| Name                     | Type                    | Readable| Writable| Description                                                  |
W
wusongqing 已提交
748
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
749
| id                        | number                   | Yes  | No  | File asset ID.                                          |
W
wusongqing 已提交
750
| uri                       | string                   | Yes  | No  | File asset URI, for example, **dataability:///media/image/2**.        |
W
wusongqing 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
| mimeType                  | string                   | Yes  | No  | Extended file attributes.                                          |
| mediaType<sup>8+</sup>    | [MediaType](#mediatype8) | Yes  | No  | Media type.                                              |
| displayName               | string                   | Yes  | Yes  | Display file name, including the file name extension.                                |
| title                     | string                   | Yes  | Yes  | Title in the file.                                              |
| relativePath<sup>8+</sup> | string                   | Yes  | Yes  | Relative public directory of the file.                                      |
| parent<sup>8+</sup>       | number                   | Yes  | No  | Parent directory ID.                                              |
| size                      | number                   | Yes  | No  | File size, in bytes.                                |
| dateAdded                 | number                   | Yes  | No  | Date when the file was added. (The value is the number of seconds elapsed since the Epoch time.)        |
| dateModified              | number                   | Yes  | No  | Date when the file was modified. (The value is the number of seconds elapsed since the Epoch time.)        |
| dateTaken                 | number                   | Yes  | No  | Date when the file (photo) was taken. (The value is the number of seconds elapsed since the Epoch time.)        |
| artist<sup>8+</sup>       | string                   | Yes  | No  | Artist of the file.                                                  |
| audioAlbum<sup>8+</sup>   | string                   | Yes  | No  | Audio album.                                                  |
| width                     | number                   | Yes  | No  | Image width, in pixels.                                |
| height                    | number                   | Yes  | No  | Image height, in pixels.                                |
| orientation               | number                   | Yes  | Yes  | Image display direction (clockwise rotation angle, for example, 0, 90, or 180, in degrees).|
W
wusongqing 已提交
766
| duration<sup>8+</sup>     | number                   | Yes  | No  | Duration, in ms.                                  |
W
wusongqing 已提交
767 768 769
| albumId                   | number                   | Yes  | No  | ID of the album to which the file belongs.                                  |
| albumUri<sup>8+</sup>     | string                   | Yes  | No  | URI of the album to which the file belongs.                                     |
| albumName                 | string                   | Yes  | No  | Name of the album to which the file belongs.                                    |
W
wusongqing 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783


### isDirectory<sup>8+</sup>

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

Checks whether this file asset is a directory. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
784
| Name     | Type                          | Mandatory  | Description                 |
W
wusongqing 已提交
785
| -------- | ---------------------------- | ---- | ------------------- |
W
wusongqing 已提交
786
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to return whether the file asset is a directory.|
W
wusongqing 已提交
787 788 789 790 791

**Example**

```
async function example() {
W
wusongqing 已提交
792
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
```

### isDirectory<sup>8+</sup>

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

Checks whether this file asset is a directory. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
820 821
| Type                    | Description                          |
| ---------------------- | ---------------------------- |
W
wusongqing 已提交
822 823 824 825 826 827
| Promise&lt;boolean&gt; | Promise used to return whether the file asset is a directory.|

**Example**

```
async function example() {
W
wusongqing 已提交
828
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory().then(function(isDirectory){
        console.info("isDirectory result:"+ isDirectory);
    }).catch(function(err){
        console.info("isDirectory failed with error:"+ err);
    });
}
```

### commitModify<sup>8+</sup>

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

W
wusongqing 已提交
850
Commits the modification in this file asset to the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
851 852 853 854 855 856 857

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
858 859 860
| Name     | Type                       | Mandatory  | Description   |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | Yes   | Void callback.|
W
wusongqing 已提交
861 862 863 864 865

**Example**

```
async function example() {
W
wusongqing 已提交
866
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.title = 'newtitle';
    asset.commitModify(() => {
        console.info('commitModify success');   
    });
}
```

### commitModify<sup>8+</sup>

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

W
wusongqing 已提交
887
Commits the modification in this file asset to the database. This API uses a promise to return the result.
W
wusongqing 已提交
888 889 890 891 892 893 894

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
895 896
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
897 898 899 900 901 902
| Promise&lt;void&gt; | Void promise.|

**Example**

```
async function example() {
W
wusongqing 已提交
903
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.title = 'newtitle';
    asset.commitModify();
}
```

### open<sup>8+</sup>

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

Opens this file asset. This API uses an asynchronous callback to return the result.

W
wusongqing 已提交
924 925 926
> **NOTE**
> 
> Currently, the write operations are mutually exclusive. After the write operation is complete, you must call **close** to release the resource.
W
wusongqing 已提交
927

W
wusongqing 已提交
928
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
929 930 931 932 933

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
934 935
| Name     | Type                         | Mandatory  | Description                                 |
| -------- | --------------------------- | ---- | ----------------------------------- |
W
wusongqing 已提交
936
| mode     | string                      | Yes   | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).|
W
wusongqing 已提交
937
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback used to return the file handle.                           |
W
wusongqing 已提交
938 939 940 941 942 943 944 945

**Example**

```
async function example() {
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
W
wusongqing 已提交
946
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
W
wusongqing 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
```

### open<sup>8+</sup>

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

Opens this file asset. This API uses a promise to return the result.

W
wusongqing 已提交
963 964 965
> **NOTE**
> 
> Currently, the write operations are mutually exclusive. After the write operation is complete, you must call **close** to release the resource.
W
wusongqing 已提交
966

W
wusongqing 已提交
967
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
968 969 970 971 972

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
973 974
| Name | Type    | Mandatory  | Description                                 |
| ---- | ------ | ---- | ----------------------------------- |
W
wusongqing 已提交
975
| mode | string | Yes   | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).|
W
wusongqing 已提交
976 977 978

**Return value**

W
wusongqing 已提交
979 980
| Type                   | Description           |
| --------------------- | ------------- |
W
wusongqing 已提交
981 982 983 984 985 986 987 988 989
| Promise&lt;number&gt; | Promise used to return the file handle.|

**Example**

```
async function example() {
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
W
wusongqing 已提交
990
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
W
wusongqing 已提交
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
            console.info('File err!' + err);
        });
}
```

### close<sup>8+</sup>

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

Closes this file asset. This API uses an asynchronous callback to return the result.

W
wusongqing 已提交
1007
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
1008 1009 1010 1011 1012

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1013 1014 1015 1016
| Name     | Type                       | Mandatory  | Description   |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | Yes   | File descriptor.|
| callback | AsyncCallback&lt;void&gt; | Yes   | Void callback.|
W
wusongqing 已提交
1017 1018 1019 1020 1021

**Example**

```
async function example() {
W
wusongqing 已提交
1022
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
W
wusongqing 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd, (closeErr) => {
            if (closeErr != undefined) {
                console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
                console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
W
wusongqing 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    });
}
```

### close<sup>8+</sup>

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

Closes this file asset. This API uses a promise to return the result.

W
wusongqing 已提交
1055
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
1056 1057 1058 1059 1060

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1061 1062 1063
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| fd   | number | Yes   | File descriptor.|
W
wusongqing 已提交
1064 1065 1066

**Return value**

W
wusongqing 已提交
1067 1068
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
1069 1070 1071 1072 1073 1074
| Promise&lt;void&gt; | Void promise.|

**Example**

```
async function example() {
W
wusongqing 已提交
1075
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
W
wusongqing 已提交
1085 1086 1087 1088 1089 1090
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd).then((closeErr) => {
            if (closeErr != undefined) {
                console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
                console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
W
wusongqing 已提交
1091

W
wusongqing 已提交
1092 1093 1094 1095 1096 1097 1098
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
W
wusongqing 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    });
}
```

### getThumbnail<sup>8+</sup>

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

Obtains the thumbnail of this file asset. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1115 1116 1117
| Name     | Type                                 | Mandatory  | Description              |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes   | Callback used to return the pixel map of the thumbnail.|
W
wusongqing 已提交
1118 1119 1120 1121 1122

**Example**

```
async function example() {
W
wusongqing 已提交
1123
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
    });
}
```

### getThumbnail<sup>8+</sup>

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

Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1151 1152 1153 1154
| Name     | Type                                 | Mandatory  | Description              |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | Yes   | Size of the thumbnail.           |
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes   | Callback used to return the pixel map of the thumbnail.|
W
wusongqing 已提交
1155 1156 1157 1158 1159

**Example**

```
async function example() {
W
wusongqing 已提交
1160
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1161 1162 1163 1164 1165 1166 1167
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
W
wusongqing 已提交
1168
    let size = { width: 720, height: 720 };
W
wusongqing 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
    });
}
```

### getThumbnail<sup>8+</sup>

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

Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1189 1190 1191
| Name | Type            | Mandatory  | Description   |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | No   | Size of the thumbnail.|
W
wusongqing 已提交
1192 1193 1194

**Return value**

W
wusongqing 已提交
1195 1196
| Type                           | Description                   |
| ----------------------------- | --------------------- |
W
wusongqing 已提交
1197 1198 1199 1200 1201 1202
| Promise&lt;image.PixelMap&gt; | Promise to return the pixel map of the thumbnail.|

**Example**

```
async function example() {
W
wusongqing 已提交
1203
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1204 1205
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
W
wusongqing 已提交
1206 1207 1208 1209
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
W
wusongqing 已提交
1210
    };
W
wusongqing 已提交
1211
    let size = { width: 720, height: 720 };
W
wusongqing 已提交
1212 1213
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
W
wusongqing 已提交
1214 1215
    asset.getThumbnail(size)
    .then((pixelmap) => {
W
wusongqing 已提交
1216
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
W
wusongqing 已提交
1217 1218 1219
    })
    .catch((err) => {
        console.info('mediaLibraryTest : getThumbnail fail'+ err);
W
wusongqing 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
    });
}
```

### favorite<sup>8+</sup>

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

Favorites or unfavorites this file asset. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1236 1237 1238 1239
| Name       | Type                       | Mandatory  | Description                                |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | Yes   | Whether to favorite or unfavorite the file. The value **true** means to favorite the file, and **false** means to unfavorite the file.|
| callback   | AsyncCallback&lt;void&gt; | Yes   | Void callback.                             |
W
wusongqing 已提交
1240 1241 1242 1243 1244

**Example**

```
async function example() {
W
wusongqing 已提交
1245
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
```

### favorite<sup>8+</sup>

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

Favorites or unfavorites this file asset. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1273 1274 1275
| Name       | Type     | Mandatory  | Description                                |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | Yes   | Whether to favorite or unfavorite the file. The value **true** means to favorite the file, and **false** means to unfavorite the file.|
W
wusongqing 已提交
1276 1277 1278

**Return value**

W
wusongqing 已提交
1279 1280
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
1281 1282 1283 1284 1285 1286
| Promise&lt;void&gt; | Void promise.|

**Example**

```
async function example() {
W
wusongqing 已提交
1287
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 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 + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true).then(function() {
        console.info("favorite successfully");
    }).catch(function(err){
        console.info("favorite failed with error:"+ err);
    });
}
```

### isFavorite<sup>8+</sup>

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

Checks whether this file asset is favorited. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1317 1318 1319
| Name     | Type                          | Mandatory  | Description         |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to return whether the file asset is favorited.|
W
wusongqing 已提交
1320 1321 1322 1323 1324

**Example**

```
async function example() {
W
wusongqing 已提交
1325
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite((err, isFavorite) => {
        if (isFavorite) {
            console.info('FileAsset is favorite');
        }else{
            console.info('FileAsset is not favorite');
        }
    });
}
```

### isFavorite<sup>8+</sup>

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

Checks whether this file asset is favorited. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1357 1358
| Type                    | Description                |
| ---------------------- | ------------------ |
W
wusongqing 已提交
1359 1360 1361 1362 1363 1364
| Promise&lt;boolean&gt; | Promise used to return whether the file asset is favorited.|

**Example**

```
async function example() {
W
wusongqing 已提交
1365
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite().then(function(isFavorite){
        console.info("isFavorite result:"+ isFavorite);
    }).catch(function(err){
        console.info("isFavorite failed with error:"+ err);
    });
}
```

### trash<sup>8+</sup>

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

Moves this file asset to the trash. This API uses an asynchronous callback to return the result.

Files in the trash are not actually deleted. You can set **isTrash** to **false** to restore the files from the trash.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1397 1398 1399 1400
| Name     | Type                       | Mandatory  | Description       |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | Yes   | Whether to move the file asset to the trash. The value **true** means to move the file asset to the trash, and **false** means the opposite.|
| callback | AsyncCallback&lt;void&gt; | Yes   | Void callback.    |
W
wusongqing 已提交
1401 1402 1403 1404 1405

**Example**

```
async function example() {
W
wusongqing 已提交
1406
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
    }
}
```

### trash<sup>8+</sup>

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

Moves this file asset to the trash. This API uses a promise to return the result.

Files in the trash are not actually deleted. You can set **isTrash** to **false** to restore the files from the trash.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1437 1438 1439
| Name    | Type     | Mandatory  | Description       |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | Yes   | Whether to move the file asset to the trash. The value **true** means to move the file asset to the trash, and **false** means the opposite.|
W
wusongqing 已提交
1440 1441 1442

**Return value**

W
wusongqing 已提交
1443 1444
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
1445 1446 1447 1448 1449 1450
| Promise&lt;void&gt; | Void promise.|

**Example**

```
async function example() {
W
wusongqing 已提交
1451
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true).then(function() {
        console.info("trash successfully");
    }).catch(function(err){
        console.info("trash failed with error:"+ err);
    });
}
```

### isTrash<sup>8+</sup>

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

Checks whether this file asset is in the trash. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1481 1482 1483
| Name     | Type                          | Mandatory  | Description             |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to return whether the file asset is in the trash. If the file asset is in the trash, **true** will be returned; otherwise, **false** will be returned.|
W
wusongqing 已提交
1484 1485 1486 1487 1488

**Example**

```
async function example() {
W
wusongqing 已提交
1489
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash(isTrashCallBack);
    function isTrashCallBack(err, isTrash) {
            if (isTrash == true) {
                console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK isTrash = ' + isTrash);
W
wusongqing 已提交
1503
                asset.trash(true, istrashCallBack);
W
wusongqing 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525

            } else {
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');

            }
    }
}
```

### isTrash<sup>8+</sup>

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

Checks whether this file asset is in the trash. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1526 1527
| Type                 | Description                  |
| ------------------- | -------------------- |
W
wusongqing 已提交
1528 1529 1530 1531 1532 1533
| Promise&lt;void&gt; | Promise used to return whether the file asset is in the trash. If the file asset is in the trash, **true** will be returned; otherwise, **false** will be returned.|

**Example**

```
async function example() {
W
wusongqing 已提交
1534
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash().then(function(isTrash){
        console.info("isTrash result:"+ isTrash);
    }).catch(function(err){
        console.info("isTrash failed with error:"+ err);
    });
}
```

W
wusongqing 已提交
1552
## FetchFileResult<sup>7+</sup>
W
wusongqing 已提交
1553 1554 1555

Implements the result set of the file retrieval operation.

W
wusongqing 已提交
1556
### getCount<sup>7+</sup>
W
wusongqing 已提交
1557 1558 1559 1560 1561 1562 1563 1564 1565

getCount(): number

Obtains the total number of files in the result set.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1566 1567
| Type    | Description      |
| ------ | -------- |
W
wusongqing 已提交
1568 1569 1570 1571 1572 1573
| number | Total number of files.|

**Example**

```
async function example() {
W
wusongqing 已提交
1574 1575
    let fileKeyObj = mediaLibrary.FileKey
    let fileType = mediaLibrary.MediaType.FILE;
W
wusongqing 已提交
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
```

W
wusongqing 已提交
1587
### isAfterLast<sup>7+</sup>
W
wusongqing 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596

isAfterLast(): boolean

Checks whether the cursor is in the last row of the result set.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1597 1598
| Type     | Description                                |
| ------- | ---------------------------------- |
W
wusongqing 已提交
1599
| boolean | Returns **true** if the cursor is in the last row of the result set; returns *false* otherwise.|
W
wusongqing 已提交
1600 1601 1602 1603 1604

**Example**

```
async function example() {
W
wusongqing 已提交
1605
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    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();

            }
    }
}
```

W
wusongqing 已提交
1631
### close<sup>7+</sup>
W
wusongqing 已提交
1632 1633 1634

close(): void

W
wusongqing 已提交
1635
Releases and invalidates this **FetchFileResult** instance. Other APIs in this instance cannot be invoked after it is released.
W
wusongqing 已提交
1636 1637 1638 1639 1640 1641 1642

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Example**

```
async function example() {
W
wusongqing 已提交
1643
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
```

W
wusongqing 已提交
1656
### getFirstObject<sup>7+</sup>
W
wusongqing 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665

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

Obtains the first file asset in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1666 1667 1668
| Name  | Type                                         | Mandatory| Description                                       |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes  | Callback used to return the first file asset.|
W
wusongqing 已提交
1669 1670 1671 1672 1673

**Example**

```
async function example() {
W
wusongqing 已提交
1674
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1675 1676 1677 1678 1679 1680 1681 1682
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
W
wusongqing 已提交
1683
    fetchFileResult.getFirstObject((err, fileAsset) => {
W
wusongqing 已提交
1684 1685 1686 1687
       if (err) {
           console.error('Failed ');
           return;
       }
W
wusongqing 已提交
1688
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
W
wusongqing 已提交
1689 1690 1691 1692
    })
}
```

W
wusongqing 已提交
1693
### getFirstObject<sup>7+</sup>
W
wusongqing 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702

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

Obtains the first file asset in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1703 1704 1705
| Type                                   | Description                      |
| --------------------------------------- | -------------------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the file asset.|
W
wusongqing 已提交
1706 1707 1708 1709 1710

**Example**

```
async function example() {
W
wusongqing 已提交
1711
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getFirstObject().then(function(fileAsset){
        console.info("getFirstObject successfully:"+ JSON.stringify(fileAsset));
    }).catch(function(err){
        console.info("getFirstObject failed with error:"+ err);
    });
}
```

W
wusongqing 已提交
1728
### getNextObject<sup>7+</sup>
W
wusongqing 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737

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

Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1738 1739 1740
| Name   | Type                                         | Mandatory| Description                                     |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes  | Callback used to return the next file asset.|
W
wusongqing 已提交
1741 1742 1743 1744 1745

**Example**

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

W
wusongqing 已提交
1765
### getNextObject<sup>7+</sup>
W
wusongqing 已提交
1766 1767 1768 1769 1770 1771 1772 1773 1774

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

Obtains the next file asset in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1775 1776 1777
| Type                                   | Description             |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.|
W
wusongqing 已提交
1778 1779 1780 1781 1782

**Example**

```
async function example() {
W
wusongqing 已提交
1783
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
    console.info('mediaLibraryTest : count:' + fetchCount);
W
wusongqing 已提交
1794
    let fileAsset = await fetchFileResult.getNextObject();
W
wusongqing 已提交
1795 1796 1797
}
```

W
wusongqing 已提交
1798
### getLastObject<sup>7+</sup>
W
wusongqing 已提交
1799 1800 1801 1802 1803 1804 1805 1806 1807

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

Obtains the last file asset in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1808 1809 1810
| Name  | Type                                         | Mandatory| Description                       |
| -------- | --------------------------------------------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes  | Callback used to return the last file asset.|
W
wusongqing 已提交
1811 1812 1813 1814 1815

**Example**

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

W
wusongqing 已提交
1835
### getLastObject<sup>7+</sup>
W
wusongqing 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844

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

Obtains the last file asset in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1845 1846 1847
| Type                                   | Description             |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.|
W
wusongqing 已提交
1848 1849 1850 1851 1852

**Example**

```
async function example() {
W
wusongqing 已提交
1853
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
```

W
wusongqing 已提交
1866
### getPositionObject<sup>7+</sup>
W
wusongqing 已提交
1867 1868 1869 1870 1871 1872 1873 1874 1875

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

Obtains a file asset with the specified index in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1876 1877 1878
| Name      | Type                                      | Mandatory  | Description                |
| -------- | ---------------------------------------- | ---- | ------------------ |
| index    | number                                   | Yes   | Index of the file asset to obtain. The value starts from **0**.    |
W
wusongqing 已提交
1879
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes   | Callback used to return the last file asset.|
W
wusongqing 已提交
1880 1881 1882 1883 1884

**Example**

```
async function example() {
W
wusongqing 已提交
1885
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1886 1887 1888 1889 1890 1891 1892 1893
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
W
wusongqing 已提交
1894
    fetchFileResult.getPositionObject(0, (err, fileAsset) => {
W
wusongqing 已提交
1895 1896 1897 1898
       if (err) {
           console.error('Failed ');
           return;
       }
W
wusongqing 已提交
1899
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
W
wusongqing 已提交
1900 1901 1902 1903
    })
}
```

W
wusongqing 已提交
1904
### getPositionObject<sup>7+</sup>
W
wusongqing 已提交
1905 1906 1907 1908 1909 1910 1911 1912 1913

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

Obtains a file asset with the specified index in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1914 1915 1916
| Name   | Type    | Mandatory  | Description            |
| ----- | ------ | ---- | -------------- |
| index | number | Yes   | Index of the file asset to obtain. The value starts from **0**.|
W
wusongqing 已提交
1917 1918 1919

**Return value**

W
wusongqing 已提交
1920 1921 1922
| Type                                   | Description             |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.|
W
wusongqing 已提交
1923 1924 1925 1926 1927

**Example**

```
async function example() {
W
wusongqing 已提交
1928
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1929 1930 1931 1932 1933 1934 1935 1936
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
W
wusongqing 已提交
1937
    fetchFileResult.getPositionObject(1, (err, fileAsset) => {
W
wusongqing 已提交
1938 1939 1940 1941
       if (err) {
           console.error('Failed ');
           return;
       }
W
wusongqing 已提交
1942
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
W
wusongqing 已提交
1943 1944 1945 1946
    })
}
```

W
wusongqing 已提交
1947
### getAllObject<sup>7+</sup>
W
wusongqing 已提交
1948 1949 1950 1951 1952 1953 1954 1955 1956

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

Obtains all the file assets in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1957 1958
| Name      | Type                                      | Mandatory  | Description                  |
| -------- | ---------------------------------------- | ---- | -------------------- |
W
wusongqing 已提交
1959
| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | Yes   | Callback used to return the file assets.|
W
wusongqing 已提交
1960 1961 1962 1963 1964

**Example**

```
async function example() {
W
wusongqing 已提交
1965
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
1966 1967 1968 1969 1970 1971 1972 1973
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
W
wusongqing 已提交
1974
    fetchFileResult.getAllObject((err, fileAsset) => {
W
wusongqing 已提交
1975 1976 1977 1978
       if (err) {
           console.error('Failed ');
           return;
       }
W
wusongqing 已提交
1979
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
W
wusongqing 已提交
1980 1981 1982 1983
    })
}
```

W
wusongqing 已提交
1984
### getAllObject<sup>7+</sup>
W
wusongqing 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993

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

Obtains all the file assets in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1994 1995 1996
| Type                                    | Description                 |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | Promise used to return the file assets.|
W
wusongqing 已提交
1997 1998 1999 2000 2001

**Example**

```
async function example() {
W
wusongqing 已提交
2002
    let fileKeyObj = mediaLibrary.FileKey
W
wusongqing 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
```

W
wusongqing 已提交
2015
## Album<sup>7+</sup>
W
wusongqing 已提交
2016 2017 2018

Provides APIs to implement a physical album.

W
wusongqing 已提交
2019
### Attributes
W
wusongqing 已提交
2020 2021 2022

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
2023 2024
| Name          | Type   | Readable  | Writable  | Description     |
| ------------ | ------ | ---- | ---- | ------- |
W
wusongqing 已提交
2025 2026 2027
| albumId | number | Yes   | No   | Album ID.   |
| albumName | string | Yes   | Yes   | Album name.   |
| albumUri<sup>8+</sup> | string | Yes   | No   | Album URI.  |
W
wusongqing 已提交
2028
| dateModified | number | Yes   | No   | Date when the album was modified.   |
W
wusongqing 已提交
2029 2030 2031
| count<sup>8+</sup> | number | Yes   | No   | Number of files in the album.|
| relativePath<sup>8+</sup> | string | Yes   | No   | Relative path of the album.   |
| coverUri<sup>8+</sup> | string | Yes   | No   | URI of the cover file of the album.|
W
wusongqing 已提交
2032 2033 2034 2035 2036

### commitModify<sup>8+</sup>

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

W
wusongqing 已提交
2037
Commits the modification in the album attributes to the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2038 2039 2040 2041 2042 2043 2044

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2045 2046 2047
| Name  | Type                     | Mandatory| Description      |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes  | Void callback.|
W
wusongqing 已提交
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073

**Example**

```
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify((err) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log('Modify successful.');
    })
}
```

### commitModify<sup>8+</sup>

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

W
wusongqing 已提交
2074
Commits the modification in the album attributes to the database. This API uses a promise to return the result.
W
wusongqing 已提交
2075 2076 2077 2078 2079 2080 2081

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
2082 2083
| Type                 | Description          |
| ------------------- | ------------ |
W
wusongqing 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
| Promise&lt;void&gt; | Void promise.|

**Example**

```
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify().then(function() {
        console.info("commitModify successfully");
    }).catch(function(err){
        console.info("commitModify failed with error:"+ err);
    });
}
```

W
wusongqing 已提交
2105
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116

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

Obtains the file assets in this album. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2117 2118 2119 2120
| Name  | Type                                               | Mandatory| Description                               |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)            | Yes  | Options for fetching the files.                     |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes  | Callback used to return the file assets.|
W
wusongqing 已提交
2121 2122 2123 2124 2125 2126 2127 2128 2129

**Example**

```
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
W
wusongqing 已提交
2130 2131 2132 2133
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
W
wusongqing 已提交
2134 2135 2136 2137 2138 2139 2140 2141 2142
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
}
```

W
wusongqing 已提交
2143
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154

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

Obtains the file assets in this album. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2155 2156 2157
| Name | Type                                    | Mandatory| Description          |
| ------- | ---------------------------------------- | ---- | -------------- |
| options | [MediaFetchOptions](#mediafetchoptions7) | No  | Options for fetching the files.|
W
wusongqing 已提交
2158 2159 2160

**Return value**

W
wusongqing 已提交
2161 2162 2163
| Type                                         | Description                     |
| --------------------------------------------- | ------------------------- |
| Promise<[FetchFileResult](#fetchfileresult7)> | Promise used to return the file assets.|
W
wusongqing 已提交
2164 2165 2166 2167 2168 2169 2170 2171 2172

**Example**

```
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
W
wusongqing 已提交
2173 2174 2175 2176
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
W
wusongqing 已提交
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
        console.info("getFileAssets successfully:"+ JSON.stringify(albumFetchFileResult));
    }).catch(function(err){
        console.info("getFileAssets failed with error:"+ err);
    });
}
```

## PeerInfo<sup>8+</sup>

Describes information about a registered device.
W
wusongqing 已提交
2190
This is a system API.
W
wusongqing 已提交
2191

W
wusongqing 已提交
2192
**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore
W
wusongqing 已提交
2193

W
wusongqing 已提交
2194 2195 2196 2197 2198 2199
| Name      | Type                      | Readable| Writable| Description            |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | Yes  | No  | Name of the registered device.  |
| networkId  | string                     | Yes  | No  | Network ID of the registered device.|
| deviceType | [DeviceType](#devicetype8) | Yes  | No  | Type of the registered device.        |
| isOnline   | boolean                    | Yes  | No  | Whether the registered device is online.        |
W
wusongqing 已提交
2200 2201 2202



W
wusongqing 已提交
2203
## MediaType<sup>8+</sup>
W
wusongqing 已提交
2204 2205 2206 2207 2208

Enumerates media types.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
2209 2210 2211 2212 2213 2214
| Name |  Description|
| ----- |  ---- |
| FILE  |  File.|
| IMAGE |  Image.|
| VIDEO |  Video.|
| AUDIO |  Audio.|
W
wusongqing 已提交
2215

W
wusongqing 已提交
2216
## FileKey<sup>8+</sup>
W
wusongqing 已提交
2217 2218 2219 2220 2221

Enumerates key file information.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
2222
| Name         | Default Value             | Description                                                      |
W
wusongqing 已提交
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
| ------------- | ------------------- | ---------------------------------------------------------- |
| ID            | file_id             | File ID.                                                  |
| RELATIVE_PATH | relative_path       | Relative public directory of the file.                                          |
| DISPLAY_NAME  | display_name        | Display file name.                                                  |
| PARENT        | parent              | Parent directory ID.                                                  |
| MIME_TYPE     | mime_type           | Extended file attributes.                                              |
| MEDIA_TYPE    | media_type          | Media type.                                                  |
| SIZE          | size                | File size, in bytes.                                    |
| DATE_ADDED    | date_added          | Date when the file was added. (The value is the number of seconds elapsed since the Epoch time.)            |
| DATE_MODIFIED | date_modified       | Date when the file was modified. (The value is the number of seconds elapsed since the Epoch time.)            |
| DATE_TAKEN    | date_taken          | Date when the file (photo) was taken. (The value is the number of seconds elapsed since the Epoch time.)            |
| TITLE         | title               | Title in the file.                                                  |
| ARTIST        | artist              | Artist of the file.                                                      |
| AUDIOALBUM    | audio_album         | Audio album.                                                      |
W
wusongqing 已提交
2237
| DURATION      | duration            | Duration, in ms.                                      |
W
wusongqing 已提交
2238 2239
| WIDTH         | width               | Image width, in pixels.                                    |
| HEIGHT        | height              | Image height, in pixels.                                    |
W
wusongqing 已提交
2240
| ORIENTATION   | orientation         | Image display direction (clockwise rotation angle, for example, 0, 90, and 180, in degrees).|
W
wusongqing 已提交
2241 2242
| ALBUM_ID      | bucket_id           | ID of the album to which the file belongs.                                      |
| ALBUM_NAME    | bucket_display_name | Name of the album to which the file belongs.                                        |
W
wusongqing 已提交
2243

W
wusongqing 已提交
2244
## DirectoryType<sup>8+</sup>
W
wusongqing 已提交
2245 2246 2247 2248 2249

Enumerates directory types.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
2250 2251 2252 2253 2254 2255 2256 2257
| Name         |  Description              |
| ------------- |  ------------------ |
| DIR_CAMERA    |  Directory of camera files.|
| DIR_VIDEO     |  Directory of video files.      |
| DIR_IMAGE     |  Directory of image files.      |
| DIR_AUDIO     |  Directory of audio files.      |
| DIR_DOCUMENTS |  Directory of documents.      |
| DIR_DOWNLOAD  |  Download directory.      |
W
wusongqing 已提交
2258

W
wusongqing 已提交
2259
## DeviceType<sup>8+</sup>
W
wusongqing 已提交
2260 2261

Enumerates device types.
W
wusongqing 已提交
2262
This is a system API.
W
wusongqing 已提交
2263

W
wusongqing 已提交
2264
**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore
W
wusongqing 已提交
2265

W
wusongqing 已提交
2266 2267 2268 2269 2270 2271 2272 2273 2274
| Name        |  Description      |
| ------------ |  ---------- |
| TYPE_UNKNOWN |  Unknown.|
| TYPE_LAPTOP  |  Laptop.|
| TYPE_PHONE   |  Phone.      |
| TYPE_TABLET  |  Tablet.  |
| TYPE_WATCH   |  Smart watch.  |
| TYPE_CAR     |  Vehicle-mounted device.  |
| TYPE_TV      |  TV.  |
W
wusongqing 已提交
2275

W
wusongqing 已提交
2276
## MediaFetchOptions<sup>7+</sup>
W
wusongqing 已提交
2277 2278 2279 2280 2281

Describes options for fetching media files.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
2282 2283 2284 2285
| Name                   | Type               | Readable| Writable| Mandatory| Description                                                        |
| ----------------------- | ------------------- | ---- | ---- | ---- | ------------------------------------------------------------ |
| selections              | string              | Yes  | Yes  | Yes  | Conditions for fetching files. The enumerated values in [FileKey](#filekey8) are used as the column names of the conditions. Example:<br>selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR' +mediaLibrary.FileKey.MEDIA_TYPE + '= ?',|
| selectionArgs           | Array&lt;string&gt; | Yes  | Yes  | Yes  | Value of the condition, which corresponds to the value of the condition column in **selections**.<br>Example:<br>selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
W
wusongqing 已提交
2286
| order                   | string              | Yes  | Yes  | No  | Sorting mode of the search results, which can be ascending or descending. The enumerated values in [FileKey](#filekey8) are used as the columns for sorting the search results. Example:<br>Ascending: order: mediaLibrary.FileKey.DATE_ADDED + " AESC"<br>Descending: order: mediaLibrary.FileKey.DATE_ADDED + " DESC"|
W
wusongqing 已提交
2287 2288 2289
| uri<sup>8+</sup>        | string              | Yes  | Yes  | No  | File URI.                                                     |
| networkId<sup>8+</sup>  | string              | Yes  | Yes  | No  | Network ID of the registered device.                                              |
| extendArgs<sup>8+</sup> | string              | Yes  | Yes  | No  | Extended parameters for fetching the files. Currently, no extended parameters are available.                        |
W
wusongqing 已提交
2290 2291 2292 2293

## Size<sup>8+</sup>

Describes the image size.
W
wusongqing 已提交
2294
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core
W
wusongqing 已提交
2295

W
wusongqing 已提交
2296 2297 2298 2299
| Name    | Type    | Readable  | Writable  | Description      |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | Yes   | Yes   | Image width, in pixels.|
| height | number | Yes   | Yes   | Image height, in pixels.|
W
wusongqing 已提交
2300

W
wusongqing 已提交
2301
## MediaAssetOption<sup>(deprecated)</sup>
W
wusongqing 已提交
2302 2303 2304

Implements the media asset option.

W
wusongqing 已提交
2305 2306 2307
> **NOTE**
>
> This API is deprecated since API version 9.
W
wusongqing 已提交
2308 2309 2310 2311

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core


W
wusongqing 已提交
2312 2313 2314 2315 2316
| Name        | Type  | Mandatory| Description                                                        |
| ------------ | ------ | ---- | ------------------------------------------------------------ |
| src          | string | Yes  | Application sandbox oath of the local file.                                      |
| mimeType     | string | Yes  | Multipurpose Internet Mail Extensions (MIME) type of the media.<br>The value can be 'image/\*', 'video/\*', 'audio/\*' or 'file\*'.|
| relativePath | string | No  | Custom path for storing media assets, for example, 'Pictures/'. If this parameter is unspecified, media assets are stored in the default path.<br> Default path of images: 'Pictures/'<br> Default path of videos: 'Videos/'<br> Default path of audios: 'Audios/'<br> Default path of files: 'Documents/'|
W
wusongqing 已提交
2317

W
wusongqing 已提交
2318
## MediaSelectOption<sup>(deprecated)</sup>
W
wusongqing 已提交
2319 2320 2321

Describes media selection option.

W
wusongqing 已提交
2322 2323 2324
> **NOTE**
>
> This API is deprecated since API version 9.
W
wusongqing 已提交
2325 2326 2327

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
2328 2329
| Name   | Type    | Mandatory  | Description                  |
| ----- | ------ | ---- | -------------------- |
W
wusongqing 已提交
2330
| type  | string | Yes   | Media type, which can be **image**, **media**, or **video**. Currently, only **media** is supported.|
W
wusongqing 已提交
2331
| count | number | Yes   | Number of media assets selected. The value starts from 1, which indicates that one media asset can be selected.           |