js-apis-medialibrary.md 77.2 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
## 导入模块
P
panqiangbiao 已提交
7
```
P
panqiangbiao 已提交
8
import mediaLibrary from '@ohos.multimedia.medialibrary';
P
panqiangbiao 已提交
9 10
```

P
panqiangbiao 已提交
11
## mediaLibrary.getMediaLibrary
P
panqiangbiao 已提交
12

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

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

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

P
panqiangbiao 已提交
19
**参数:** 
P
panqiangbiao 已提交
20

H
HelloCrease 已提交
21 22 23
| 参数名     | 类型      | 必填   | 说明                                       |
| ------- | ------- | ---- | ---------------------------------------- |
| context | Context | 是    | API7以下接口可不传此参数,API 8以上接口此参数是必填参数,传入Ability实例的context,获取媒体库 |
P
panqiangbiao 已提交
24 25 26

**返回值:**

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

**示例:**

```
P
panqiangbiao 已提交
34
import featureAbility from '@ohos.ability.featureAbility';
P
panqiangbiao 已提交
35

P
panqiangbiao 已提交
36
var context = featureAbility.getContext()
P
panqiangbiao 已提交
37
var media = mediaLibrary.getMediaLibrary(context);
P
panqiangbiao 已提交
38
```
P
panqiangbiao 已提交
39
## MediaLibrary
P
panqiangbiao 已提交
40

P
panqiangbiao 已提交
41
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
42 43


P
panqiangbiao 已提交
44
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 
P
panqiangbiao 已提交
45 46 47

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

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

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

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

