diff --git a/zh-cn/application-dev/reference/apis/js-apis-medialibrary.md b/zh-cn/application-dev/reference/apis/js-apis-medialibrary.md index 7f5e6e26aba25b92847ec1509f5096573ebf5919..f558c70d9c5c0b2047328fa297927df4314f2d22 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-medialibrary.md +++ b/zh-cn/application-dev/reference/apis/js-apis-medialibrary.md @@ -4,7 +4,7 @@ > 该组件从API Version 6开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 ## 导入模块 -``` +```js import mediaLibrary from '@ohos.multimedia.mediaLibrary'; ``` @@ -32,13 +32,13 @@ getMediaLibrary(context: Context): MediaLibrary **示例:(从API Version 9开始)** -``` +```ts var media = mediaLibrary.getMediaLibrary(this.context); ``` **示例:(API Version 8)** -``` +```js import featureAbility from '@ohos.ability.featureAbility'; var context = featureAbility.getContext() @@ -90,7 +90,7 @@ getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileRe **示例:** -``` +```js let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE let imagesfetchOp = { @@ -134,7 +134,7 @@ getFileAssets(options: MediaFetchOptions): Promise<FetchFileResult> **示例:** -``` +```js let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE let imagesfetchOp = { @@ -165,7 +165,7 @@ on(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'| **示例:** -``` +```js media.on('imageChange', () => { // image file had changed, do something }) @@ -187,7 +187,7 @@ off(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange' **示例:** -``` +```js media.off('imageChange', () => { // stop listening success }) @@ -214,7 +214,7 @@ createAsset(mediaType: MediaType, displayName: string, relativePath: string, cal **示例:** -``` +```js async function example() { // 使用Callback方式创建Image类型文件 let mediaType = mediaLibrary.MediaType.IMAGE; @@ -256,7 +256,7 @@ createAsset(mediaType: MediaType, displayName: string, relativePath: string): Pr **示例:** -``` +```js let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; media.getPublicDirectory(DIR_CAMERA).then(function(dicResult){ console.info("getPublicDirectory successfully:"+ JSON.stringify(dicResult)); @@ -282,7 +282,7 @@ getPublicDirectory(type: DirectoryType, callback: AsyncCallback<string>): **示例:** -``` +```js let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; media.getPublicDirectory(DIR_CAMERA, (err, dicResult) => { if (dicResult == 'Camera/') { @@ -315,7 +315,7 @@ getPublicDirectory(type: DirectoryType): Promise<string> **示例:** -``` +```js async function example() { let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; const dicResult = await media.getPublicDirectory(DIR_CAMERA); @@ -346,7 +346,7 @@ getAlbums(options: MediaFetchOptions, callback: AsyncCallback **示例:** -``` +```js let AlbumNoArgsfetchOp = { selections: '', selectionArgs: [], @@ -415,7 +415,7 @@ release(callback: AsyncCallback<void>): void **示例:** -``` +```js var media = mediaLibrary.getMediaLibrary(context); media.release((err) => { // do something @@ -439,7 +439,7 @@ release(): Promise<void> **示例:** -``` +```js media.release() ``` @@ -462,7 +462,7 @@ storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback<string>) **示例:** - ``` +```js let option = { src : "/data/storage/el2/base/haps/entry/image.png", mimeType : "image/*", @@ -476,7 +476,7 @@ mediaLibrary.getMediaLibrary().storeMediaAsset(option, (err, value) => { console.log("Media resources stored. "); // Obtain the URI that stores media resources. }); - ``` +``` ### storeMediaAsset(deprecated) @@ -503,7 +503,7 @@ storeMediaAsset(option: MediaAssetOption): Promise<string> **示例:** - ``` +```js let option = { src : "/data/storage/el2/base/haps/entry/image.png", mimeType : "image/*", @@ -515,7 +515,7 @@ mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => { }).catch((err) => { console.log("An error occurred when storing media resources."); }); - ``` +``` ### startImagePreview(deprecated) @@ -538,7 +538,7 @@ startImagePreview(images: Array<string>, index: number, callback: AsyncCal **示例:** - ``` +```js let images = [ "dataability:///media/xxxx/2", "dataability:///media/xxxx/3" @@ -557,7 +557,7 @@ mediaLibrary.getMediaLibrary().startImagePreview(images, index, (err) => { } console.log("Succeeded in previewing the images."); }); - ``` +``` ### startImagePreview(deprecated) @@ -579,7 +579,7 @@ startImagePreview(images: Array<string>, callback: AsyncCallback<void&g **示例:** - ``` +```js let images = [ "dataability:///media/xxxx/2", "dataability:///media/xxxx/3" @@ -597,7 +597,7 @@ mediaLibrary.getMediaLibrary().startImagePreview(images, (err) => { } console.log("Succeeded in previewing the images."); }); - ``` +``` ### startImagePreview(deprecated) @@ -625,7 +625,7 @@ startImagePreview(images: Array<string>, index?: number): Promise<void& **示例:** - ``` +```js let images = [ "dataability:///media/xxxx/2", "dataability:///media/xxxx/3" @@ -642,7 +642,7 @@ mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => { }).catch((err) => { console.log("An error occurred when previewing the images."); }); - ``` +``` ### startMediaSelect(deprecated) @@ -664,7 +664,7 @@ startMediaSelect(option: MediaSelectOption, callback: AsyncCallback<Array< **示例:** - ``` +```js let option = { type : "media", count : 2 @@ -677,7 +677,7 @@ mediaLibrary.getMediaLibrary().startMediaSelect(option, (err, value) => { console.log("Media resources selected."); // Obtain the media selection value. }); - ``` +``` ### startMediaSelect(deprecated) @@ -704,7 +704,7 @@ startMediaSelect(option: MediaSelectOption): Promise<Array<string>> **示例:** - ``` +```js let option = { type : "media", count : 2 @@ -716,7 +716,7 @@ mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => { console.log("An error occurred when selecting media resources."); }); - ``` +``` ## FileAsset7+ @@ -769,7 +769,7 @@ isDirectory(callback: AsyncCallback<boolean>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -805,7 +805,7 @@ isDirectory():Promise<boolean> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -843,7 +843,7 @@ commitModify(callback: AsyncCallback<void>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -880,7 +880,7 @@ commitModify(): Promise<void> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -918,7 +918,7 @@ open(mode: string, callback: AsyncCallback<number>): void **示例:** -``` +```js async function example() { let mediaType = mediaLibrary.MediaType.IMAGE; let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; @@ -960,7 +960,7 @@ open(mode: string): Promise<number> **示例:** -``` +```js async function example() { let mediaType = mediaLibrary.MediaType.IMAGE; let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; @@ -995,7 +995,7 @@ close(fd: number, callback: AsyncCallback<void>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1048,7 +1048,7 @@ close(fd: number): Promise<void> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1096,7 +1096,7 @@ getThumbnail(callback: AsyncCallback<image.PixelMap>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1133,7 +1133,7 @@ getThumbnail(size: Size, callback: AsyncCallback<image.PixelMap>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1176,7 +1176,7 @@ getThumbnail(size?: Size): Promise<image.PixelMap> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1218,7 +1218,7 @@ favorite(isFavorite: boolean, callback: AsyncCallback<void>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1260,7 +1260,7 @@ favorite(isFavorite: boolean): Promise<void> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1298,7 +1298,7 @@ isFavorite(callback: AsyncCallback<boolean>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1338,7 +1338,7 @@ isFavorite():Promise<boolean> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1379,7 +1379,7 @@ trash(isTrash: boolean, callback: AsyncCallback<void>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1424,7 +1424,7 @@ trash(isTrash: boolean): Promise<void> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1462,7 +1462,7 @@ isTrash(callback: AsyncCallback<boolean>): void **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1507,7 +1507,7 @@ isTrash():Promise<boolean> **示例:** -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1527,6 +1527,239 @@ async function example() { } ``` +### deleteAsset8+ + +deleteAsset(uri: string): Promise; + +删除媒体文件资源 + +**系统接口**:此接口为系统接口。 + +**需要权限**:ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA + +**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------- | ---- | --------------- | +| uri | string | 是 | 需要删除的媒体文件资源的uri | + +**示例:** + +```js +async function example() { + let fileKeyObj = mediaLibrary.FileKey + let fileType = mediaLibrary.MediaType.FILE + let option = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [fileType.toString()], + }; + const context = getContext(this); + var media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + let asset = await fetchFileResult.getFirstObject(); + if (asset == undefined) { + console.error('asset not exist') + return + } + media.deleteAsset(asset.uri).then(() => { + console.info("deleteAsset successfully"); + }).catch((err) => { + console.info("deleteAsset failed with error:"+ err); + }); +} +``` + +deleteAsset(uri: string, callback: AsyncCallback): void; + +删除媒体文件资源 + +**系统接口**:此接口为系统接口。 + +**需要权限**:ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA + +**系统能力**:SystemCapability.Multimedia.MediaLibrary.Core + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ---------------------------- | ---- | --------------- | +| uri | string | 是 | 需要删除的媒体文件资源的uri | + +**示例:** + +```js +async function example() { + let fileKeyObj = mediaLibrary.FileKey + let fileType = mediaLibrary.MediaType.FILE + let option = { + selections: fileKeyObj.MEDIA_TYPE + '= ?', + selectionArgs: [fileType.toString()], + }; + const context = getContext(this); + var media = mediaLibrary.getMediaLibrary(context); + const fetchFileResult = await media.getFileAssets(option); + let asset = await fetchFileResult.getFirstObject(); + if (asset == undefined) { + console.error('asset not exist') + return + } + media.deleteAsset(asset.uri, (err) => { + if (err != undefined) { + console.info("deleteAsset successfully"); + } else { + console.info("deleteAsset failed with error:"+ err); + } + }); +} +``` + +### getActivePeers8+ + +getActivePeers(): Promise>; + +获取在线对端设备的信息,使用Promise方式返回异步结果 + +**系统接口**:此接口为系统接口。 + +**需要权限**:ohos.permission.READ_MEDIA + +**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore + +**返回值:** + +| 类型 | 说明 | +| ------------------- | -------------------- | +| Promise> | 返回获取的所有在线对端设备的PeerInfo | + +**示例:** + +```js +async function example() { + const context = getContext(this); + var media = mediaLibrary.getMediaLibrary(context); + media.getActivePeers().then((devicesInfo) => { + if (devicesInfo != undefined) { + for (let i = 0; i < devicesInfo.length; i++) { + console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId); + } + } else { + console.info('get distributed info is undefined!') + } + }).catch((err) => { + console.info("get distributed info failed with error:" + err); + }); +} +``` + +getActivePeers(callback: AsyncCallback>): void; + +获取在线对端设备的信息,使用callback方式返回异步结果。 + +**系统接口**:此接口为系统接口。 + +**需要权限**:ohos.permission.READ_MEDIA + +**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore + +**返回值:** + +| 类型 | 说明 | +| ------------------- | -------------------- | +| callback: AsyncCallback> | 返回获取的所有在线对端设备的PeerInfo | + +**示例:** + +```js +async function example() { + const context = getContext(this); + var media = mediaLibrary.getMediaLibrary(context); + media.getActivePeers((err, devicesInfo) => { + if (devicesInfo != undefined) { + for (let i = 0; i < devicesInfo.length; i++) { + console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId); + } + } else { + console.info('get distributed fail, message = ' + err) + } + }) +} +``` + + +### getAllPeers8+ + +getAllPeers(): Promise>; + +获取所有对端设备的信息,使用Promise方式返回异步结果 + +**系统接口**:此接口为系统接口。 + +**需要权限**:ohos.permission.READ_MEDIA + +**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore + +**返回值:** + +| 类型 | 说明 | +| ------------------- | -------------------- | +| Promise> | 返回获取的所有对端设备的PeerInfo | + +**示例:** + +```js +async function example() { + const context = getContext(this); + var media = mediaLibrary.getMediaLibrary(context); + media.getAllPeers().then((devicesInfo) => { + if (devicesInfo != undefined) { + for (let i = 0; i < devicesInfo.length; i++) { + console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId); + } + } else { + console.info('get distributed info is undefined!') + } + }).catch((err) => { + console.info("get distributed info failed with error:" + err); + }); +} +``` + +getAllPeers(callback: AsyncCallback>): void; + +获取所有对端设备的信息,使用callback方式返回异步结果。 + +**系统接口**:此接口为系统接口。 + +**需要权限**:ohos.permission.READ_MEDIA + +**系统能力**:SystemCapability.Multimedia.MediaLibrary.DistributedCore + +**返回值:** + +| 类型 | 说明 | +| ------------------- | -------------------- | +| callback: AsyncCallback> | 返回获取的所有对端设备的PeerInfo | + +**示例:** + +```js +async function example() { + const context = getContext(this); + var media = mediaLibrary.getMediaLibrary(context); + media.getAllPeers((err, devicesInfo) => { + if (devicesInfo != undefined) { + for (let i = 0; i < devicesInfo.length; i++) { + console.info('get distributed info ' + devicesInfo[i].deviceName + devicesInfo[i].networkId); + } + } else { + console.info('get distributed fail, message = ' + err) + } + }) +} +``` + ## FetchFileResult7+ 文件检索结果集。 @@ -1547,7 +1780,7 @@ getCount(): number **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let fileType = mediaLibrary.MediaType.FILE; @@ -1578,7 +1811,7 @@ isAfterLast(): boolean **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1616,7 +1849,7 @@ close(): void **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1647,7 +1880,7 @@ getFirstObject(callback: AsyncCallback<FileAsset>): void **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1684,7 +1917,7 @@ getFirstObject(): Promise<FileAsset> **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1719,7 +1952,7 @@ async function example() { **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1756,7 +1989,7 @@ async function example() { **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1789,7 +2022,7 @@ getLastObject(callback: AsyncCallback<FileAsset>): void **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1826,7 +2059,7 @@ getLastObject(): Promise<FileAsset> **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1858,7 +2091,7 @@ getPositionObject(index: number, callback: AsyncCallback<FileAsset>): void **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1901,7 +2134,7 @@ getPositionObject(index: number): Promise<FileAsset> **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1913,9 +2146,9 @@ async function example() { }; let fetchFileResult = await media.getFileAssets(getImageOp); fetchFileResult.getPositionObject(1) .then(function (fileAsset){ - console.log('[Demo] fileAsset.displayName : ' + fileAsset.displayName); + console.log('fileAsset.displayName : ' + fileAsset.displayName); }).catch(function (err) { - console.info("[Demo] getFileAssets failed with error:" + err); + console.info("getFileAssets failed with error:" + err); }); } ``` @@ -1936,7 +2169,7 @@ getAllObject(callback: AsyncCallback<Array<FileAsset>>): void **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -1973,7 +2206,7 @@ getAllObject(): Promise<Array<FileAsset>> **示例**: -``` +```js async function example() { let fileKeyObj = mediaLibrary.FileKey let imageType = mediaLibrary.MediaType.IMAGE; @@ -2024,7 +2257,7 @@ commitModify(callback: AsyncCallback<void>): void **示例**: -``` +```js async function example() { let AlbumNoArgsfetchOp = { selections: '', @@ -2061,7 +2294,7 @@ commitModify(): Promise<void> **示例**: -``` +```js async function example() { let AlbumNoArgsfetchOp = { selections: '', @@ -2097,7 +2330,7 @@ getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileRe **示例**: -``` +```js async function example() { let AlbumNoArgsfetchOp = { selections: '', @@ -2140,7 +2373,7 @@ async function example() { **示例**: -``` +```js async function example() { let AlbumNoArgsfetchOp = { selections: '', @@ -2163,7 +2396,8 @@ async function example() { ## PeerInfo8+ 注册设备的信息。 -此接口为系统接口。 + +**系统接口**:此接口为系统接口。 **系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.DistributedCore @@ -2235,7 +2469,8 @@ async function example() { ## DeviceType8+ 枚举,设备类型。 -此接口为系统接口。 + +**系统接口**:此接口为系统接口。 **系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.DistributedCore @@ -2267,7 +2502,8 @@ async function example() { ## Size8+ 图片尺寸。 -系统能力: 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core + +**系统能力:** 以下各项对应的系统能力均为SystemCapability.Multimedia.MediaLibrary.Core | 名称 | 类型 | 可读 | 可写 | 说明 | | ------ | ------ | ---- | ---- | -------- | @@ -2302,3 +2538,4 @@ async function example() { | type | string | 是 | 媒体类型,包括:image, video, media,当前仅支持media类型 | | count | number | 是 | 媒体选择,count = 1表示单选,count大于1表示多选。 | +