js-apis-medialibrary.md 91.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# 媒体库管理

P
panqiangbiao 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
P
panqiangbiao 已提交
5

Z
zengyawen 已提交
6
## 导入模块
7
```js
8
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
P
panqiangbiao 已提交
9 10
```

Z
zengyawen 已提交
11
## mediaLibrary.getMediaLibrary<sup>8+</sup>
P
panqiangbiao 已提交
12

P
panqiangbiao 已提交
13
getMediaLibrary(context: Context): MediaLibrary
P
panqiangbiao 已提交
14

Z
zengyawen 已提交
15
获取媒体库的实例,用于访问和修改用户等个人媒体数据信息(如音频、视频、图片、文档等)。
P
panqiangbiao 已提交
16

W
wangqinxiao 已提交
17 18
此接口仅可在Stage模型下使用。

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

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

Z
zengyawen 已提交
23 24 25
| 参数名  | 类型    | 必填 | 说明                       |
| ------- | ------- | ---- | -------------------------- |
| context | Context | 是   | 传入Ability实例的Context。 |
P
panqiangbiao 已提交
26 27 28

**返回值:**

H
HelloCrease 已提交
29 30
| 类型                            | 说明    |
| ----------------------------- | :---- |
P
panqiangbiao 已提交
31
| [MediaLibrary](#medialibrary) | 媒体库实例 |
P
panqiangbiao 已提交
32

潘强标 已提交
33 34
**示例:(从API Version 9开始)**

35
```ts
36 37
const context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
潘强标 已提交
38 39 40
```

**示例:(API Version 8)**
P
panqiangbiao 已提交
41

42
```js
P
panqiangbiao 已提交
43
import featureAbility from '@ohos.ability.featureAbility';
P
panqiangbiao 已提交
44

45
let context = featureAbility.getContext();
46
let media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
47
```
48

Z
zengyawen 已提交
49 50 51 52 53 54
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

获取媒体库的实例,用于访问和修改用户等个人媒体数据信息(如音频、视频、图片、文档等)。

W
wangqinxiao 已提交
55 56
此接口仅可在FA模型下使用。

Z
zengyawen 已提交
57 58 59 60 61 62 63 64 65 66 67
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core

**返回值:**

| 类型                          | 说明       |
| ----------------------------- | :--------- |
| [MediaLibrary](#medialibrary) | 媒体库实例 |

**示例:**

```js
68
let media = mediaLibrary.getMediaLibrary();
Z
zengyawen 已提交
69 70
```

P
panqiangbiao 已提交
71
## MediaLibrary
P
panqiangbiao 已提交
72

Z
zengyawen 已提交
73
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
74 75


P
panqiangbiao 已提交
76
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 
P
panqiangbiao 已提交
77 78 79

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

P
panqiangbiao 已提交
80
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
81

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

P
panqiangbiao 已提交
84 85
**参数:**

Z
zengyawen 已提交
86 87 88 89
| 参数名   | 类型                                                | 必填 | 说明                              |
| -------- | --------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 文件获取选项                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步获取FetchFileResult之后的回调 |
P
panqiangbiao 已提交
90 91 92

**示例:**

93
```js
94 95
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
Z
zhang-daiyue 已提交
96
let imagesFetchOp = {
P
panqiangbiao 已提交
97 98 99
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
Z
zhang-daiyue 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
    if (fetchFileResult == undefined) {
        console.error('Failed to get fetchFileResult: ' + error);
        return;
    }
    const count = fetchFileResult.getCount();
    if (count < 0) {
        console.error('Failed to get count from fetchFileResult: count: ' + count);
        return;
    }
    if (count == 0) {
        console.info('The count of fetchFileResult is zero');
        return;
    }

    console.info('Get fetchFileResult success, count: ' + count);
    fetchFileResult.getFirstObject((err, fileAsset) => {
        if (fileAsset == undefined) {
            console.error('Failed to get first object: ' + err);
            return;
        }
        console.log('fileAsset.displayName ' + ': ' + fileAsset.displayName);
        for (let i = 1; i < count; i++) {
123
            fetchFileResult.getNextObject((err, fileAsset) => {
Z
zhang-daiyue 已提交
124 125 126 127 128
                if (fileAsset == undefined) {
                    console.error('Failed to get next object: ' + err);
                    return;
                }
                console.log('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
129
            })
Z
zhang-daiyue 已提交
130 131
        }
    });
P
panqiangbiao 已提交
132
});
P
panqiangbiao 已提交
133
```
Z
zengyawen 已提交
134
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
135

P
panqiangbiao 已提交
136
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
137 138 139

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

P
panqiangbiao 已提交
140
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
141

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

P
panqiangbiao 已提交
144 145
**参数:**

Z
zengyawen 已提交
146 147 148
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 文件检索选项 |
P
panqiangbiao 已提交
149 150 151

**返回值**

Z
zengyawen 已提交
152 153 154
| 类型                                 | 说明           |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | 文件数据结果集 |
P
panqiangbiao 已提交
155 156 157

**示例:**

158
```js
159 160
let fileKeyObj = mediaLibrary.FileKey;
let imageType = mediaLibrary.MediaType.IMAGE;
Z
zhang-daiyue 已提交
161
let imagesFetchOp = {
P
panqiangbiao 已提交
162 163 164
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
Z
zhang-daiyue 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
media.getFileAssets(imagesFetchOp).then(function(fetchFileResult) {
    const count = fetchFileResult.getCount();
    if (count < 0) {
        console.error('Failed to get count from fetchFileResult: count: ' + count);
        return;
    }
    if (count == 0) {
        console.info('The count of fetchFileResult is zero');
        return;
    }
    console.info('Get fetchFileResult success, count: ' + count);
    fetchFileResult.getFirstObject().then(function(fileAsset) {
        console.log('fileAsset.displayName ' + ': ' + fileAsset.displayName);
        for (let i = 1; i < count; i++) {
            fetchFileResult.getNextObject().then(function(fileAsset) {
                console.log('fileAsset.displayName ' + ': ' + fileAsset.displayName);
            }).catch(function(err) {
                console.error('Failed to get next object: ' + err);
            })
        }
    }).catch(function(err) {
        console.error('Failed to get first object: ' + err);
    });
P
panqiangbiao 已提交
188
}).catch(function(err){
Z
zhang-daiyue 已提交
189
    console.error("Failed to get file assets: " + err);
P
panqiangbiao 已提交
190
});
P
panqiangbiao 已提交
191 192
```

P
panqiangbiao 已提交
193
### on<sup>8+</sup>
P
panqiangbiao 已提交
194

195
on(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
196

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

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

P
panqiangbiao 已提交
201 202
**参数:**

H
HelloCrease 已提交
203 204
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
205 206
| type     | 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange'               | 是    | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'fileChange':     &nbsp;文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更 |
| callback | Callback&lt;void&gt; | 是    | 回调返回空                                    |
P
panqiangbiao 已提交
207 208 209

**示例:**

210
```js
Z
zhang-daiyue 已提交
211
media.on('imageChange', () => {
P
panqiangbiao 已提交
212
    // image file had changed, do something
P
panqiangbiao 已提交
213 214
})
```
P
panqiangbiao 已提交
215
### off<sup>8+</sup>
P
panqiangbiao 已提交
216

217
off(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback?: Callback&lt;void&gt;): void
P
panqiangbiao 已提交
218

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

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

P
panqiangbiao 已提交
223 224
**参数:**

H
HelloCrease 已提交
225 226
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
H
huweiqi 已提交
227 228
| type     | 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange'               | 是    | 媒体类型 <br/>'deviceChange':&nbsp;注册设备变更 <br/>'albumChange':&nbsp;相册变更<br/>'imageChange':&nbsp;图片文件变更<br/>'audioChange': &nbsp;音频文件变更<br/>'videoChange':  &nbsp;视频文件变更<br/>'fileChange':     &nbsp;文件变更<br/>'remoteFileChange':&nbsp;注册设备上文件变更 |
| callback | Callback&lt;void&gt; | 否    | 回调返回空                                    |
P
panqiangbiao 已提交
229 230 231

**示例:**

232
```js
潘强标 已提交
233
media.off('imageChange', () => {
P
panqiangbiao 已提交
234
    // stop listening success
P
panqiangbiao 已提交
235 236 237
})
```

B
bmeangel 已提交
238
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
239

P
panqiangbiao 已提交
240
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
241 242 243

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

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

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

P
panqiangbiao 已提交
248 249
**参数:**

Z
zengyawen 已提交
250 251 252 253 254 255
| 参数名       | 类型                                    | 必填 | 说明                                                         |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | 是   | 媒体类型                                                     |
| displayName  | string                                  | 是   | 展示文件名                                                   |
| relativePath | string                                  | 是   | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | 是   | 异步获取媒体数据FileAsset之后的回调                          |
P
panqiangbiao 已提交
256 257 258

**示例:**

259
```js
P
panqiangbiao 已提交
260 261 262 263 264
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
潘强标 已提交
265
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
P
panqiangbiao 已提交
266 267 268 269 270 271 272
        if (fileAsset != undefined) {
            console.info('createAsset successfully, message = ' + err);
        } else {
            console.info('createAsset failed, message = ' + err);
        }
    });
}
P
panqiangbiao 已提交
273 274
```

P
panqiangbiao 已提交
275
### createAsset<sup>8+</sup>
P
panqiangbiao 已提交
276

P
panqiangbiao 已提交
277
createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
278 279 280

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

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

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

P
panqiangbiao 已提交
285 286
**参数:**

Z
zengyawen 已提交
287 288 289 290 291
| 参数名       | 类型                     | 必填 | 说明                                                         |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | 是   | 媒体类型                                                     |
| displayName  | string                   | 是   | 展示文件名                                                   |
| relativePath | string                   | 是   | 相对路径,可以通过getPublicDirectory获取不同类型媒体文件的一层目录的relative path |
P
panqiangbiao 已提交
292 293 294

**返回值**

Z
zengyawen 已提交
295 296 297
| 类型                     | 说明              |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | 媒体数据FileAsset |
P
panqiangbiao 已提交
298 299 300

**示例:**

301
```js
H
huweiqi 已提交
302 303 304 305 306 307 308 309 310 311 312
async function example() {
    // 使用Promise方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
    media.createAsset(mediaType, 'imagePromise.jpg', path + 'myPicture/').then((fileAsset) => {
        console.info('createAsset successfully, message = ' + JSON.stringify(fileAsset));
    }).catch((err) => {
        console.info('createAsset failed, message = ' + err);
    });
}
P
panqiangbiao 已提交
313 314
```

Z
zengyawen 已提交
315 316 317 318 319 320 321 322
### deleteAsset<sup>8+</sup>

deleteAsset(uri: string): Promise\<void>

删除媒体文件资源

**系统接口**:此接口为系统接口。

323
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

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

**参数:**

| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | 是    | 需要删除的媒体文件资源的uri |

**返回值:**
| 类型                  | 说明                   |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise回调返回删除的结果。 |

**示例:**

```js
async function example() {
342 343
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
        console.error('asset not exist')
        return
    }
    media.deleteAsset(asset.uri).then(() => {
        console.info("deleteAsset successfully");
    }).catch((err) => {
        console.info("deleteAsset failed with error:"+ err);
    });
}
```

### deleteAsset<sup>8+</sup>
deleteAsset(uri: string, callback: AsyncCallback\<void>): void

删除媒体文件资源

**系统接口**:此接口为系统接口。

369
**需要权限**:ohos.permission.READ_MEDIA 和 ohos.permission.WRITE_MEDIA
Z
zengyawen 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383

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

**参数:**

| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | 是    | 需要删除的媒体文件资源的uri。 |
|callback |AsyncCallback\<void>| 是  |回调函数,用于获取删除的结果。|

**示例:**

```js
async function example() {
384 385
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
Z
zengyawen 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
        console.error('asset not exist')
        return
    }
    media.deleteAsset(asset.uri, (err) => {
        if (err != undefined) {
            console.info("deleteAsset successfully");
        } else {
            console.info("deleteAsset failed with error:"+ err);
        }
    });
}
```

P
panqiangbiao 已提交
406
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
407

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

P
panqiangbiao 已提交
410
获取公共目录路径,使用callback方式返回结果。
P
panqiangbiao 已提交
411 412 413 414 415

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

**参数:**

Z
zengyawen 已提交
416 417 418 419
| 参数名   | 类型                             | 必填 | 说明                      |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | 是   | 公共目录类型              |
| callback | AsyncCallback&lt;string&gt;      | 是   | callback 返回公共目录路径 |
P
panqiangbiao 已提交
420 421 422

**示例:**

423
```js
P
panqiangbiao 已提交
424
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
P
panqiangbiao 已提交
425
media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => {
P
panqiangbiao 已提交
426
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
427
        console.info('mediaLibraryTest : getPublicDirectory passed');
P
panqiangbiao 已提交
428
    } else {
P
panqiangbiao 已提交
429
        console.info('mediaLibraryTest : getPublicDirectory failed');
P
panqiangbiao 已提交
430 431 432 433
    }
});
```

P
panqiangbiao 已提交
434
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
435

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

P
panqiangbiao 已提交
438
获取公共目录路径,使用Promise方式返回结果。
P
panqiangbiao 已提交
439 440 441 442 443

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

**参数:**

Z
zengyawen 已提交
444 445 446
| 参数名 | 类型                             | 必填 | 说明         |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | 是   | 公共目录类型 |
P
panqiangbiao 已提交
447 448 449

**返回值:**

Z
zengyawen 已提交
450 451 452
| 类型             | 说明             |
| ---------------- | ---------------- |
| Promise\<string> | 返回公共目录路径 |
P
panqiangbiao 已提交
453 454 455

**示例:**

456
```js
P
panqiangbiao 已提交
457
async function example() {
P
panqiangbiao 已提交
458 459
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
    const dicResult = await media.getPublicDirectory(DIR_CAMERA);
P
panqiangbiao 已提交
460
    if (dicResult == 'Camera/') {
P
panqiangbiao 已提交
461 462 463 464
        console.info('MediaLibraryTest : getPublicDirectory');
    } else {
        console.info('MediaLibraryTest : getPublicDirectory failed');
    }
P
panqiangbiao 已提交
465 466 467
}
```

Z
zengyawen 已提交
468
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
469

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

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

P
panqiangbiao 已提交
474
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
475

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

P
panqiangbiao 已提交
478 479
**参数**

Z
zengyawen 已提交
480 481 482 483
| 参数名   | 类型                                         | 必填 | 说明                        |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | 是   | 相册获取条件                |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | 是   | 异步获取Album列表之后的回调 |
P
panqiangbiao 已提交
484 485 486

**示例:**

487
```js
P
panqiangbiao 已提交
488 489 490 491
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
492
media.getAlbums(AlbumNoArgsfetchOp, (err, albumList) => {
P
panqiangbiao 已提交
493 494 495 496 497 498 499
    if (albumList != undefined) {
        const album = albumList[0];
        console.info('album.albumName = ' + album.albumName);
        console.info('album.count = ' + album.count);
     } else {
        console.info('getAlbum fail, message = ' + err);
     }
P
panqiangbiao 已提交
500
})
P
panqiangbiao 已提交
501 502
```

Z
zengyawen 已提交
503
### getAlbums<sup>7+</sup>
P
panqiangbiao 已提交
504

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

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

P
panqiangbiao 已提交
509
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
510

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

P
panqiangbiao 已提交
513 514
**参数:**

Z
zengyawen 已提交
515 516 517
| 参数名  | 类型                                     | 必填 | 说明         |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | 是   | 相册获取条件 |
P
panqiangbiao 已提交
518 519 520

**返回值:**

Z
zengyawen 已提交
521 522 523
| 类型                             | 说明          |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | 返回Album列表 |
P
panqiangbiao 已提交
524 525 526

**示例:**

527
```js
P
panqiangbiao 已提交
528 529 530 531
let AlbumNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
};
潘强标 已提交
532
media.getAlbums(AlbumNoArgsfetchOp).then(function(albumList){
P
panqiangbiao 已提交
533 534 535 536
    console.info("getAlbums successfully:"+ JSON.stringify(albumList));
}).catch(function(err){
    console.info("getAlbums failed with error:"+ err);
});
P
panqiangbiao 已提交
537 538
```

P
panqiangbiao 已提交
539
### release<sup>8+</sup>
P
panqiangbiao 已提交
540

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

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

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

P
panqiangbiao 已提交
548 549
**参数:**

H
HelloCrease 已提交
550 551 552
| 参数名      | 类型                        | 必填   | 说明         |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调表示成功还是失败 |
P
panqiangbiao 已提交
553 554 555

**示例:**

556
```js
P
panqiangbiao 已提交
557
media.release((err) => {
P
panqiangbiao 已提交
558
    // do something
P
panqiangbiao 已提交
559
});
P
panqiangbiao 已提交
560 561
```

P
panqiangbiao 已提交
562
### release<sup>8+</sup>
P
panqiangbiao 已提交
563

P
panqiangbiao 已提交
564
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
565

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

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

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

H
HelloCrease 已提交
573 574
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
575
| Promise&lt;void&gt; | Promise实例,用于获取异步返回结果 |
P
panqiangbiao 已提交
576 577 578

**示例:**

579
```js
P
panqiangbiao 已提交
580
media.release()
P
panqiangbiao 已提交
581 582
```

Z
update  
zengyawen 已提交
583
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
584 585 586 587 588

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

保存媒体资源,以异步方法获取保存成功的URI,使用callback形式返回结果。

Z
update  
zengyawen 已提交
589
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
590 591 592 593 594

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

**参数:**

H
HelloCrease 已提交
595 596 597 598
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
| callback | AsyncCallback&lt;string&gt;           | 是    | 媒体资源保存回调,返回保存成功后得到的URI。 |
P
panqiangbiao 已提交
599 600 601

**示例:**

602
```js
P
panqiangbiao 已提交
603
let option = {
潘强标 已提交
604 605 606
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
607 608 609 610 611 612 613 614 615
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (err, value) => {
    if (err) {
        console.log("An error occurred when storing media resources.");
        return;
    }
    console.log("Media resources stored. ");
    // Obtain the URI that stores media resources.
});
616
```
P
panqiangbiao 已提交
617 618


Z
update  
zengyawen 已提交
619
### storeMediaAsset<sup>(deprecated)</sup>
P
panqiangbiao 已提交
620 621 622 623 624

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

保存媒体资源,以异步方法获取保存成功的URI,使用Promise形式返回结果。

Z
update  
zengyawen 已提交
625
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
626 627 628 629 630

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

**参数:**

H
HelloCrease 已提交
631 632 633
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
634 635 636

**返回值:**

H
HelloCrease 已提交
637 638
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
639 640 641 642
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

643
```js
P
panqiangbiao 已提交
644
let option = {
潘强标 已提交
645 646 647
    src : "/data/storage/el2/base/haps/entry/image.png",
    mimeType : "image/*",
    relativePath : "Pictures/"
P
panqiangbiao 已提交
648 649 650 651 652 653 654
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
    console.log("Media resources stored.");
    // Obtain the URI that stores media resources.
}).catch((err) => {
    console.log("An error occurred when storing media resources.");
});
655
```
P
panqiangbiao 已提交
656 657


Z
update  
zengyawen 已提交
658
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
659 660 661

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

Z
zhang-daiyue 已提交
662
启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(datashare://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。
P
panqiangbiao 已提交
663

664
> **说明**: <br/>从API Version 9开始废弃。建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
665 666 667 668 669

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

**参数:**

H
HelloCrease 已提交
670 671
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
672
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
673 674
| index    | number                    | 是    | 开始显示的图片序号。                               |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
675 676 677

**示例:**

678
```js
P
panqiangbiao 已提交
679
let images = [
Z
zhang-daiyue 已提交
680 681
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
682
];
H
HelloCrease 已提交
683
/* 网络图片使用方式
P
panqiangbiao 已提交
684 685 686 687
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
688
*/
P
panqiangbiao 已提交
689 690 691 692 693 694 695 696
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.");
});
697
```
P
panqiangbiao 已提交
698 699


Z
update  
zengyawen 已提交
700
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
701 702 703

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

Z
zhang-daiyue 已提交
704
启动图片预览界面,可以预览列表中首张本地图片(datashare://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。
P
panqiangbiao 已提交
705

706
> **说明**: <br/>从API Version 9开始废弃。建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
707 708 709 710 711

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

**参数:**

H
HelloCrease 已提交
712 713
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
714
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
715
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
716 717 718

**示例:**

719
```js
P
panqiangbiao 已提交
720
let images = [
Z
zhang-daiyue 已提交
721 722
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
723
];
H
HelloCrease 已提交
724
/* 网络图片使用方式
P
panqiangbiao 已提交
725 726 727 728
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
729
*/
P
panqiangbiao 已提交
730 731 732 733 734 735 736
mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
737
```
P
panqiangbiao 已提交
738 739


Z
update  
zengyawen 已提交
740
### startImagePreview<sup>(deprecated)</sup>
P
panqiangbiao 已提交
741 742 743

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

Z
zhang-daiyue 已提交
744
启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(datashare://),也可以预览列表中的所有网络图片(https://)。使用Promise方式进行异步回调。
P
panqiangbiao 已提交
745

746
> **说明**: <br/>从API Version 9开始废弃。建议使用[Image组件](../arkui-ts/ts-basic-components-image.md)替代。<br/>Image组件,可用于本地图片和网络图片的渲染展示。
P
panqiangbiao 已提交
747 748 749 750 751

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

**参数:**

H
HelloCrease 已提交
752 753
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
Z
zhang-daiyue 已提交
754
| images | Array&lt;string&gt; | 是    | 预览的图片URI("https://","datashare://")列表。 |
H
HelloCrease 已提交
755
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
756 757 758

**返回值:**

H
HelloCrease 已提交
759 760
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
P
panqiangbiao 已提交
761 762 763 764
| Promise&lt;void&gt; | Promise实例,用于异步获取预览结果,失败时返回错误信息。 |

**示例:**

765
```js
P
panqiangbiao 已提交
766
let images = [
Z
zhang-daiyue 已提交
767 768
    "datashare:///media/xxxx/2",
    "datashare:///media/xxxx/3"
P
panqiangbiao 已提交
769
];
H
HelloCrease 已提交
770
/* 网络图片使用方式
P
panqiangbiao 已提交
771 772 773 774
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
775
*/
P
panqiangbiao 已提交
776 777 778 779 780 781
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.");
});
782
```
P
panqiangbiao 已提交
783 784


Z
update  
zengyawen 已提交
785
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
786 787 788 789 790

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

启动媒体选择界面,以异步方法获取选择的媒体URI列表,使用callback形式返回结果。

791
> **说明**: <br/>从API Version 9开始废弃。建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
792 793 794 795 796

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

**参数:**

H
HelloCrease 已提交
797 798
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
Z
zengyawen 已提交
799
| option   | [MediaSelectOption](#mediaselectoptiondeprecated)  | 是    | 媒体选择选项。                              |
Z
zhang-daiyue 已提交
800
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 媒体选择回调,返回选择的媒体URI(datashare://)列表。 |
P
panqiangbiao 已提交
801 802 803

**示例:**

804
```js
Z
zhang-daiyue 已提交
805 806 807
let option : mediaLibrary.MediaSelectOption = {
    type : "media",
    count : 2
P
panqiangbiao 已提交
808
};
Z
zhang-daiyue 已提交
809
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
P
panqiangbiao 已提交
810 811 812 813 814 815 816
    if (err) {
        console.log("An error occurred when selecting media resources.");
        return;
    }
    console.log("Media resources selected.");
    // Obtain the media selection value.
});
817
```
P
panqiangbiao 已提交
818 819


Z
update  
zengyawen 已提交
820
### startMediaSelect<sup>(deprecated)</sup>
P
panqiangbiao 已提交
821 822 823 824 825

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

启动媒体选择界面,以异步方法获取选择的媒体URI列表,使用Promise形式返回结果。

826
> **说明**: <br/>从API Version 9开始废弃。建议使用系统应用图库替代。图库是系统内置的可视资源访问应用,提供图片和视频的管理、浏览等功能,使用方法请参考[OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos#4-%E5%85%B8%E5%9E%8B%E6%8E%A5%E5%8F%A3%E7%9A%84%E4%BD%BF%E7%94%A8)。
P
panqiangbiao 已提交
827 828 829 830 831

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

**参数:**

H
HelloCrease 已提交
832 833
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
Z
zengyawen 已提交
834
| option | [MediaSelectOption](#mediaselectoptiondeprecated) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
835 836 837

**返回值:**

H
HelloCrease 已提交
838 839
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
Z
zhang-daiyue 已提交
840
| Promise&lt;Array&lt;string&gt;&gt; | Promise实例,用于异步获取选择的媒体URI(datashare://)列表。 |
P
panqiangbiao 已提交
841 842 843

**示例:**

844
```js
Z
zhang-daiyue 已提交
845 846 847
let option : mediaLibrary.MediaSelectOption = {
    type : "media",
    count : 2
P
panqiangbiao 已提交
848
};
Z
zhang-daiyue 已提交
849
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
P
panqiangbiao 已提交
850 851 852 853 854 855
    console.log("Media resources selected.");
    // Obtain the media selection value.
}).catch((err) => {
    console.log("An error occurred when selecting media resources.");
});

Z
zengyawen 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893
```
### getActivePeers<sup>8+</sup>

getActivePeers(): Promise\<Array\<PeerInfo>>;

获取在线对端设备的信息,使用Promise方式返回异步结果

**系统接口**:此接口为系统接口。

**需要权限**:ohos.permission.READ_MEDIA

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
|  Promise\<Array\<PeerInfo>> | 返回获取的所有在线对端设备的PeerInfo |

**示例:**

```js
async function example() {
    media.getActivePeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
            console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed info is undefined!')
        }
    }).catch((err) => {
        console.info("get distributed info failed with error:" + err);
    });
}
```

### getActivePeers<sup>8+</sup>
894

Z
zengyawen 已提交
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

获取在线对端设备的信息,使用callback方式返回异步结果。

**系统接口**:此接口为系统接口。

**需要权限**:ohos.permission.READ_MEDIA

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
| callback: AsyncCallback\<Array\<PeerInfo>> | 返回获取的所有在线对端设备的PeerInfo |

**示例:**

```js
async function example() {
    media.getActivePeers((err, devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
                console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed fail, message = ' + err)
        }
    })
}
```


### getAllPeers<sup>8+</sup>

getAllPeers(): Promise\<Array\<PeerInfo>>;

获取所有对端设备的信息,使用Promise方式返回异步结果

**系统接口**:此接口为系统接口。

**需要权限**:ohos.permission.READ_MEDIA

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
|  Promise\<Array\<PeerInfo>> | 返回获取的所有对端设备的PeerInfo |

**示例:**

```js
async function example() {
    media.getAllPeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
                console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed info is undefined!')
        }
    }).catch((err) => {
        console.info("get distributed info failed with error:" + err);
    });
}
```

### getAllPeers<sup>8+</sup>
965

Z
zengyawen 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

获取所有对端设备的信息,使用callback方式返回异步结果。

**系统接口**:此接口为系统接口。

**需要权限**:ohos.permission.READ_MEDIA

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

**返回值:**

| 类型                  | 说明                   |
| ------------------- | -------------------- |
| callback: AsyncCallback\<Array\<PeerInfo>> | 返回获取的所有对端设备的PeerInfo |

**示例:**

```js
async function example() {
    media.getAllPeers((err, devicesInfo) => {
        if (devicesInfo != undefined) {
            for (let i = 0; i < devicesInfo.length; i++) {
            console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId);
            }
        } else {
            console.info('get distributed fail, message = ' + err)
        }
    })
}
996
```
P
panqiangbiao 已提交
997

Z
zengyawen 已提交
998
## FileAsset<sup>7+</sup>
P
panqiangbiao 已提交
999 1000 1001

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

Z
zengyawen 已提交
1002 1003 1004
### 属性

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
1005

Z
zengyawen 已提交
1006 1007 1008
| 名称                      | 类型                     | 可读 | 可写 | 说明                                                   |
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
| id                        | number                   | 是   | 否   | 文件资源编号                                           |
Z
zhang-daiyue 已提交
1009
| uri                       | string                   | 是   | 否   | 文件资源uri(如:datashare:///media/image/2)         |
Z
zengyawen 已提交
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
| mimeType                  | string                   | 是   | 否   | 文件扩展属性                                           |
| mediaType<sup>8+</sup>    | [MediaType](#mediatype8) | 是   | 否   | 媒体类型                                               |
| displayName               | string                   | 是   | 是   | 显示文件名,包含后缀名                                 |
| title                     | string                   | 是   | 是   | 文件标题                                               |
| relativePath<sup>8+</sup> | string                   | 是   | 是   | 相对公共目录路径                                       |
| parent<sup>8+</sup>       | number                   | 是   | 否   | 父目录id                                               |
| size                      | number                   | 是   | 否   | 文件大小(单位:字节)                                 |
| dateAdded                 | number                   | 是   | 否   | 添加日期(添加文件时间到1970年1月1日的秒数值)         |
| dateModified              | number                   | 是   | 否   | 修改日期(修改文件时间到1970年1月1日的秒数值)         |
| dateTaken                 | number                   | 是   | 否   | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)         |
| artist<sup>8+</sup>       | string                   | 是   | 否   | 作者                                                   |
| audioAlbum<sup>8+</sup>   | string                   | 是   | 否   | 专辑                                                   |
| width                     | number                   | 是   | 否   | 图片宽度(单位:像素)                                 |
| height                    | number                   | 是   | 否   | 图片高度(单位:像素)                                 |
| orientation               | number                   | 是   | 是   | 图片显示方向(顺时针旋转角度,如0,90,180  单位:度) |
潘强标 已提交
1025
| duration<sup>8+</sup>     | number                   | 是   | 否   | 持续时间(单位:毫秒)                                   |
Z
zengyawen 已提交
1026 1027 1028
| albumId                   | number                   | 是   | 否   | 文件所归属的相册编号                                   |
| albumUri<sup>8+</sup>     | string                   | 是   | 否   | 文件所归属相册uri                                      |
| albumName                 | string                   | 是   | 否   | 文件所归属相册名称                                     |
P
panqiangbiao 已提交
1029 1030 1031


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

P
panqiangbiao 已提交
1033
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1034 1035 1036

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

P
panqiangbiao 已提交
1037
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1038

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

P
panqiangbiao 已提交
1041 1042
**参数:**

H
HelloCrease 已提交
1043 1044 1045
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
1046 1047 1048

**示例:**

1049
```js
P
panqiangbiao 已提交
1050
async function example() {
Z
zhang-daiyue 已提交
1051
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1052 1053 1054 1055
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1056 1057
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1058 1059 1060 1061 1062 1063 1064
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
1065 1066
```

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

P
panqiangbiao 已提交
1069
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1070 1071 1072

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

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

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

P
panqiangbiao 已提交
1077 1078
**返回值:**

H
HelloCrease 已提交
1079 1080
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
1081
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
1082 1083 1084

**示例:**

1085
```js
P
panqiangbiao 已提交
1086
async function example() {
Z
zhang-daiyue 已提交
1087
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1088 1089 1090 1091
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1092 1093
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory().then(function(isDirectory){
        console.info("isDirectory result:"+ isDirectory);
    }).catch(function(err){
        console.info("isDirectory failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1103 1104
```

P
panqiangbiao 已提交
1105
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1106

P
panqiangbiao 已提交
1107
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1108 1109 1110

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

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

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

P
panqiangbiao 已提交
1115 1116
**参数:**

H
HelloCrease 已提交
1117 1118 1119
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1120 1121 1122

**示例:**

1123
```js
P
panqiangbiao 已提交
1124
async function example() {
Z
zhang-daiyue 已提交
1125
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1126 1127 1128 1129
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1130 1131
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1132 1133 1134
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1135
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1136 1137
    asset.commitModify(() => {
        console.info('commitModify success');   
P
panqiangbiao 已提交
1138
    });
P
panqiangbiao 已提交
1139
}
P
panqiangbiao 已提交
1140 1141
```

P
panqiangbiao 已提交
1142
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1143

P
panqiangbiao 已提交
1144
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1145 1146 1147

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

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

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

P
panqiangbiao 已提交
1152 1153
**返回值:**

H
HelloCrease 已提交
1154 1155
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1156
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1157 1158 1159

**示例:**

1160
```js
P
panqiangbiao 已提交
1161
async function example() {
Z
zhang-daiyue 已提交
1162
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1163 1164 1165 1166
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1167 1168
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1169 1170 1171
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
1172
    asset.title = 'newtitle';
P
panqiangbiao 已提交
1173 1174
    asset.commitModify();
}
P
panqiangbiao 已提交
1175 1176
```

P
panqiangbiao 已提交
1177
### open<sup>8+</sup>
P
panqiangbiao 已提交
1178

P
panqiangbiao 已提交
1179
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
1180 1181 1182

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

潘强标 已提交
1183 1184
**注意**:当前写操作是互斥的操作,写操作完成后需要调用close进行释放

Z
zhang-daiyue 已提交
1185
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1186

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

P
panqiangbiao 已提交
1189 1190
**参数**

H
HelloCrease 已提交
1191 1192 1193 1194
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件句柄                            |
P
panqiangbiao 已提交
1195 1196 1197

**示例:**

1198
```js
P
panqiangbiao 已提交
1199
async function example() {
P
panqiangbiao 已提交
1200
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1201 1202
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1203
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1204 1205 1206 1207 1208 1209 1210 1211
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
P
panqiangbiao 已提交
1212 1213
```

P
panqiangbiao 已提交
1214
### open<sup>8+</sup>
P
panqiangbiao 已提交
1215

P
panqiangbiao 已提交
1216
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
1217 1218 1219

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

潘强标 已提交
1220 1221
**注意**:当前写操作是互斥的操作,写操作完成后需要调用close进行释放

Z
zhang-daiyue 已提交
1222
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1223

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

P
panqiangbiao 已提交
1226 1227
**参数:**

H
HelloCrease 已提交
1228 1229 1230
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
1231 1232 1233

**返回值:**

H
HelloCrease 已提交
1234 1235
| 类型                    | 说明            |
| --------------------- | ------------- |
P
panqiangbiao 已提交
1236
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
1237 1238 1239

**示例:**

1240
```js
P
panqiangbiao 已提交
1241
async function example() {
P
panqiangbiao 已提交
1242
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
1243 1244
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
Z
zhang-daiyue 已提交
1245
    const asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
1246 1247 1248 1249 1250 1251 1252
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
            console.info('File err!' + err);
        });
P
panqiangbiao 已提交
1253
}
P
panqiangbiao 已提交
1254 1255
```

P
panqiangbiao 已提交
1256
### close<sup>8+</sup>
P
panqiangbiao 已提交
1257

P
panqiangbiao 已提交
1258
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1259 1260 1261

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

Z
zhang-daiyue 已提交
1262
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1263

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

P
panqiangbiao 已提交
1266 1267
**参数:**

H
HelloCrease 已提交
1268 1269 1270 1271
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1272 1273 1274

**示例:**

1275
```js
P
panqiangbiao 已提交
1276
async function example() {
Z
zhang-daiyue 已提交
1277
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1278 1279 1280 1281
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1282 1283
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1284 1285 1286
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1287 1288 1289 1290
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd, (closeErr) => {
            if (closeErr != undefined) {
1291
                console.info('mediaLibraryTest : close : FAIL ' + closeErr);
Z
zhang-daiyue 已提交
1292 1293 1294 1295 1296 1297 1298 1299
                console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1300 1301
    });
}
P
panqiangbiao 已提交
1302 1303
```

P
panqiangbiao 已提交
1304
### close<sup>8+</sup>
P
panqiangbiao 已提交
1305

P
panqiangbiao 已提交
1306
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
1307 1308 1309

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

Z
zhang-daiyue 已提交
1310
**需要权限**:ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
P
panqiangbiao 已提交
1311

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

P
panqiangbiao 已提交
1314 1315
**参数:**

H
HelloCrease 已提交
1316 1317 1318
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
1319 1320 1321

**返回值:**

H
HelloCrease 已提交
1322 1323
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1324
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1325 1326 1327

**示例:**

1328
```js
P
panqiangbiao 已提交
1329
async function example() {
Z
zhang-daiyue 已提交
1330
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1331 1332 1333 1334
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1335 1336
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1337 1338 1339
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1340 1341 1342 1343
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
        asset.close(fd).then((closeErr) => {
            if (closeErr != undefined) {
1344
                console.info('mediaLibraryTest : close : FAIL ' + closeErr);
Z
zhang-daiyue 已提交
1345
                console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
1346

Z
zhang-daiyue 已提交
1347 1348 1349 1350 1351 1352 1353
            } else {
                console.info("=======asset.close success====>");
            }
        });
    })
    .catch((err) => {
        console.info('File err!' + err);
P
panqiangbiao 已提交
1354 1355
    });
}
P
panqiangbiao 已提交
1356 1357
```

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

P
panqiangbiao 已提交
1360
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1361 1362 1363

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

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

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

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

H
HelloCrease 已提交
1370 1371 1372
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1373 1374 1375

**示例:**

1376
```js
P
panqiangbiao 已提交
1377
async function example() {
Z
zhang-daiyue 已提交
1378
    let fileKeyObj = mediaLibrary.FileKey
P
panqiangbiao 已提交
1379 1380 1381 1382
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1383 1384
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1385 1386 1387 1388
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
Z
zengyawen 已提交
1389
        console.info('mediaLibraryTest : getThumbnail Successful '+ pixelmap);
P
panqiangbiao 已提交
1390 1391
    });
}
P
panqiangbiao 已提交
1392 1393
```

P
panqiangbiao 已提交
1394
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1395

P
panqiangbiao 已提交
1396
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1397 1398 1399

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

P
panqiangbiao 已提交
1400
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1401

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

P
panqiangbiao 已提交
1404 1405
**参数:**

H
HelloCrease 已提交
1406 1407 1408 1409
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1410 1411 1412

**示例:**

1413
```js
P
panqiangbiao 已提交
1414
async function example() {
1415
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1416 1417 1418 1419
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1420 1421
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1422
    };
Z
zhang-daiyue 已提交
1423
    let size = { width: 720, height: 720 };
P
panqiangbiao 已提交
1424 1425 1426
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
Z
zengyawen 已提交
1427
        console.info('mediaLibraryTest : getThumbnail Successful '+ pixelmap);
P
panqiangbiao 已提交
1428 1429
    });
}
P
panqiangbiao 已提交
1430 1431
```

P
panqiangbiao 已提交
1432
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1433

P
panqiangbiao 已提交
1434
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1435 1436 1437

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

P
panqiangbiao 已提交
1438
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1439

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

P
panqiangbiao 已提交
1442 1443
**参数:**

H
HelloCrease 已提交
1444 1445 1446
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1447 1448 1449

**返回值:**

H
HelloCrease 已提交
1450 1451
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1452
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1453 1454 1455

**示例:**

1456
```js
P
panqiangbiao 已提交
1457
async function example() {
1458
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1459 1460
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
潘强标 已提交
1461 1462 1463 1464
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1465
    };
Z
zhang-daiyue 已提交
1466
    let size = { width: 720, height: 720 };
P
panqiangbiao 已提交
1467 1468
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
潘强标 已提交
1469 1470
    asset.getThumbnail(size)
    .then((pixelmap) => {
Z
zengyawen 已提交
1471
        console.info('mediaLibraryTest : getThumbnail Successful '+ pixelmap);
潘强标 已提交
1472 1473 1474
    })
    .catch((err) => {
        console.info('mediaLibraryTest : getThumbnail fail'+ err);
P
panqiangbiao 已提交
1475 1476
    });
}
P
panqiangbiao 已提交
1477 1478
```

P
panqiangbiao 已提交
1479
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1480

P
panqiangbiao 已提交
1481
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1482 1483 1484

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

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

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

P
panqiangbiao 已提交
1489 1490
**参数:**

H
HelloCrease 已提交
1491 1492 1493 1494
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1495 1496 1497

**示例:**

1498
```js
P
panqiangbiao 已提交
1499
async function example() {
1500
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1501 1502 1503 1504
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1505 1506
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1507 1508 1509 1510 1511 1512 1513
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
1514 1515
```

P
panqiangbiao 已提交
1516
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1517

P
panqiangbiao 已提交
1518
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1519 1520 1521

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

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

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

P
panqiangbiao 已提交
1526 1527
**参数:**

H
HelloCrease 已提交
1528 1529 1530
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1531 1532 1533

**返回值:**

H
HelloCrease 已提交
1534 1535
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1536
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1537 1538 1539

**示例:**

1540
```js
P
panqiangbiao 已提交
1541
async function example() {
1542
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1543 1544 1545 1546
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1547 1548
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1549 1550 1551 1552 1553 1554 1555 1556 1557
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true).then(function() {
        console.info("favorite successfully");
    }).catch(function(err){
        console.info("favorite failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1558 1559
```

P
panqiangbiao 已提交
1560
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1561

P
panqiangbiao 已提交
1562
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1563 1564 1565

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

P
panqiangbiao 已提交
1566
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1567

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

P
panqiangbiao 已提交
1570 1571
**参数:**

H
HelloCrease 已提交
1572 1573 1574
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1575 1576 1577

**示例:**

1578
```js
P
panqiangbiao 已提交
1579
async function example() {
1580
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1581 1582 1583 1584
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1585 1586
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite((err, isFavorite) => {
        if (isFavorite) {
            console.info('FileAsset is favorite');
        }else{
            console.info('FileAsset is not favorite');
        }
    });
}
P
panqiangbiao 已提交
1598 1599
```

P
panqiangbiao 已提交
1600
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1601

P
panqiangbiao 已提交
1602
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1603 1604 1605

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

P
panqiangbiao 已提交
1606
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1607

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

P
panqiangbiao 已提交
1610 1611
**返回值:**

H
HelloCrease 已提交
1612 1613
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1614
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1615 1616 1617

**示例:**

1618
```js
P
panqiangbiao 已提交
1619
async function example() {
1620
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1621 1622 1623 1624
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1625 1626
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1627 1628 1629 1630 1631 1632 1633 1634 1635
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isFavorite().then(function(isFavorite){
        console.info("isFavorite result:"+ isFavorite);
    }).catch(function(err){
        console.info("isFavorite failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1636 1637
```

P
panqiangbiao 已提交
1638
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1639

A
AOL 已提交
1640
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1641 1642 1643

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

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

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

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

P
panqiangbiao 已提交
1650 1651
**参数:**

H
HelloCrease 已提交
1652 1653 1654 1655
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1656 1657 1658

**示例:**

1659
```js
P
panqiangbiao 已提交
1660
async function example() {
1661
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1662 1663 1664 1665
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1666 1667
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1668 1669 1670 1671 1672
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1673
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1674
    }
P
panqiangbiao 已提交
1675
}
P
panqiangbiao 已提交
1676 1677
```

P
panqiangbiao 已提交
1678
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1679

A
AOL 已提交
1680
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1681 1682 1683

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

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

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

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

P
panqiangbiao 已提交
1690 1691
**参数:**

H
HelloCrease 已提交
1692 1693 1694
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1695 1696 1697

**返回值:**

H
HelloCrease 已提交
1698 1699
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1700
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1701 1702 1703

**示例:**

1704
```js
P
panqiangbiao 已提交
1705
async function example() {
1706
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1707 1708 1709 1710
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1711 1712
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true).then(function() {
        console.info("trash successfully");
    }).catch(function(err){
        console.info("trash failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1722 1723
```

P
panqiangbiao 已提交
1724
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1725

P
panqiangbiao 已提交
1726
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1727 1728 1729

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

P
panqiangbiao 已提交
1730
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1731

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

P
panqiangbiao 已提交
1734 1735
**参数:**

H
HelloCrease 已提交
1736 1737 1738
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1739 1740 1741

**示例:**

1742
```js
P
panqiangbiao 已提交
1743
async function example() {
1744
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1745 1746 1747 1748
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1749 1750
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1751 1752 1753
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
Z
zhang-daiyue 已提交
1754 1755 1756 1757 1758 1759 1760
    asset.isTrash((err, isTrash) => {
      if (isTrash == undefined) {
        console.error('Failed to get trash state: ' + err);
        return;
      }
      console.info('Get trash state success: ' + isTrash);
    });
P
panqiangbiao 已提交
1761
}
P
panqiangbiao 已提交
1762 1763
```

P
panqiangbiao 已提交
1764
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1765

P
panqiangbiao 已提交
1766
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1767

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

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

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

P
panqiangbiao 已提交
1774 1775
**返回值:**

H
HelloCrease 已提交
1776 1777
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1778
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1779 1780 1781

**示例:**

1782
```js
P
panqiangbiao 已提交
1783
async function example() {
1784
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1785 1786 1787 1788
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1789
      order: fileKeyObj.DATE_ADDED + " DESC",
P
panqiangbiao 已提交
1790 1791 1792 1793
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash().then(function(isTrash){
Z
zhang-daiyue 已提交
1794
      console.info("isTrash result: " + isTrash);
P
panqiangbiao 已提交
1795
    }).catch(function(err){
Z
zhang-daiyue 已提交
1796
      console.error("isTrash failed with error: " + err);
P
panqiangbiao 已提交
1797 1798
    });
}
P
panqiangbiao 已提交
1799 1800
```

Z
zengyawen 已提交
1801
## FetchFileResult<sup>7+</sup>
P
panqiangbiao 已提交
1802 1803 1804

文件检索结果集。

Z
zengyawen 已提交
1805
### getCount<sup>7+</sup>
P
panqiangbiao 已提交
1806

P
panqiangbiao 已提交
1807
getCount(): number
P
panqiangbiao 已提交
1808 1809 1810

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

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

P
panqiangbiao 已提交
1813 1814
**返回值**

H
HelloCrease 已提交
1815 1816
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1817
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1818 1819 1820

**示例**

1821
```js
P
panqiangbiao 已提交
1822
async function example() {
1823
    let fileKeyObj = mediaLibrary.FileKey;
Z
zhang-daiyue 已提交
1824
    let fileType = mediaLibrary.MediaType.FILE;
P
panqiangbiao 已提交
1825 1826 1827
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1828 1829
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1830 1831 1832 1833
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1834 1835
```

Z
zengyawen 已提交
1836
### isAfterLast<sup>7+</sup>
P
panqiangbiao 已提交
1837

P
panqiangbiao 已提交
1838
isAfterLast(): boolean
P
panqiangbiao 已提交
1839 1840 1841

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

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

P
panqiangbiao 已提交
1844 1845
**返回值**

H
HelloCrease 已提交
1846 1847
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1848
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1849 1850 1851

**示例**

1852
```js
P
panqiangbiao 已提交
1853
async function example() {
1854
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1855 1856 1857 1858
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1859 1860
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1861 1862 1863
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1864
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1865 1866 1867 1868
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1869
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1870
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1871 1872
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1873
              fetchFileResult.close();
P
panqiangbiao 已提交
1874

P
panqiangbiao 已提交
1875 1876
            }
    }
P
panqiangbiao 已提交
1877
}
P
panqiangbiao 已提交
1878 1879
```

Z
zengyawen 已提交
1880
### close<sup>7+</sup>
P
panqiangbiao 已提交
1881

P
panqiangbiao 已提交
1882
close(): void
P
panqiangbiao 已提交
1883 1884 1885

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

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

P
panqiangbiao 已提交
1888 1889
**示例**

1890
```js
P
panqiangbiao 已提交
1891
async function example() {
1892
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1893 1894 1895 1896
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1897 1898
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1899 1900 1901 1902
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1903 1904
```

Z
zengyawen 已提交
1905
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1906

P
panqiangbiao 已提交
1907
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1908 1909 1910

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

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

P
panqiangbiao 已提交
1913 1914
**参数**

Z
zengyawen 已提交
1915 1916 1917
| 参数名   | 类型                                          | 必填 | 说明                                        |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步获取结果集中第一个FileAsset完成后的回调 |
P
panqiangbiao 已提交
1918 1919 1920

**示例**

1921
```js
P
panqiangbiao 已提交
1922
async function example() {
1923
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1924 1925 1926 1927
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1928 1929
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1930 1931
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
1932
    fetchFileResult.getFirstObject((err, fileAsset) => {
P
panqiangbiao 已提交
1933 1934 1935 1936
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
1937
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
1938 1939
    })
}
P
panqiangbiao 已提交
1940 1941
```

Z
zengyawen 已提交
1942
### getFirstObject<sup>7+</sup>
P
panqiangbiao 已提交
1943

P
panqiangbiao 已提交
1944
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1945

Z
zengyawen 已提交
1946
获取文件检索结果中的第一个文件资产。此方法使用Promise方式返回FileAsset。
P
panqiangbiao 已提交
1947

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

P
panqiangbiao 已提交
1950 1951
**返回值**

Z
zengyawen 已提交
1952 1953
| 类型                                    | 说明                       |
| --------------------------------------- | -------------------------- |
Z
zengyawen 已提交
1954
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise方式返回FileAsset。 |
P
panqiangbiao 已提交
1955 1956 1957

**示例**

1958
```js
P
panqiangbiao 已提交
1959
async function example() {
1960
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1961 1962 1963 1964
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1965 1966
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1967 1968 1969 1970 1971 1972 1973 1974
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getFirstObject().then(function(fileAsset){
        console.info("getFirstObject successfully:"+ JSON.stringify(fileAsset));
    }).catch(function(err){
        console.info("getFirstObject failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1975 1976
```

Z
zengyawen 已提交
1977
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
1978

P
panqiangbiao 已提交
1979
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1980 1981 1982

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

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

P
panqiangbiao 已提交
1985 1986
**参数**

Z
zengyawen 已提交
1987 1988 1989
| 参数名    | 类型                                          | 必填 | 说明                                      |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1990 1991 1992

**示例**

1993
```js
P
panqiangbiao 已提交
1994
async function example() {
1995
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
1996 1997 1998 1999
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2000 2001
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2002 2003
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2004
    fetchFileResult.getNextObject((err, fileAsset) => {
P
panqiangbiao 已提交
2005 2006 2007 2008
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2009
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2010 2011
    })
}
P
panqiangbiao 已提交
2012 2013
```

Z
zengyawen 已提交
2014
### getNextObject<sup>7+</sup>
P
panqiangbiao 已提交
2015

P
panqiangbiao 已提交
2016
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2017 2018 2019

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

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

P
panqiangbiao 已提交
2022 2023
**返回值**

Z
zengyawen 已提交
2024 2025 2026
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2027 2028 2029

**示例**

2030
```js
P
panqiangbiao 已提交
2031
async function example() {
2032
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2033 2034 2035 2036
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2037 2038
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2039 2040 2041
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
2042
    console.info('mediaLibraryTest : count:' + fetchCount);
Z
zhang-daiyue 已提交
2043
    let fileAsset = await fetchFileResult.getNextObject();
P
panqiangbiao 已提交
2044
}
P
panqiangbiao 已提交
2045 2046
```

Z
zengyawen 已提交
2047
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2048

P
panqiangbiao 已提交
2049
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2050 2051 2052

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

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

P
panqiangbiao 已提交
2055 2056
**参数**

Z
zengyawen 已提交
2057 2058
| 参数名   | 类型                                          | 必填 | 说明                        |
| -------- | --------------------------------------------- | ---- | --------------------------- |
Z
zengyawen 已提交
2059
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是   | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2060 2061 2062

**示例**

2063
```js
P
panqiangbiao 已提交
2064
async function example() {
2065
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2066 2067 2068 2069
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2070 2071
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2072 2073
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2074
    fetchFileResult.getLastObject((err, fileAsset) => {
P
panqiangbiao 已提交
2075 2076 2077 2078
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2079
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2080 2081
    })
}
P
panqiangbiao 已提交
2082 2083
```

Z
zengyawen 已提交
2084
### getLastObject<sup>7+</sup>
P
panqiangbiao 已提交
2085

P
panqiangbiao 已提交
2086
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2087 2088 2089

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

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

P
panqiangbiao 已提交
2092 2093
**返回值**

Z
zengyawen 已提交
2094 2095 2096
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2097 2098 2099

**示例**

2100
```js
P
panqiangbiao 已提交
2101
async function example() {
2102
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2103 2104 2105 2106
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2107 2108
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2109 2110 2111 2112
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
2113 2114
```

Z
zengyawen 已提交
2115
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2116

P
panqiangbiao 已提交
2117
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
2118 2119 2120

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

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

P
panqiangbiao 已提交
2123 2124
**参数**

Z
zengyawen 已提交
2125
| 参数名       | 类型                                       | 必填   | 说明                 |
H
HelloCrease 已提交
2126 2127
| -------- | ---------------------------------------- | ---- | ------------------ |
| index    | number                                   | 是    | 要获取的文件的索引,从0开始     |
Z
zengyawen 已提交
2128
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | 是    | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
2129 2130 2131

**示例**

2132
```js
P
panqiangbiao 已提交
2133
async function example() {
2134
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2135 2136 2137 2138
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2139 2140
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2141 2142
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2143
    fetchFileResult.getPositionObject(0, (err, fileAsset) => {
P
panqiangbiao 已提交
2144 2145 2146 2147
       if (err) {
           console.error('Failed ');
           return;
       }
Z
zhang-daiyue 已提交
2148
       console.log('fileAsset.displayName : ' + fileAsset.displayName);
P
panqiangbiao 已提交
2149 2150
    })
}
P
panqiangbiao 已提交
2151 2152
```

Z
zengyawen 已提交
2153
### getPositionObject<sup>7+</sup>
P
panqiangbiao 已提交
2154

P
panqiangbiao 已提交
2155
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
2156 2157 2158

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

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

P
panqiangbiao 已提交
2161 2162
**参数**

Z
zengyawen 已提交
2163
| 参数名    | 类型     | 必填   | 说明             |
H
HelloCrease 已提交
2164 2165
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
2166 2167 2168

**返回值**

Z
zengyawen 已提交
2169 2170 2171
| 类型                                    | 说明              |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
2172 2173 2174

**示例**

2175
```js
P
panqiangbiao 已提交
2176
async function example() {
2177
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2178 2179 2180 2181
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2182 2183
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2184 2185
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2186
    fetchFileResult.getPositionObject(1) .then(function (fileAsset){
2187
        console.log('fileAsset.displayName : ' + fileAsset.displayName);
Z
zhang-daiyue 已提交
2188
    }).catch(function (err) {
2189
        console.info("getFileAssets failed with error:" + err);
Z
zhang-daiyue 已提交
2190
    });
P
panqiangbiao 已提交
2191
}
P
panqiangbiao 已提交
2192 2193
```

Z
zengyawen 已提交
2194
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2195

P
panqiangbiao 已提交
2196
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
2197 2198 2199

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

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

P
panqiangbiao 已提交
2202 2203
**参数**

Z
zengyawen 已提交
2204
| 参数名       | 类型                                       | 必填   | 说明                   |
H
HelloCrease 已提交
2205
| -------- | ---------------------------------------- | ---- | -------------------- |
Z
zengyawen 已提交
2206
| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
2207 2208 2209

**示例**

2210
```js
P
panqiangbiao 已提交
2211
async function example() {
2212
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2213 2214 2215 2216
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2217 2218
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2219 2220
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
Z
zhang-daiyue 已提交
2221
    fetchFileResult.getAllObject((err, fileAsset) => {
2222
        if (err) {
P
panqiangbiao 已提交
2223 2224
           console.error('Failed ');
           return;
2225 2226 2227 2228
        }
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
            console.log('fileAsset.displayName : ' + fileAsset[i].displayName);
        } 
P
panqiangbiao 已提交
2229 2230
    })
}
P
panqiangbiao 已提交
2231 2232
```

Z
zengyawen 已提交
2233
### getAllObject<sup>7+</sup>
P
panqiangbiao 已提交
2234

P
panqiangbiao 已提交
2235
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
2236 2237 2238

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

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

P
panqiangbiao 已提交
2241 2242
**返回值**

Z
zengyawen 已提交
2243 2244 2245
| 类型                                     | 说明                  |
| ---------------------------------------- | --------------------- |
| Promise<Array<[FileAsset](#fileasset7)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
2246 2247 2248

**示例**

2249
```js
P
panqiangbiao 已提交
2250
async function example() {
2251
    let fileKeyObj = mediaLibrary.FileKey;
P
panqiangbiao 已提交
2252 2253 2254 2255
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
2256 2257
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
2258 2259 2260 2261
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
P
panqiangbiao 已提交
2262 2263
```

Z
zengyawen 已提交
2264
## Album<sup>7+</sup>
P
panqiangbiao 已提交
2265 2266 2267

实体相册

Z
zengyawen 已提交
2268
### 属性
P
panqiangbiao 已提交
2269

Z
zengyawen 已提交
2270 2271
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

Z
zengyawen 已提交
2272
| 名称           | 类型    | 可读   | 可写   | 说明      |
H
HelloCrease 已提交
2273
| ------------ | ------ | ---- | ---- | ------- |
Z
zengyawen 已提交
2274 2275 2276
| albumId | number | 是    | 否    | 相册编号    |
| albumName | string | 是    | 是    | 相册名称    |
| albumUri<sup>8+</sup> | string | 是    | 否    | 相册Uri   |
H
HelloCrease 已提交
2277
| dateModified | number | 是    | 否    | 修改日期    |
Z
zengyawen 已提交
2278 2279 2280
| count<sup>8+</sup> | number | 是    | 否    | 相册中文件数量 |
| relativePath<sup>8+</sup> | string | 是    | 否    | 相对路径    |
| coverUri<sup>8+</sup> | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
2281

P
panqiangbiao 已提交
2282 2283 2284
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
2285 2286 2287

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

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

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

P
panqiangbiao 已提交
2292 2293
**参数**

Z
zengyawen 已提交
2294 2295 2296
| 参数名   | 类型                      | 必填 | 说明       |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | 是   | 回调返回空 |
P
panqiangbiao 已提交
2297 2298 2299

**示例**

2300
```js
P
panqiangbiao 已提交
2301
async function example() {
P
panqiangbiao 已提交
2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify((err) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log('Modify successful.');
    })
}
P
panqiangbiao 已提交
2317 2318
```

P
panqiangbiao 已提交
2319
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
2320

P
panqiangbiao 已提交
2321
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
2322 2323 2324

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

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

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

P
panqiangbiao 已提交
2329 2330
**返回值**

H
HelloCrease 已提交
2331 2332
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
2333 2334 2335 2336
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

2337
```js
P
panqiangbiao 已提交
2338
async function example() {
P
panqiangbiao 已提交
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
    album.commitModify().then(function() {
        console.info("commitModify successfully");
    }).catch(function(err){
        console.info("commitModify failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2352 2353
```

Z
zengyawen 已提交
2354
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2355

P
panqiangbiao 已提交
2356
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2357 2358 2359

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

P
panqiangbiao 已提交
2360
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2361

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

P
panqiangbiao 已提交
2364 2365
**参数**

Z
zengyawen 已提交
2366
| 参数名   | 类型                                                | 必填 | 说明                                |
Z
zengyawen 已提交
2367
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
Z
zengyawen 已提交
2368 2369
| options  | [MediaFetchOptions](#mediafetchoptions7)            | 是   | 媒体检索选项。                      |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | 是   | 异步返回FetchFileResult之后的回调。 |
P
panqiangbiao 已提交
2370 2371 2372

**示例**

2373
```js
P
panqiangbiao 已提交
2374
async function example() {
P
panqiangbiao 已提交
2375 2376 2377 2378
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2379 2380 2381 2382
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
    }
P
panqiangbiao 已提交
2383 2384 2385 2386 2387 2388
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
P
panqiangbiao 已提交
2389 2390 2391
}
```

Z
zengyawen 已提交
2392
### getFileAssets<sup>7+</sup>
P
panqiangbiao 已提交
2393

P
panqiangbiao 已提交
2394
 getFileAssets(options?: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
2395

Z
zengyawen 已提交
2396
按照检索条件获取相册中的文件。此方法使用异步Promise来返回文件结果集。
P
panqiangbiao 已提交
2397

P
panqiangbiao 已提交
2398
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2399

P
panqiangbiao 已提交
2400
**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2401 2402 2403

**参数**

Z
zengyawen 已提交
2404
| 参数名  | 类型                                     | 必填 | 说明           |
Z
zengyawen 已提交
2405
| ------- | ---------------------------------------- | ---- | -------------- |
Z
zengyawen 已提交
2406
| options | [MediaFetchOptions](#mediafetchoptions7) | 否   | 媒体检索选项。 |
P
panqiangbiao 已提交
2407 2408 2409

**返回值**

Z
zengyawen 已提交
2410 2411
| 类型                                          | 说明                      |
| --------------------------------------------- | ------------------------- |
Z
zengyawen 已提交
2412
| Promise<[FetchFileResult](#fetchfileresult7)> | 返回FetchFileResult对象。 |
P
panqiangbiao 已提交
2413 2414 2415

**示例**

2416
```js
P
panqiangbiao 已提交
2417
async function example() {
P
panqiangbiao 已提交
2418 2419 2420 2421
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
Z
zhang-daiyue 已提交
2422 2423 2424
    let fileNoArgsfetchOp = {
    selections: '',
    selectionArgs: [],
2425
    };
P
panqiangbiao 已提交
2426 2427 2428 2429 2430 2431 2432 2433
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
        console.info("getFileAssets successfully:"+ JSON.stringify(albumFetchFileResult));
    }).catch(function(err){
        console.info("getFileAssets failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2434 2435
```

P
panqiangbiao 已提交
2436
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2437

P
panqiangbiao 已提交
2438
注册设备的信息。
2439 2440

**系统接口**:此接口为系统接口。
P
panqiangbiao 已提交
2441

Z
zhang-daiyue 已提交
2442
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.DistributedCore
Z
zengyawen 已提交
2443

Z
zengyawen 已提交
2444 2445 2446 2447 2448 2449
| 名称       | 类型                       | 可读 | 可写 | 说明             |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | 是   | 否   | 注册设备的名称   |
| networkId  | string                     | 是   | 否   | 注册设备的网络ID |
| deviceType | [DeviceType](#devicetype8) | 是   | 否   | 设备类型         |
| isOnline   | boolean                    | 是   | 否   | 是否在线         |
P
panqiangbiao 已提交
2450 2451 2452



Z
zengyawen 已提交
2453
## MediaType<sup>8+</sup>
P
panqiangbiao 已提交
2454 2455 2456

枚举,媒体类型。

Z
zengyawen 已提交
2457 2458
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

H
huweiqi 已提交
2459 2460 2461 2462 2463 2464
| 名称  |  值 |  说明 |
| ----- |  ---- | ---- |
| FILE  |  0 | 文件 |
| IMAGE |  1 | 图片 |
| VIDEO |  2 | 视频 |
| AUDIO |  3 | 音频 |
P
panqiangbiao 已提交
2465

Z
zengyawen 已提交
2466
## FileKey<sup>8+</sup>
P
panqiangbiao 已提交
2467 2468 2469

枚举,文件关键信息。

Z
zengyawen 已提交
2470 2471
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

H
huweiqi 已提交
2472
| 名称          | 值              | 说明                                                       |
Z
zengyawen 已提交
2473
| ------------- | ------------------- | ---------------------------------------------------------- |
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
| ID            | "file_id"             | 文件编号                                                   |
| RELATIVE_PATH | "relative_path"       | 相对公共目录路径                                           |
| DISPLAY_NAME  | "display_name"        | 显示名字                                                   |
| PARENT        | "parent"              | 父目录id                                                   |
| MIME_TYPE     | "mime_type"           | 文件扩展属性                                               |
| MEDIA_TYPE    | "media_type"          | 媒体类型                                                   |
| SIZE          | "size"                | 文件大小(单位:字节)                                     |
| DATE_ADDED    | "date_added"          | 添加日期(添加文件时间到1970年1月1日的秒数值)             |
| DATE_MODIFIED | "date_modified"       | 修改日期(修改文件时间到1970年1月1日的秒数值)             |
| DATE_TAKEN    | "date_taken"          | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)             |
| TITLE         | "title"               | 文件标题                                                   |
| ARTIST        | "artist"              | 作者                                                       |
| AUDIOALBUM    | "audio_album"         | 专辑                                                       |
| DURATION      | "duration"            | 持续时间(单位:毫秒)                                       |
| WIDTH         | "width"               | 图片宽度(单位:像素)                                     |
| HEIGHT        | "height"              | 图片高度(单位:像素)                                     |
| ORIENTATION   | "orientation"         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
| ALBUM_ID      | "bucket_id"           | 文件所归属的相册编号                                       |
| ALBUM_NAME    | "bucket_display_name" | 文件所归属相册名称                                         |
P
panqiangbiao 已提交
2493

Z
zengyawen 已提交
2494
## DirectoryType<sup>8+</sup>
P
panqiangbiao 已提交
2495 2496 2497

枚举,目录类型。

Z
zengyawen 已提交
2498 2499
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

H
huweiqi 已提交
2500 2501 2502 2503 2504 2505 2506 2507
| 名称          | 值 |  说明               |
| ------------- | --- | ------------------ |
| DIR_CAMERA    |  0 | 表示Camera文件路径 |
| DIR_VIDEO     |  1 |  表示视频路径       |
| DIR_IMAGE     |  2 | 表示图片路径       |
| DIR_AUDIO     |  3 | 表示音频路径       |
| DIR_DOCUMENTS |  4 | 表示文档路径       |
| DIR_DOWNLOAD  |  5 |  表示下载路径       |
P
panqiangbiao 已提交
2508

Z
zengyawen 已提交
2509
## DeviceType<sup>8+</sup>
P
panqiangbiao 已提交
2510 2511

枚举,设备类型。
2512 2513

**系统接口**:此接口为系统接口。
P
panqiangbiao 已提交
2514

Z
zhang-daiyue 已提交
2515
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.DistributedCore
Z
zengyawen 已提交
2516

H
huweiqi 已提交
2517 2518 2519 2520 2521 2522 2523 2524 2525
| 名称         |  值 | 说明       |
| ------------ | --- | ---------- |
| TYPE_UNKNOWN |  0 | 未识别设备 |
| TYPE_LAPTOP  |  1 | 笔记本电脑 |
| TYPE_PHONE   |  2 | 手机       |
| TYPE_TABLET  |  3 | 平板电脑   |
| TYPE_WATCH   |  4 | 智能手表   |
| TYPE_CAR     |  5 | 车载设备   |
| TYPE_TV      |  6 | 电视设备   |
P
panqiangbiao 已提交
2526

Z
zengyawen 已提交
2527
## MediaFetchOptions<sup>7+</sup>
P
panqiangbiao 已提交
2528 2529 2530

检索条件。

Z
zengyawen 已提交
2531 2532
**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

H
huweiqi 已提交
2533 2534 2535 2536 2537 2538 2539 2540
| 名称                    | 类型                | 可读 | 可写 | 说明                                                         |
| ----------------------- | ------------------- | ---- | ---- | ------------------------------------------------------------ |
| selections              | string              | 是   | 是   | 检索条件,使用[FileKey](#filekey8)中的枚举值作为检索条件的列名。示例:<br />selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR ' +mediaLibrary.FileKey.MEDIA_TYPE + '= ?', |
| selectionArgs           | Array&lt;string&gt; | 是   | 是   | 检索条件的值,对应selections中检索条件列的值。<br />示例:<br />selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
| order                   | string              | 是   | 是   | 检索结果排序方式,使用[FileKey](#filekey8)中的枚举值作为检索结果排序的列,可以用升序或降序排列。示例:<br />升序排列:order: mediaLibrary.FileKey.DATE_ADDED + " ASC"<br />降序排列:order: mediaLibrary.FileKey.DATE_ADDED + " DESC" |
| uri<sup>8+</sup>        | string              | 是   | 是   | 文件URI                                                      |
| networkId<sup>8+</sup>  | string              | 是   | 是   | 注册设备网络ID                                               |
| extendArgs<sup>8+</sup> | string              | 是   | 是   | 扩展的检索参数,目前没有扩展检索参数                         |
P
panqiangbiao 已提交
2541

P
panqiangbiao 已提交
2542
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2543 2544

图片尺寸。
2545 2546

**系统能力:**  以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core
P
panqiangbiao 已提交
2547

H
HelloCrease 已提交
2548 2549 2550 2551
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2552

Z
update  
zengyawen 已提交
2553
## MediaAssetOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2554 2555 2556

媒体资源选项。

Z
update  
zengyawen 已提交
2557
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2558 2559 2560 2561

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core


H
huweiqi 已提交
2562 2563 2564 2565 2566
| 名称         | 类型   | 可读 | 可写 | 说明                                                         |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| src          | string | 是   | 是   | 本地文件应用沙箱路径。                                       |
| mimeType     | string | 是   | 是   | 媒体MIME(Multipurpose&nbsp;Internet&nbsp;Mail&nbsp;Extensions)类型。<br/>包括:'image/\*'、'video/\*'、'audio/\*'、 'file\*'。 |
| relativePath | string | 是   | 是   | 自定义媒体资源保存位置,例:Pictures/ 不填则保存到默认路径。 <br/> image类型默认路径Pictures/ <br/> video类型默认路径Videos/ <br/> audio类型默认路径Audios/ <br/> file类型默认路径Documents/ 。 |
P
panqiangbiao 已提交
2567

Z
update  
zengyawen 已提交
2568
## MediaSelectOption<sup>(deprecated)</sup>
P
panqiangbiao 已提交
2569 2570 2571

媒体资源类型选项。

Z
update  
zengyawen 已提交
2572
> **说明**: 从API Version 9开始废弃。
P
panqiangbiao 已提交
2573 2574 2575

**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core

H
huweiqi 已提交
2576 2577
| 名称    | 类型     | 可读 | 可写 | 说明                   |
| ----- | ------ | ---- | ---- | -------------------- |
2578
| type  | 'image' &#124; 'video' &#124; 'media' | 是    | 是  | 媒体类型,包括:image, video, media,当前仅支持media类型 |
H
huweiqi 已提交
2579
| count | number | 是    | 是  | 媒体选择,count = 1表示单选,count大于1表示多选。            |
Z
zhang-daiyue 已提交
2580

H
huweiqi 已提交
2581