H
HelloCrease 已提交
54 55 56 57
| 参数名      | 类型                                       | 必填   | 说明                       |
| -------- | ---------------------------------------- | ---- | ------------------------ |
| options  | [MediaFetchOptions](#mediafetchoptions8) | 是    | 文件获取选项                   |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult8)> | 是    | 异步获取FetchFileResult之后的回调 |
P
panqiangbiao 已提交
58 59 60 61

**示例:**

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

P
panqiangbiao 已提交
81
getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
82 83 84

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

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

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

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

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

**返回值**

H
HelloCrease 已提交
97 98
| 类型                                   | 说明      |
| ------------------------------------ | ------- |
P
panqiangbiao 已提交
99
| [FetchFileResult](#fetchfileresult8) | 文件数据结果集 |
P
panqiangbiao 已提交
100 101 102 103

**示例:**

```
P
panqiangbiao 已提交
104 105 106 107 108 109
let fileKeyObj = mediaLibrary.FileKey
let imageType = mediaLibrary.MediaType.IMAGE
let imagesfetchOp = {
    selections: fileKeyObj.MEDIA_TYPE + '= ?',
    selectionArgs: [imageType.toString()],
};
P
panqiangbiao 已提交
110
mediaLibrary.getFileAssets(imagesfetchOp).then(function(fetchFileResult){
P
panqiangbiao 已提交
111 112 113 114
    console.info("getFileAssets successfully:"+ JSON.stringify(dir));
}).catch(function(err){
    console.info("getFileAssets failed with error:"+ err);
});
P
panqiangbiao 已提交
115 116
```

P
panqiangbiao 已提交
117
### on<sup>8+</sup>
P
panqiangbiao 已提交
118

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

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

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

P
panqiangbiao 已提交
125 126
**参数:**

H
HelloCrease 已提交
127 128 129 130
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | 是    | 媒体类型 <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 已提交
131 132 133 134

**示例:**

```
P
panqiangbiao 已提交
135
mediaLibrary.on('imageChange', () => {
P
panqiangbiao 已提交
136
    // image file had changed, do something
P
panqiangbiao 已提交
137 138
})
```
P
panqiangbiao 已提交
139
### off<sup>8+</sup>
P
panqiangbiao 已提交
140

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

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

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

P
panqiangbiao 已提交
147 148
**参数:**

H
HelloCrease 已提交
149 150 151 152
| 参数名      | 类型                   | 必填   | 说明                                       |
| -------- | -------------------- | ---- | ---------------------------------------- |
| type     | string               | 是    | 媒体类型 <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 已提交
153 154 155 156

**示例:**

```
P
panqiangbiao 已提交
157
mediaLibrary.off('imageChange', () => {
P
panqiangbiao 已提交
158
    // stop listening success
P
panqiangbiao 已提交
159 160 161
})
```

P
panqiangbiao 已提交
162
### createAsset <sup>8+</sup>
P
panqiangbiao 已提交
163

P
panqiangbiao 已提交
164
createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
165 166 167

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

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

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

P
panqiangbiao 已提交
172 173
**参数:**

H
HelloCrease 已提交
174 175 176 177 178 179
| 参数名          | 类型                                      | 必填   | 说明                                       |
| ------------ | --------------------------------------- | ---- | ---------------------------------------- |
| mediaType    | [MediaType](#mediatype)                 | 是    | 媒体类型                                     |
| displayName  | string                                  | 是    | 展示文件名                                    |
| relativePath | string                                  | 是    | 文件保存路径,可以通过[getPublicDirectory](#getpublicdirectory8)获取不同类型文件的保存路径 |
| callback     | AsyncCallback<[FileAsset](#fileasset8)> | 是    | 异步获取媒体数据FileAsset之后的回调                   |
P
panqiangbiao 已提交
180 181 182 183

**示例:**

```
P
panqiangbiao 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196
async function example() {
    // 使用Callback方式创建Image类型文件
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
    mediaLibrary.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (err, fileAsset) => {
        if (fileAsset != undefined) {
            console.info('createAsset successfully, message = ' + err);
        } else {
            console.info('createAsset failed, message = ' + err);
        }
    });
}
P
panqiangbiao 已提交
197 198
```

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

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

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

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

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

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

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

**返回值**

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

**示例:**

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

P
panqiangbiao 已提交
239
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
240

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

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

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

**参数:**

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

**示例:**

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

P
panqiangbiao 已提交
267
### getPublicDirectory<sup>8+</sup>
P
panqiangbiao 已提交
268

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

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

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

**参数:**

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

**返回值:**

H
HelloCrease 已提交
283 284
| 类型              | 说明       |
| --------------- | -------- |
P
panqiangbiao 已提交
285 286 287 288 289
| Promise<string> | 返回公共目录路径 |

**示例:**

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

P
panqiangbiao 已提交
301
### getAlbums<sup>8+</sup>
P
panqiangbiao 已提交
302

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

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

P
panqiangbiao 已提交
307
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
308

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

P
panqiangbiao 已提交
311 312
**参数**

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

**示例:**

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

P
panqiangbiao 已提交
336
### getAlbums<sup>8+</sup>
P
panqiangbiao 已提交
337

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

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

P
panqiangbiao 已提交
342
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
343

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

P
panqiangbiao 已提交
346 347
**参数:**

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

**返回值:**

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

**示例:**

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

P
panqiangbiao 已提交
372
### release<sup>8+</sup>
P
panqiangbiao 已提交
373

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

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

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

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

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

**示例:**

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

P
panqiangbiao 已提交
396
### release<sup>8+</sup>
P
panqiangbiao 已提交
397

P
panqiangbiao 已提交
398
release(): Promise&lt;void&gt;
P
panqiangbiao 已提交
399

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

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

P
panqiangbiao 已提交
405 406
**返回值:**

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

**示例:**

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

P
panqiangbiao 已提交
418 419 420 421 422 423 424 425 426 427 428 429
### storeMediaAsset

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
430 431 432 433
| 参数名      | 类型                                    | 必填   | 说明                      |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。                 |
| callback | AsyncCallback&lt;string&gt;           | 是    | 媒体资源保存回调,返回保存成功后得到的URI。 |
P
panqiangbiao 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

**示例:**

  ```
let option = {
    src : "file:///data/data/ohos.xxx.yyy/files/image.png",
    mimeType : "image/jpeg",
    relativePath : "imageDir/image2/"
};
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.
});
  ```


### storeMediaAsset

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
466 467 468
| 参数名    | 类型                                    | 必填   | 说明      |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | 是    | 媒体资源选项。 |
P
panqiangbiao 已提交
469 470 471

**返回值:**

H
HelloCrease 已提交
472 473
| 类型                    | 说明                           |
| --------------------- | ---------------------------- |
P
panqiangbiao 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
| Promise&lt;string&gt; | Promise实例,用于异步获取保存成功后得到的URI。 |

**示例:**

  ```
let option = {
    src : "file:///data/data/ohos.xxx.yyy/files/image.jpg",
    mimeType : "image/jpeg",
    relativePath : "imageDir/image2/"
};
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.");
});
  ```


### startImagePreview

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

启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
505 506 507 508 509
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","dataability://")列表。 |
| index    | number                    | 是    | 开始显示的图片序号。                               |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
510 511 512 513 514 515 516 517

**示例:**

  ```
let images = [
    "dataability:///media/external/images/media/50",
    "dataability:///media/external/images/media/55"
];
H
HelloCrease 已提交
518
/* 网络图片使用方式
P
panqiangbiao 已提交
519 520 521 522
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
523
*/
P
panqiangbiao 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
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.");
});
  ```


### startImagePreview

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

启动图片预览界面,可以预览列表中首张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用callback方式进行异步回调。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
547 548 549 550
| 参数名      | 类型                        | 必填   | 说明                                       |
| -------- | ------------------------- | ---- | ---------------------------------------- |
| images   | Array&lt;string&gt;       | 是    | 预览的图片URI("https://","dataability://")列表。 |
| callback | AsyncCallback&lt;void&gt; | 是    | 图片预览回调,失败时返回错误信息。                        |
P
panqiangbiao 已提交
551 552 553 554 555 556 557 558

**示例:**

  ```
let images = [
    "dataability:///media/external/images/media/50",
    "dataability:///media/external/images/media/55"
];
H
HelloCrease 已提交
559
/* 网络图片使用方式
P
panqiangbiao 已提交
560 561 562 563
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
564
*/
P
panqiangbiao 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => {
    if (err) {
        console.log("An error occurred when previewing the images.");
        return;
    }
    console.log("Succeeded in previewing the images.");
});
  ```


### startImagePreview

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

启动图片预览界面并限定预览开始显示的图片。可以预览指定序号的单张本地图片(dataability://),也可以预览列表中的所有网络图片(https://)。使用Promise方式进行异步回调。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
587 588 589 590
| 参数名    | 类型                  | 必填   | 说明                                       |
| ------ | ------------------- | ---- | ---------------------------------------- |
| images | Array&lt;string&gt; | 是    | 预览的图片URI("https://","dataability://")列表。 |
| index  | number              | 否    | 开始显示的图片序号,不选择时默认为0。                      |
P
panqiangbiao 已提交
591 592 593

**返回值:**

H
HelloCrease 已提交
594 595
| 类型                  | 说明                              |
| ------------------- | ------------------------------- |
P
panqiangbiao 已提交
596 597 598 599 600 601 602 603 604
| Promise&lt;void&gt; | Promise实例,用于异步获取预览结果,失败时返回错误信息。 |

**示例:**

  ```
let images = [
    "dataability:///media/external/images/media/50",
    "dataability:///media/external/images/media/55"
];
H
HelloCrease 已提交
605
/* 网络图片使用方式
P
panqiangbiao 已提交
606 607 608 609
let images = [
    "https://media.xxxx.com/image1.jpg",
    "https://media.xxxx.com/image2.jpg"
];
H
HelloCrease 已提交
610
*/
P
panqiangbiao 已提交
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
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.");
});
  ```


### startMediaSelect

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
632 633 634 635
| 参数名      | 类型                                       | 必填   | 说明                                   |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
| option   | [MediaSelectOption](#mediaselectoption)  | 是    | 媒体选择选项。                              |
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | 是    | 媒体选择回调,返回选择的媒体URI(dataability://)列表。 |
P
panqiangbiao 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

**示例:**

  ```
let option = {
    type : "image",
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => {
    if (err) {
        console.log("An error occurred when selecting media resources.");
        return;
    }
    console.log("Media resources selected.");
    // Obtain the media selection value.
});
  ```


### startMediaSelect

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

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

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

**参数:**

H
HelloCrease 已提交
667 668 669
| 参数名    | 类型                                      | 必填   | 说明      |
| ------ | --------------------------------------- | ---- | ------- |
| option | [MediaSelectOption](#mediaselectoption) | 是    | 媒体选择选项。 |
P
panqiangbiao 已提交
670 671 672

**返回值:**

H
HelloCrease 已提交
673 674
| 类型                                 | 说明                                       |
| ---------------------------------- | ---------------------------------------- |
P
panqiangbiao 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
| Promise&lt;Array&lt;string&gt;&gt; | Promise实例,用于异步获取选择的媒体URI(dataability://)列表。 |

**示例:**

  ```
let option = {
    type : "image",
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
    console.log("Media resources selected.");
    // Obtain the media selection value.
}).catch((err) => {
    console.log("An error occurred when selecting media resources.");
});

  ```

P
panqiangbiao 已提交
693 694 695 696
## FileAsset<sup>8+</sup>

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

Z
zengyawen 已提交
697 698 699
### 属性

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

H
HelloCrease 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
| 名称           | 类型                      | 可读   | 可写   | 说明                                      |
| ------------ | ----------------------- | ---- | ---- | --------------------------------------- |
| id           | number                  | 是    | 否    | 文件资源编号                                  |
| uri          | string                  | 是    | 否    | 文件资源uri(如:dataability:///media/image/2) |
| mimeType     | string                  | 是    | 否    | 文件扩展属性                                  |
| mediaType    | [MediaType](#mediatype) | 是    | 否    | 媒体类型                                    |
| displayName  | string                  | 是    | 是    | 显示文件名,包含后缀名                             |
| title        | string                  | 是    | 是    | 文件标题                                    |
| relativePath | string                  | 是    | 是    | 相对公共目录路径                                |
| parent       | number                  | 是    | 否    | 父目录id                                   |
| size         | number                  | 是    | 否    | 文件大小(单位:字节)                             |
| dateAdded    | number                  | 是    | 否    | 添加日期(添加文件时间到1970年1月1日的秒数值)              |
| dateModified | number                  | 是    | 否    | 修改日期(修改文件时间到1970年1月1日的秒数值)              |
| dateTaken    | number                  | 是    | 否    | 拍摄日期(文件拍照时间到1970年1月1日的秒数值)              |
| artist       | string                  | 是    | 否    | 作者                                      |
| audioAlbum   | string                  | 是    | 否    | 专辑                                      |
| width        | number                  | 是    | 否    | 图片宽度(单位:像素)                             |
| height       | number                  | 是    | 否    | 图片高度(单位:像素)                             |
| orientation  | number                  | 是    | 是    | 图片显示方向(顺时针旋转角度,如0,90,180  单位:度)         |
| duration     | number                  | 是    | 否    | 持续时间(单位:秒)                              |
| albumId      | number                  | 是    | 否    | 文件所归属的相册编号                              |
| albumUri     | string                  | 是    | 否    | 文件所归属相册uri                              |
| albumName    | string                  | 是    | 否    | 文件所归属相册名称                               |
P
panqiangbiao 已提交
724 725 726


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

P
panqiangbiao 已提交
728
isDirectory(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
729 730 731

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

P
panqiangbiao 已提交
732
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
733

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

P
panqiangbiao 已提交
736 737
**参数:**

H
HelloCrease 已提交
738 739 740
| 参数名      | 类型                           | 必填   | 说明                  |
| -------- | ---------------------------- | ---- | ------------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 当前FileAsset是否是目录的回调 |
P
panqiangbiao 已提交
741 742 743 744

**示例:**

```
P
panqiangbiao 已提交
745
async function example() {
P
panqiangbiao 已提交
746 747 748 749
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
750 751
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
752 753 754 755 756 757 758
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isDirectory((err, isDirectory) => {
        // do something
    });
}
P
panqiangbiao 已提交
759 760
```

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

P
panqiangbiao 已提交
763
isDirectory():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
764 765 766

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

P
panqiangbiao 已提交
767
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
768

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

P
panqiangbiao 已提交
771 772
**返回值:**

H
HelloCrease 已提交
773 774
| 类型                     | 说明                           |
| ---------------------- | ---------------------------- |
P
panqiangbiao 已提交
775
| Promise&lt;boolean&gt; | Promise实例,返回当前FileAsset是否是目录 |
P
panqiangbiao 已提交
776 777 778 779

**示例:**

```
P
panqiangbiao 已提交
780
async function example() {
P
panqiangbiao 已提交
781 782 783 784
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
785 786
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
787 788 789 790 791 792 793 794 795
    };
    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 已提交
796 797
```

P
panqiangbiao 已提交
798
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
799

P
panqiangbiao 已提交
800
commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
801 802 803

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

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

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

P
panqiangbiao 已提交
808 809
**参数:**

H
HelloCrease 已提交
810 811 812
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
813 814 815 816

**示例:**

```
P
panqiangbiao 已提交
817
async function example() {
P
panqiangbiao 已提交
818 819 820 821
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
822 823
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
824 825 826
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
827
    asset.title = 'newtitle';
P
panqiangbiao 已提交
828 829
    asset.commitModify(() => {
        console.info('commitModify success');   
P
panqiangbiao 已提交
830
    });
P
panqiangbiao 已提交
831
}
P
panqiangbiao 已提交
832 833
```

P
panqiangbiao 已提交
834
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
835

P
panqiangbiao 已提交
836
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
837 838 839

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

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

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

P
panqiangbiao 已提交
844 845
**返回值:**

H
HelloCrease 已提交
846 847
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
848
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
849 850 851 852

**示例:**

```
P
panqiangbiao 已提交
853
async function example() {
P
panqiangbiao 已提交
854 855 856 857
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
858 859
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
860 861 862
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
P
panqiangbiao 已提交
863
    asset.title = 'newtitle';
P
panqiangbiao 已提交
864 865
    asset.commitModify();
}
P
panqiangbiao 已提交
866 867
```

P
panqiangbiao 已提交
868
### open<sup>8+</sup>
P
panqiangbiao 已提交
869

P
panqiangbiao 已提交
870
open(mode: string, callback: AsyncCallback&lt;number&gt;): void
P
panqiangbiao 已提交
871 872 873

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

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

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

P
panqiangbiao 已提交
878 879
**参数**

H
HelloCrease 已提交
880 881 882 883
| 参数名      | 类型                          | 必填   | 说明                                  |
| -------- | --------------------------- | ---- | ----------------------------------- |
| mode     | string                      | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
| callback | AsyncCallback&lt;number&gt; | 是    | 回调返回文件句柄                            |
P
panqiangbiao 已提交
884 885 886 887

**示例:**

```
P
panqiangbiao 已提交
888
async function example() {
P
panqiangbiao 已提交
889
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
890 891
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
P
panqiangbiao 已提交
892 893 894 895 896 897 898 899 900
    asset = await media.createAsset(mediaType, "image00003.jpg", path);
    asset.open('rw', (openError, fd) => {
            if(fd > 0){
                asset.close(fd);
            }else{
                console.info('File Open Failed!' + openError);
            }
    });
}
P
panqiangbiao 已提交
901 902
```

P
panqiangbiao 已提交
903
### open<sup>8+</sup>
P
panqiangbiao 已提交
904

P
panqiangbiao 已提交
905
open(mode: string): Promise&lt;number&gt;
P
panqiangbiao 已提交
906 907 908

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

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

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

P
panqiangbiao 已提交
913 914
**参数:**

H
HelloCrease 已提交
915 916 917
| 参数名  | 类型     | 必填   | 说明                                  |
| ---- | ------ | ---- | ----------------------------------- |
| mode | string | 是    | 打开文件方式,如:'r'(只读), 'w'(只写), 'rw'(读写) |
P
panqiangbiao 已提交
918 919 920

**返回值:**

H
HelloCrease 已提交
921 922
| 类型                    | 说明            |
| --------------------- | ------------- |
P
panqiangbiao 已提交
923
| Promise&lt;number&gt; | Promise返回文件句柄 |
P
panqiangbiao 已提交
924 925 926 927

**示例:**

```
P
panqiangbiao 已提交
928
async function example() {
P
panqiangbiao 已提交
929
    let mediaType = mediaLibrary.MediaType.IMAGE;
P
panqiangbiao 已提交
930 931
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
P
panqiangbiao 已提交
932
    asset = await media.createAsset(mediaType, "image00003.jpg", path);
P
panqiangbiao 已提交
933 934 935 936 937 938 939
    asset.open('rw')
        .then((fd) => {
            console.info('File fd!' + fd);
        })
        .catch((err) => {
            console.info('File err!' + err);
        });
P
panqiangbiao 已提交
940
}
P
panqiangbiao 已提交
941 942
```

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

P
panqiangbiao 已提交
945
close(fd: number, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
946 947 948

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

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

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

P
panqiangbiao 已提交
953 954
**参数:**

H
HelloCrease 已提交
955 956 957 958
| 参数名      | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | 是    | 文件描述符 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
959 960 961 962

**示例:**

```
P
panqiangbiao 已提交
963
async function example() {
P
panqiangbiao 已提交
964 965 966 967
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
968 969
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
970 971 972 973 974
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.close(fd, (closeErr) => {
        if (closeErr != undefined) {
P
panqiangbiao 已提交
975 976
            console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
            console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
977 978 979 980 981
        } else {
            console.info("=======asset.close success====>");
        }
    });
}
P
panqiangbiao 已提交
982 983
```

P
panqiangbiao 已提交
984
### close<sup>8+</sup>
P
panqiangbiao 已提交
985

P
panqiangbiao 已提交
986
close(fd: number): Promise&lt;void&gt;
P
panqiangbiao 已提交
987 988 989

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

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

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

P
panqiangbiao 已提交
994 995
**参数:**

H
HelloCrease 已提交
996 997 998
| 参数名  | 类型     | 必填   | 说明    |
| ---- | ------ | ---- | ----- |
| fd   | number | 是    | 文件描述符 |
P
panqiangbiao 已提交
999 1000 1001

**返回值:**

H
HelloCrease 已提交
1002 1003
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1004
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1005 1006 1007 1008

**示例:**

```
P
panqiangbiao 已提交
1009
async function example() {
P
panqiangbiao 已提交
1010 1011 1012 1013
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1014 1015
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1016 1017 1018 1019 1020
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.close(fd).then((closeErr) => {
        if (closeErr != undefined) {
P
panqiangbiao 已提交
1021 1022
            console.info('mediaLibraryTest : close : FAIL ' + closeErr.message);
            console.info('mediaLibraryTest : ASSET_CALLBACK : FAIL');
P
panqiangbiao 已提交
1023 1024 1025 1026 1027 1028

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

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

P
panqiangbiao 已提交
1033
getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1034 1035 1036

获取文件的缩略图,使用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;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1046 1047 1048 1049

**示例:**

```
P
panqiangbiao 已提交
1050
async function example() {
P
panqiangbiao 已提交
1051 1052 1053 1054
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1055 1056
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1057 1058 1059 1060
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail((err, pixelmap) => {
P
panqiangbiao 已提交
1061
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
1062 1063
    });
}
P
panqiangbiao 已提交
1064 1065
```

P
panqiangbiao 已提交
1066
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1067

P
panqiangbiao 已提交
1068
getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void
P
panqiangbiao 已提交
1069 1070 1071

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

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

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

P
panqiangbiao 已提交
1076 1077
**参数:**

H
HelloCrease 已提交
1078 1079 1080 1081
| 参数名      | 类型                                  | 必填   | 说明               |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | 是    | 缩略图尺寸            |
| callback | AsyncCallback&lt;image.PixelMap&gt; | 是    | 回调返回缩略图的PixelMap |
P
panqiangbiao 已提交
1082 1083 1084 1085

**示例:**

```
P
panqiangbiao 已提交
1086
async function example() {
P
panqiangbiao 已提交
1087 1088 1089 1090
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1091 1092
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1093 1094 1095 1096
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
P
panqiangbiao 已提交
1097
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
1098 1099
    });
}
P
panqiangbiao 已提交
1100 1101
```

P
panqiangbiao 已提交
1102
### getThumbnail<sup>8+</sup>
P
panqiangbiao 已提交
1103

P
panqiangbiao 已提交
1104
getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;
P
panqiangbiao 已提交
1105 1106 1107

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

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

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

P
panqiangbiao 已提交
1112 1113
**参数:**

H
HelloCrease 已提交
1114 1115 1116
| 参数名  | 类型             | 必填   | 说明    |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | 否    | 缩略图尺寸 |
P
panqiangbiao 已提交
1117 1118 1119

**返回值:**

H
HelloCrease 已提交
1120 1121
| 类型                            | 说明                    |
| ----------------------------- | --------------------- |
P
panqiangbiao 已提交
1122
| Promise&lt;image.PixelMap&gt; | Promise返回缩略图的PixelMap |
P
panqiangbiao 已提交
1123 1124 1125 1126

**示例:**

```
P
panqiangbiao 已提交
1127
async function example() {
P
panqiangbiao 已提交
1128 1129 1130 1131
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1132 1133
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1134 1135 1136 1137
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.getThumbnail(size, (err, pixelmap) => {
P
panqiangbiao 已提交
1138
        console.info('mediaLibraryTest : getThumbnail Successfull '+ pixelmap);
P
panqiangbiao 已提交
1139 1140
    });
}
P
panqiangbiao 已提交
1141 1142
```

P
panqiangbiao 已提交
1143
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1144

P
panqiangbiao 已提交
1145
favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1146 1147 1148

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

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

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

P
panqiangbiao 已提交
1153 1154
**参数:**

H
HelloCrease 已提交
1155 1156 1157 1158
| 参数名        | 类型                        | 必填   | 说明                                 |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
| callback   | AsyncCallback&lt;void&gt; | 是    | 回调返回空                              |
P
panqiangbiao 已提交
1159 1160 1161 1162

**示例:**

```
P
panqiangbiao 已提交
1163
async function example() {
P
panqiangbiao 已提交
1164 1165 1166 1167
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1168 1169
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1170 1171 1172 1173 1174 1175 1176
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.favorite(true,function(err){
        // do something
    });
}
P
panqiangbiao 已提交
1177 1178
```

P
panqiangbiao 已提交
1179
### favorite<sup>8+</sup>
P
panqiangbiao 已提交
1180

P
panqiangbiao 已提交
1181
favorite(isFavorite: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1182 1183 1184

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

P
panqiangbiao 已提交
1185
**需要权限**:ohos.permission.READ_MEDIA, 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
| 参数名        | 类型      | 必填   | 说明                                 |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | 是    | 是否设置为收藏文件, true:设置为收藏文件,false:取消收藏 |
P
panqiangbiao 已提交
1194 1195 1196

**返回值:**

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

**示例:**

```
P
panqiangbiao 已提交
1204
async function example() {
P
panqiangbiao 已提交
1205 1206 1207 1208
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1209 1210
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219
    };
    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 已提交
1220 1221
```

P
panqiangbiao 已提交
1222
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1223

P
panqiangbiao 已提交
1224
isFavorite(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1225 1226 1227

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

P
panqiangbiao 已提交
1228
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1229

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

P
panqiangbiao 已提交
1232 1233
**参数:**

H
HelloCrease 已提交
1234 1235 1236
| 参数名      | 类型                           | 必填   | 说明          |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调表示是否为收藏文件 |
P
panqiangbiao 已提交
1237 1238 1239 1240

**示例:**

```
P
panqiangbiao 已提交
1241
async function example() {
P
panqiangbiao 已提交
1242 1243 1244 1245
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1246 1247
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
    };
    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 已提交
1259 1260
```

P
panqiangbiao 已提交
1261
### isFavorite<sup>8+</sup>
P
panqiangbiao 已提交
1262

P
panqiangbiao 已提交
1263
isFavorite():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1264 1265 1266

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

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

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

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

H
HelloCrease 已提交
1273 1274
| 类型                     | 说明                 |
| ---------------------- | ------------------ |
P
panqiangbiao 已提交
1275
| Promise&lt;boolean&gt; | Promise回调表示是否是收藏文件 |
P
panqiangbiao 已提交
1276 1277 1278 1279

**示例:**

```
P
panqiangbiao 已提交
1280
async function example() {
P
panqiangbiao 已提交
1281 1282 1283 1284
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1285 1286
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1287 1288 1289 1290 1291 1292 1293 1294 1295
    };
    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 已提交
1296 1297
```

P
panqiangbiao 已提交
1298
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1299

A
AOL 已提交
1300
trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1301 1302 1303

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

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

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

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

P
panqiangbiao 已提交
1310 1311
**参数:**

H
HelloCrease 已提交
1312 1313 1314 1315
| 参数名      | 类型                        | 必填   | 说明        |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | 是    | 是否设置为垃圾文件 |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空     |
P
panqiangbiao 已提交
1316 1317 1318 1319

**示例:**

```
P
panqiangbiao 已提交
1320
async function example() {
P
panqiangbiao 已提交
1321 1322 1323 1324
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1325 1326
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1327 1328 1329 1330 1331
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.trash(true, trashCallBack);
    function trashCallBack(err, trash) {
P
panqiangbiao 已提交
1332
        console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK trash');
P
panqiangbiao 已提交
1333
    }
P
panqiangbiao 已提交
1334
}
P
panqiangbiao 已提交
1335 1336
```

P
panqiangbiao 已提交
1337
### trash<sup>8+</sup>
P
panqiangbiao 已提交
1338

A
AOL 已提交
1339
trash(isTrash: boolean): Promise&lt;void&gt;
P
panqiangbiao 已提交
1340 1341 1342

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

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

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

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

P
panqiangbiao 已提交
1349 1350
**参数:**

H
HelloCrease 已提交
1351 1352 1353
| 参数名     | 类型      | 必填   | 说明        |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | 是    | 是否设置为垃圾文件 |
P
panqiangbiao 已提交
1354 1355 1356

**返回值:**

H
HelloCrease 已提交
1357 1358
| 类型                  | 说明         |
| ------------------- | ---------- |
P
panqiangbiao 已提交
1359
| Promise&lt;void&gt; | Promise返回空 |
P
panqiangbiao 已提交
1360 1361 1362 1363

**示例:**

```
P
panqiangbiao 已提交
1364
async function example() {
P
panqiangbiao 已提交
1365 1366 1367 1368
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1369 1370
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379
    };
    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 已提交
1380 1381
```

P
panqiangbiao 已提交
1382
### isTrash<sup>8+</sup>
P
panqiangbiao 已提交
1383

P
panqiangbiao 已提交
1384
isTrash(callback: AsyncCallback&lt;boolean&gt;): void
P
panqiangbiao 已提交
1385 1386 1387

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

P
panqiangbiao 已提交
1388
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1389

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

P
panqiangbiao 已提交
1392 1393
**参数:**

H
HelloCrease 已提交
1394 1395 1396
| 参数名      | 类型                           | 必填   | 说明              |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | 是    | 回调返回表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1397 1398 1399 1400

**示例:**

```
P
panqiangbiao 已提交
1401
async function example() {
P
panqiangbiao 已提交
1402 1403 1404 1405
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1406 1407
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1408 1409 1410 1411 1412 1413
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash(isTrashCallBack);
    function isTrashCallBack(err, isTrash) {
            if (isTrash == true) {
P
panqiangbiao 已提交
1414
                console.info('mediaLibraryTest : ASSET_CALLBACK ASSET_CALLBACK isTrash = ' + isTrash);
P
panqiangbiao 已提交
1415 1416 1417
                asset.trash(true, trashCallBack);

            } else {
P
panqiangbiao 已提交
1418 1419
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash Unsuccessfull = ' + err);
                console.info('mediaLibraryTest : ASSET_CALLBACK isTrash : FAIL');
P
panqiangbiao 已提交
1420 1421

            }
P
panqiangbiao 已提交
1422
    }
P
panqiangbiao 已提交
1423
}
P
panqiangbiao 已提交
1424 1425
```

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

P
panqiangbiao 已提交
1428
isTrash():Promise&lt;boolean&gt;
P
panqiangbiao 已提交
1429

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

P
panqiangbiao 已提交
1432
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1433

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

P
panqiangbiao 已提交
1436 1437
**返回值:**

H
HelloCrease 已提交
1438 1439
| 类型                  | 说明                   |
| ------------------- | -------------------- |
P
panqiangbiao 已提交
1440
| Promise&lt;void&gt; | Promise回调表示文件是否为垃圾文件 |
P
panqiangbiao 已提交
1441 1442 1443 1444

**示例:**

```
P
panqiangbiao 已提交
1445
async function example() {
P
panqiangbiao 已提交
1446 1447 1448 1449
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1450 1451
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.isTrash().then(function(isTrash){
        console.info("isTrash result:"+ isTrash);
    }).catch(function(err){
        console.info("isTrash failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
1461 1462
```

P
panqiangbiao 已提交
1463
## FetchFileResult<sup>8+</sup>
P
panqiangbiao 已提交
1464 1465 1466

文件检索结果集。

P
panqiangbiao 已提交
1467
### getCount<sup>8+</sup>
P
panqiangbiao 已提交
1468

P
panqiangbiao 已提交
1469
getCount(): number
P
panqiangbiao 已提交
1470 1471 1472

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

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

P
panqiangbiao 已提交
1475 1476
**返回值**

H
HelloCrease 已提交
1477 1478
| 类型     | 说明       |
| ------ | -------- |
P
panqiangbiao 已提交
1479
| number | 检索到的文件总数 |
P
panqiangbiao 已提交
1480 1481 1482 1483

**示例**

```
P
panqiangbiao 已提交
1484
async function example() {
P
panqiangbiao 已提交
1485 1486 1487
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
P
panqiangbiao 已提交
1488 1489
        order: fileKeyObj.DATE_ADDED + " DESC",
        extendArgs: "",
P
panqiangbiao 已提交
1490 1491 1492 1493
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
}
P
panqiangbiao 已提交
1494 1495
```

P
panqiangbiao 已提交
1496
### isAfterLast<sup>8+</sup>
P
panqiangbiao 已提交
1497

P
panqiangbiao 已提交
1498
isAfterLast(): boolean
P
panqiangbiao 已提交
1499 1500 1501

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

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

P
panqiangbiao 已提交
1504 1505
**返回值**

H
HelloCrease 已提交
1506 1507
| 类型      | 说明                                 |
| ------- | ---------------------------------- |
P
panqiangbiao 已提交
1508
| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 |
P
panqiangbiao 已提交
1509 1510 1511 1512

**示例**

```
P
panqiangbiao 已提交
1513
async function example() {
P
panqiangbiao 已提交
1514 1515 1516 1517
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1518 1519
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1520 1521 1522
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1523
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1524 1525 1526 1527
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
            fileAsset = await fetchFileResult.getNextObject();
            if(i == fetchCount - 1) {
P
panqiangbiao 已提交
1528
              console.info('mediaLibraryTest : isLast');
P
panqiangbiao 已提交
1529
              var result = fetchFileResult.isAfterLast();
P
panqiangbiao 已提交
1530 1531
              console.info('mediaLibraryTest : isAfterLast:' + result);
              console.info('mediaLibraryTest : isAfterLast end');
P
panqiangbiao 已提交
1532
              fetchFileResult.close();
P
panqiangbiao 已提交
1533

P
panqiangbiao 已提交
1534 1535
            }
    }
P
panqiangbiao 已提交
1536
}
P
panqiangbiao 已提交
1537 1538
```

P
panqiangbiao 已提交
1539
### close<sup>8+</sup>
P
panqiangbiao 已提交
1540

P
panqiangbiao 已提交
1541
close(): void
P
panqiangbiao 已提交
1542 1543 1544

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

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

P
panqiangbiao 已提交
1547 1548 1549
**示例**

```
P
panqiangbiao 已提交
1550
async function example() {
P
panqiangbiao 已提交
1551 1552 1553 1554
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1555 1556
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1557 1558 1559 1560
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
P
panqiangbiao 已提交
1561 1562
```

P
panqiangbiao 已提交
1563
### getFirstObject<sup>8+</sup>
P
panqiangbiao 已提交
1564

P
panqiangbiao 已提交
1565
getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1566 1567 1568

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

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

P
panqiangbiao 已提交
1571 1572
**参数**

H
HelloCrease 已提交
1573 1574 1575
| 参数名      | 类型                                       | 必填   | 说明                         |
| -------- | ---------------------------------------- | ---- | -------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是    | 异步获取结果集中第一个FileAsset完成后的回调 |
P
panqiangbiao 已提交
1576 1577 1578 1579

**示例**

```
P
panqiangbiao 已提交
1580
async function example() {
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
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getFirstObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1597 1598
```

P
panqiangbiao 已提交
1599
### getFirstObject<sup>8+</sup>
P
panqiangbiao 已提交
1600

P
panqiangbiao 已提交
1601
getFirstObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1602 1603 1604

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

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

P
panqiangbiao 已提交
1607 1608
**返回值**

H
HelloCrease 已提交
1609 1610
| 类型                                      | 说明                   |
| --------------------------------------- | -------------------- |
Z
zengyawen 已提交
1611
| Promise&lt;[FileAsset](#fileasset8)&gt; | Promise方式返回FileAsset |
P
panqiangbiao 已提交
1612 1613 1614 1615

**示例**

```
P
panqiangbiao 已提交
1616
async function example() {
P
panqiangbiao 已提交
1617 1618 1619 1620
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1621 1622
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1623 1624 1625 1626 1627 1628 1629 1630
    };
    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 已提交
1631 1632
```

P
panqiangbiao 已提交
1633
### getNextObject<sup>8+</sup>
P
panqiangbiao 已提交
1634

P
panqiangbiao 已提交
1635
 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1636 1637 1638

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

P
panqiangbiao 已提交
1639
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1640

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

P
panqiangbiao 已提交
1643 1644
**参数**

H
HelloCrease 已提交
1645 1646 1647
| 参数名       | 类型                                       | 必填   | 说明                        |
| --------- | ---------------------------------------- | ---- | ------------------------- |
| callbacke | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是    | 异步返回结果集中下一个FileAsset之后的回调 |
P
panqiangbiao 已提交
1648 1649 1650 1651

**示例**

```
P
panqiangbiao 已提交
1652
async function example() {
P
panqiangbiao 已提交
1653 1654 1655 1656
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1657 1658
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getNextObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1669 1670
```

P
panqiangbiao 已提交
1671
### getNextObject<sup>8+</sup>
P
panqiangbiao 已提交
1672

P
panqiangbiao 已提交
1673
 getNextObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1674 1675 1676

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

P
panqiangbiao 已提交
1677
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1678

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

P
panqiangbiao 已提交
1681 1682
**返回值**

H
HelloCrease 已提交
1683 1684
| 类型                                      | 说明            |
| --------------------------------------- | ------------- |
P
panqiangbiao 已提交
1685
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1686 1687 1688 1689

**示例**

```
P
panqiangbiao 已提交
1690
async function example() {
P
panqiangbiao 已提交
1691 1692 1693 1694
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1695 1696
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1697 1698 1699
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
P
panqiangbiao 已提交
1700
    console.info('mediaLibraryTest : count:' + fetchCount);
P
panqiangbiao 已提交
1701 1702
    fileAsset = await fetchFileResult.getNextObject();
}
P
panqiangbiao 已提交
1703 1704
```

P
panqiangbiao 已提交
1705
### getLastObject<sup>8+</sup>
P
panqiangbiao 已提交
1706

P
panqiangbiao 已提交
1707
getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1708 1709 1710

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

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

P
panqiangbiao 已提交
1713 1714
**参数**

H
HelloCrease 已提交
1715 1716 1717
| 参数       | 类型                                       | 必填   | 说明                 |
| -------- | ---------------------------------------- | ---- | ------------------ |
| callback | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是    | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1718 1719 1720 1721

**示例**

```
P
panqiangbiao 已提交
1722
async function example() {
P
panqiangbiao 已提交
1723 1724 1725 1726
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1727 1728
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getLastObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1739 1740
```

P
panqiangbiao 已提交
1741
### getLastObject<sup>8+</sup>
P
panqiangbiao 已提交
1742

P
panqiangbiao 已提交
1743
getLastObject(): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1744 1745 1746

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

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

P
panqiangbiao 已提交
1749 1750
**返回值**

H
HelloCrease 已提交
1751 1752
| 类型                                      | 说明            |
| --------------------------------------- | ------------- |
P
panqiangbiao 已提交
1753
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1754 1755 1756 1757

**示例**

```
P
panqiangbiao 已提交
1758
async function example() {
P
panqiangbiao 已提交
1759 1760 1761 1762
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1763 1764
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1765 1766 1767 1768
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    let lastObject = await fetchFileResult.getLastObject();
}
P
panqiangbiao 已提交
1769 1770
```

P
panqiangbiao 已提交
1771
### getPositionObject<sup>8+</sup>
P
panqiangbiao 已提交
1772

P
panqiangbiao 已提交
1773
getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void
P
panqiangbiao 已提交
1774 1775 1776

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

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

P
panqiangbiao 已提交
1779 1780
**参数**

H
HelloCrease 已提交
1781 1782 1783 1784
| 参数       | 类型                                       | 必填   | 说明                 |
| -------- | ---------------------------------------- | ---- | ------------------ |
| index    | number                                   | 是    | 要获取的文件的索引,从0开始     |
| callback | AsyncCallback&lt;[FileAsset](#fileasset8)&gt; | 是    | 异步返回FileAsset之后的回调 |
P
panqiangbiao 已提交
1785 1786 1787 1788

**示例**

```
P
panqiangbiao 已提交
1789
async function example() {
P
panqiangbiao 已提交
1790 1791 1792 1793
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1794 1795
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1796 1797
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1798
    fetchFileResult.getPositionObject(0, (err, value) => {
P
panqiangbiao 已提交
1799 1800 1801 1802 1803 1804 1805
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1806 1807
```

P
panqiangbiao 已提交
1808
### getPositionObject<sup>8+</sup>
P
panqiangbiao 已提交
1809

P
panqiangbiao 已提交
1810
getPositionObject(index: number): Promise&lt;FileAsset&gt;
P
panqiangbiao 已提交
1811 1812 1813

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

P
panqiangbiao 已提交
1814
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1815

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

P
panqiangbiao 已提交
1818 1819
**参数**

H
HelloCrease 已提交
1820 1821 1822
| 参数    | 类型     | 必填   | 说明             |
| ----- | ------ | ---- | -------------- |
| index | number | 是    | 要获取的文件的索引,从0开始 |
P
panqiangbiao 已提交
1823 1824 1825

**返回值**

H
HelloCrease 已提交
1826 1827
| 类型                                      | 说明            |
| --------------------------------------- | ------------- |
P
panqiangbiao 已提交
1828
| Promise&lt;[FileAsset](#fileasset8)&gt; | 返回FileAsset对象 |
P
panqiangbiao 已提交
1829 1830 1831 1832

**示例**

```
P
panqiangbiao 已提交
1833
async function example() {
P
panqiangbiao 已提交
1834 1835 1836 1837
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1838 1839
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1840 1841
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
P
panqiangbiao 已提交
1842
    fetchFileResult.getPositionObject(1, (err, value) => {
P
panqiangbiao 已提交
1843 1844 1845 1846 1847 1848 1849
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1850 1851
```

P
panqiangbiao 已提交
1852
### getAllObject<sup>8+</sup>
P
panqiangbiao 已提交
1853

P
panqiangbiao 已提交
1854
getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void
P
panqiangbiao 已提交
1855 1856 1857

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

P
panqiangbiao 已提交
1858
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
1859

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

P
panqiangbiao 已提交
1862 1863
**参数**

H
HelloCrease 已提交
1864 1865 1866
| 参数       | 类型                                       | 必填   | 说明                   |
| -------- | ---------------------------------------- | ---- | -------------------- |
| callback | AsyncCallback<Array<[FileAsset](#fileasset8)>> | 是    | 异步返回FileAsset列表之后的回调 |
P
panqiangbiao 已提交
1867 1868 1869 1870

**示例**

```
P
panqiangbiao 已提交
1871
async function example() {
P
panqiangbiao 已提交
1872 1873 1874 1875
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1876 1877
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.getAllObject((err, value) => {
       if (err) {
           console.error('Failed ');
           return;
       }
       console.log(value);
    })
}
P
panqiangbiao 已提交
1888 1889
```

P
panqiangbiao 已提交
1890
### getAllObject<sup>8+</sup>
P
panqiangbiao 已提交
1891

P
panqiangbiao 已提交
1892
getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;
P
panqiangbiao 已提交
1893 1894 1895

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

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

P
panqiangbiao 已提交
1898 1899
**返回值**

H
HelloCrease 已提交
1900 1901
| 类型                                       | 说明              |
| ---------------------------------------- | --------------- |
P
panqiangbiao 已提交
1902
| Promise<Array<[FileAsset](#fileasset8)>> | 返回FileAsset对象列表 |
P
panqiangbiao 已提交
1903 1904 1905 1906

**示例**

```
P
panqiangbiao 已提交
1907
async function example() {
P
panqiangbiao 已提交
1908 1909 1910 1911
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
      selections: fileKeyObj.MEDIA_TYPE + '= ?',
      selectionArgs: [imageType.toString()],
P
panqiangbiao 已提交
1912 1913
      order: fileKeyObj.DATE_ADDED + " DESC",
      extendArgs: "",
P
panqiangbiao 已提交
1914 1915 1916 1917
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    var data = fetchFileResult.getAllObject();
}
P
panqiangbiao 已提交
1918 1919
```

P
panqiangbiao 已提交
1920 1921 1922 1923 1924 1925
## Album<sup>8+</sup>

实体相册

### **属性**

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

H
HelloCrease 已提交
1928 1929 1930 1931 1932 1933 1934 1935 1936
| 名称           | 参数型    | 可读   | 可写   | 说明      |
| ------------ | ------ | ---- | ---- | ------- |
| albumId      | number | 是    | 否    | 相册编号    |
| albumName    | string | 是    | 是    | 相册名称    |
| albumUri     | string | 是    | 否    | 相册Uri   |
| dateModified | number | 是    | 否    | 修改日期    |
| count        | number | 是    | 否    | 相册中文件数量 |
| relativePath | string | 是    | 否    | 相对路径    |
| coverUri     | string | 是    | 否    | 封面文件Uri |
P
panqiangbiao 已提交
1937

P
panqiangbiao 已提交
1938 1939 1940
### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void
P
panqiangbiao 已提交
1941 1942 1943

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

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

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

P
panqiangbiao 已提交
1948 1949
**参数**

H
HelloCrease 已提交
1950 1951 1952
| 参数       | 类型                        | 必填   | 说明    |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | 是    | 回调返回空 |
P
panqiangbiao 已提交
1953 1954 1955 1956

**示例**

```
P
panqiangbiao 已提交
1957
async function example() {
P
panqiangbiao 已提交
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
    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 已提交
1973 1974
```

P
panqiangbiao 已提交
1975
### commitModify<sup>8+</sup>
P
panqiangbiao 已提交
1976

P
panqiangbiao 已提交
1977
commitModify(): Promise&lt;void&gt;
P
panqiangbiao 已提交
1978 1979 1980

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

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

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

P
panqiangbiao 已提交
1985 1986
**返回值**

H
HelloCrease 已提交
1987 1988
| 类型                  | 说明           |
| ------------------- | ------------ |
P
panqiangbiao 已提交
1989 1990 1991 1992 1993
| Promise&lt;void&gt; | Promise调用返回空 |

**示例**

```
P
panqiangbiao 已提交
1994
async function example() {
P
panqiangbiao 已提交
1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
    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 已提交
2008 2009
```

P
panqiangbiao 已提交
2010
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
2011

P
panqiangbiao 已提交
2012
getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void
P
panqiangbiao 已提交
2013 2014 2015

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

P
panqiangbiao 已提交
2016
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2017

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

P
panqiangbiao 已提交
2020 2021
**参数**

H
HelloCrease 已提交
2022 2023 2024 2025
| 参数       | 类型                                       | 必填   | 说明                       |
| -------- | ---------------------------------------- | ---- | ------------------------ |
| options  | [MediaFetchOptions](#mediafetchoptions8) | 是    | 媒体检索选项                   |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult8)> | 是    | 异步返回FetchFileResult之后的回调 |
P
panqiangbiao 已提交
2026 2027 2028 2029

**示例**

```
P
panqiangbiao 已提交
2030
async function example() {
P
panqiangbiao 已提交
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp, getFileAssetsCallBack);
    function getFileAssetsCallBack(err, fetchFileResult) {
        // do something
    }
P
panqiangbiao 已提交
2041 2042 2043
}
```

P
panqiangbiao 已提交
2044
### getFileAssets<sup>8+</sup>
P
panqiangbiao 已提交
2045

P
panqiangbiao 已提交
2046
 getFileAssets(options?: MediaFetchOptions): Promise&lt;FetchFileResult&gt;
P
panqiangbiao 已提交
2047 2048 2049

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

P
panqiangbiao 已提交
2050
**需要权限**:ohos.permission.READ_MEDIA
P
panqiangbiao 已提交
2051

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

**参数**

H
HelloCrease 已提交
2056 2057 2058
| 参数      | 类型                                       | 必填   | 说明     |
| ------- | ---------------------------------------- | ---- | ------ |
| options | [MediaFetchOptions](#mediafetchoptions8) | 否    | 媒体检索选项 |
P
panqiangbiao 已提交
2059 2060 2061

**返回值**

H
HelloCrease 已提交
2062 2063
| 类型                                       | 说明                  |
| ---------------------------------------- | ------------------- |
P
panqiangbiao 已提交
2064
| Promise<[FetchFileResult](#fetchfileresult8)> | 返回FetchFileResult对象 |
P
panqiangbiao 已提交
2065 2066 2067 2068

**示例**

```
P
panqiangbiao 已提交
2069
async function example() {
P
panqiangbiao 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.getFileAssets(fileNoArgsfetchOp).then(function(albumFetchFileResult){
        console.info("getFileAssets successfully:"+ JSON.stringify(albumFetchFileResult));
    }).catch(function(err){
        console.info("getFileAssets failed with error:"+ err);
    });
}
P
panqiangbiao 已提交
2082 2083
```

P
panqiangbiao 已提交
2084
## PeerInfo<sup>8+</sup>
P
panqiangbiao 已提交
2085

P
panqiangbiao 已提交
2086
注册设备的信息。
P
panqiangbiao 已提交
2087

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

H
HelloCrease 已提交
2090 2091 2092 2093 2094 2095
| 名称         | 类型         | 可读   | 可写   | 说明        |
| ---------- | ---------- | ---- | ---- | --------- |
| deviceName | string     | 是    | 否    | 注册设备的名称   |
| networkId  | string     | 是    | 否    | 注册设备的网络ID |
| deviceType | DeviceType | 是    | 否    | 设备类型      |
| isOnline   | boolean    | 是    | 否    | 是否在线      |
P
panqiangbiao 已提交
2096 2097 2098



Z
zengyawen 已提交
2099
## MediaType
P
panqiangbiao 已提交
2100 2101 2102

枚举,媒体类型。

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

H
HelloCrease 已提交
2105 2106 2107 2108 2109 2110
| 名称    | 默认值  | 描述   |
| ----- | ---- | ---- |
| FILE  | 0    | 文件   |
| IMAGE | 1    | 图片   |
| VIDEO | 2    | 视频   |
| AUDIO | 3    | 音频   |
P
panqiangbiao 已提交
2111

Z
zengyawen 已提交
2112
## FileKey
P
panqiangbiao 已提交
2113 2114 2115

枚举,文件关键信息。

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

H
HelloCrease 已提交
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
| 名称            | 默认值                 | 描述                               |
| ------------- | ------------------- | -------------------------------- |
| 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              | 图片高度(单位:像素)                      |
P
panqiangbiao 已提交
2136
| ORIENTATION   | orientation         | 图片显示方向,即顺时针旋转角度,如0,90,180。(单位:度) |
H
HelloCrease 已提交
2137 2138
| ALBUM_ID      | bucket_id           | 文件所归属的相册编号                       |
| ALBUM_NAME    | bucket_display_name | 文件所归属相册名称                        |
P
panqiangbiao 已提交
2139

Z
zengyawen 已提交
2140
## DirectoryType
P
panqiangbiao 已提交
2141 2142 2143

枚举,目录类型。

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

H
HelloCrease 已提交
2146 2147 2148 2149 2150 2151 2152 2153
| 名称            | 默认值  | 描述           |
| ------------- | ---- | ------------ |
| DIR_CAMERA    | 0    | 表示Camera文件路径 |
| DIR_VIDEO     | 1    | 表示视频路径       |
| DIR_IMAGE     | 2    | 表示图片路径       |
| DIR_AUDIO     | 3    | 表示音频路径       |
| DIR_DOCUMENTS | 4    | 表示文档路径       |
| DIR_DOWNLOAD  | 5    | 表示下载路径       |
P
panqiangbiao 已提交
2154

Z
zengyawen 已提交
2155
## DeviceType
P
panqiangbiao 已提交
2156 2157 2158

枚举,设备类型。

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

H
HelloCrease 已提交
2161 2162 2163 2164 2165 2166 2167 2168 2169
| 名称           | 默认值  | 描述    |
| ------------ | ---- | ----- |
| TYPE_UNKNOWN | 0    | 未识别设备 |
| TYPE_LAPTOP  | 1    | 笔记本电脑 |
| TYPE_PHONE   | 2    | 手机    |
| TYPE_TABLET  | 3    | 平板电脑  |
| TYPE_WATCH   | 4    | 智能手表  |
| TYPE_CAR     | 5    | 车载设备  |
| TYPE_TV      | 6    | 电视设备  |
P
panqiangbiao 已提交
2170

P
panqiangbiao 已提交
2171
## MediaFetchOptions<sup>8+</sup>
P
panqiangbiao 已提交
2172 2173 2174

检索条件。

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

H
HelloCrease 已提交
2177 2178 2179 2180 2181 2182 2183 2184
| 名称            | 类型                  | 可读   | 可写   | 必填   | 说明                                       |
| ------------- | ------------------- | ---- | ---- | ---- | ---------------------------------------- |
| selections    | string              | 是    | 是    | 是    | 检索条件,使用[FileKey](#filekey)中的枚举值作为检索条件的列名。示例:<br />selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR' +mediaLibrary.FileKey.MEDIA_TYPE + '= ?‘, |
| selectionArgs | Array&lt;string&gt; | 是    | 是    | 是    | 检索条件的值,对应selections中检索条件列的值。<br />示例:<br />selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
| order         | string              | 是    | 是    | 否    | 检索结果排序方式,使用[FileKey](#filekey)中的枚举值作为检索结果排序的列,可以用升序或降序排列。示例:<br />升序排列:order: mediaLibrary.FileKey.DATE_ADDED + " AESC"<br />降序排列:order: mediaLibrary.FileKey.DATE_ADDED + " DESC" |
| uri           | string              | 是    | 是    | 否    | 文件URI                                    |
| networkId     | string              | 是    | 是    | 否    | 注册设备网络ID                                 |
| extendArgs    | string              | 是    | 是    | 否    | 扩展的检索参数,目前没有扩展检索参数                       |
P
panqiangbiao 已提交
2185

P
panqiangbiao 已提交
2186
## Size<sup>8+</sup>
P
panqiangbiao 已提交
2187 2188 2189

图片尺寸。

H
HelloCrease 已提交
2190 2191 2192 2193
| 名称     | 类型     | 可读   | 可写   | 说明       |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | 是    | 是    | 宽(单位:像素) |
| height | number | 是    | 是    | 高(单位:像素) |
P
panqiangbiao 已提交
2194

P
panqiangbiao 已提交
2195 2196 2197 2198 2199 2200 2201 2202 2203
## MediaAssetOption

媒体资源选项。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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


H
HelloCrease 已提交
2204 2205 2206 2207 2208
| 名称           | 类型     | 必填   | 描述                                       |
| ------------ | ------ | ---- | ---------------------------------------- |
| src          | string | 是    | 媒体库数据的URI。                               |
| mimeType     | string | 是    | 媒体MIME(Multipurpose&nbsp;Internet&nbsp;Mail&nbsp;Extensions)类型。<br/>例:'image/\*'、'video/\*'等。 |
| relativePath | string | 否    | 自定义媒体资源保存位置,不填则保存到默认路径。例:imageDir/image2/(媒体资源将保存位置为:default/imageDir/image2/),default为默认保存路径。 |
P
panqiangbiao 已提交
2209 2210 2211 2212 2213 2214 2215 2216 2217

## MediaSelectOption

媒体资源类型选项。

本接口在OpenHarmony 3.1 Release版本仅为接口定义,暂不支持使用。接口将在OpenHarmony 3.1 MR版本中提供使用支持。

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

H
HelloCrease 已提交
2218 2219 2220 2221
| 名称    | 类型     | 必填   | 描述                   |
| ----- | ------ | ---- | -------------------- |
| type  | string | 是    | 媒体类型,包括:image,video。 |
| count | number | 是    | 媒体选择最大数量。            |