diff --git a/zh-cn/application-dev/file-management/photoAccessHelper-notify-guidelines.md b/zh-cn/application-dev/file-management/photoAccessHelper-notify-guidelines.md index a6568e442d847946af56ba3a189f0010c391569c..0a5f460ae3da0b97024ebd4d1cfe5986912c0a4c 100644 --- a/zh-cn/application-dev/file-management/photoAccessHelper-notify-guidelines.md +++ b/zh-cn/application-dev/file-management/photoAccessHelper-notify-guidelines.md @@ -33,29 +33,27 @@ photoAccessHelper提供监听媒体资源变更的接口,供开发者对指定 ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.uri : ' + fileAsset.uri); - - let onCallback = (changeData:dataSharePredicates.ChangeData) => { - console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.uri : ' + fileAsset.uri); + let onCallback = (changeData: photoAccessHelper.ChangeData) => { + console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); + } + phAccessHelper.registerChange(fileAsset.uri, false, onCallback); + await fileAsset.setFavorite(true); + fetchResult.close(); + } catch (err) { + console.error('onCallback failed with err: ' + err); } - phAccessHelper.registerChange(fileAsset.uri, false, onCallback); - - await fileAsset.favorite(true); - fetchResult.close(); -} catch (err) { - console.error('onCallback failed with err: ' + err); } ``` @@ -78,31 +76,31 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKey.ALBUM_NAME; -predicates.equalTo(albumName, 'albumName'); -let fetchOptions: dataSharePredicates.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); - let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); - console.info('getAlbums successfullyfully, albumName: ' + album.albumUri); - - let onCallback = (changeData: photoAccessHelper.ChangeData) => { - console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; + predicates.equalTo(albumName, 'albumName'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); + let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); + console.info('getAlbums successfullyfully, albumName: ' + album.albumUri); + + let onCallback = (changeData: photoAccessHelper.ChangeData) => { + console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); + } + phAccessHelper.registerChange(album.albumUri, false, onCallback); + album.albumName = 'newAlbumName' + Date.now(); + await album.commitModify(); + fetchResult.close(); + } catch (err) { + console.error('onCallback failed with err: ' + err); } - phAccessHelper.registerChange(album.albumUri, false, onCallback); - - album.albumName = 'newAlbumName' + Date.now(); - await album.commitModify(); - fetchResult.close(); -} catch (err) { - console.error('onCallback failed with err: ' + err); } ``` @@ -129,27 +127,26 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; -let onCallback = (changeData: dataSharePredicates.ChangeData) => { - console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); -} -phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback); - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.uri : ' + fileAsset.uri); - await fileAsset.favorite(true); - fetchResult.close(); -} catch (err) { - console.error('onCallback failed with err: ' + err); +async function example() { + let onCallback = (changeData: photoAccessHelper.ChangeData) => { + console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); + } + phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback); + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.uri : ' + fileAsset.uri); + await fileAsset.setFavorite(true); + fetchResult.close(); + } catch (err) { + console.error('onCallback failed with err: ' + err); + } } ``` @@ -172,33 +169,31 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.uri : ' + fileAsset.uri); - - let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { - console.info('onCallback1, changData: ' + JSON.stringify(changeData)); - } - let onCallback2 = (changeData: photoAccessHelper.ChangeData) => { - console.info('onCallback2, changData: ' + JSON.stringify(changeData)); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.uri : ' + fileAsset.uri); + let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { + console.info('onCallback1, changData: ' + JSON.stringify(changeData)); + } + let onCallback2 = (changeData: photoAccessHelper.ChangeData) => { + console.info('onCallback2, changData: ' + JSON.stringify(changeData)); + } + phAccessHelper.registerChange(fileAsset.uri, false, onCallback1); + phAccessHelper.registerChange(fileAsset.uri, false, onCallback2); + phAccessHelper.unRegisterChange(fileAsset.uri, onCallback1); + await fileAsset.setFavorite(true); + fetchResult.close(); + } catch (err) { + console.error('onCallback failed with err: ' + err); } - phAccessHelper.registerChange(fileAsset.uri, false, onCallback1); - phAccessHelper.registerChange(fileAsset.uri, false, onCallback2); - phAccessHelper.unRegisterChange(fileAsset.uri, onCallback1); - - await fileAsset.favorite(true); - fetchResult.close(); -} catch (err) { - console.error('onCallback failed with err: ' + err); } ``` diff --git a/zh-cn/application-dev/file-management/photoAccessHelper-overview.md b/zh-cn/application-dev/file-management/photoAccessHelper-overview.md index 9ac3deb5f0c76597d9e78415669c40e7d0168bb1..6546d5c56a92465e6eca08704850ed3ea4297032 100644 --- a/zh-cn/application-dev/file-management/photoAccessHelper-overview.md +++ b/zh-cn/application-dev/file-management/photoAccessHelper-overview.md @@ -107,14 +107,12 @@ let phAccessHelper: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getP export default class EntryAbility extends UIAbility { onWindowStageCreate(windowStage) { let list : Array = ['ohos.permission.READ_IMAGEVIDEO', 'ohos.permission.WRITE_IMAGEVIDEO']; - let permissionRequestResult; let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); atManager.requestPermissionsFromUser(this.context, list, (err, result) => { if (err) { console.error('requestPermissionsFromUserError: ' + JSON.stringify(err)); } else { - permissionRequestResult = result; - console.info('permissionRequestResult: ' + JSON.stringify(permissionRequestResult)); + console.info('permissionRequestResult: ' + JSON.stringify(result)); } }); } diff --git a/zh-cn/application-dev/file-management/photoAccessHelper-resource-guidelines.md b/zh-cn/application-dev/file-management/photoAccessHelper-resource-guidelines.md index a728ea8e283eebd21343551c0030731692ec5069..68c21f740e2a7d18e48da3a56dcf66f06183a7e5 100644 --- a/zh-cn/application-dev/file-management/photoAccessHelper-resource-guidelines.md +++ b/zh-cn/application-dev/file-management/photoAccessHelper-resource-guidelines.md @@ -27,34 +27,24 @@ 下面以查询文件名为'test.jpg'的图片资源为例。 -**开发步骤:** - -创建FetchOptions对象指定检索条件为检索文件名为'test.jpg'的图片。 - ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; -``` -调用PhotoAccessHelper.getAssets接口获取图片资源。 - -```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); - fetchResult.close(); -} catch (err) { - console.error('getAssets failed with err: ' + err); +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); + fetchResult.close(); + } catch (err) { + console.error('getAssets failed with err: ' + err); + } } ``` @@ -64,65 +54,53 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -predicates.equalTo(photoAccessHelper.PhotoKeys.URI, 'file://media/Photo/1'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; -``` -调用PhotoAccessHelper.getAssets接口获取图片资源。 +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + predicates.equalTo(photoAccessHelper.PhotoKeys.URI, 'file://media/Photo/1'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; -```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.uri : ' + fileAsset.uri); - fetchResult.close(); -} catch (err) { - console.error('getAssets failed with err: ' + err); + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.uri : ' + fileAsset.uri); + fetchResult.close(); + } catch (err) { + console.error('getAssets failed with err: ' + err); + } } ``` - ### 指定文件添加的时间获取图片或视频资源 下面以查询指定添加时间为'2022-06-01'至'2023-06-01'这一年内为例。 ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let startTime: Number = Date.parse(new Date('2022-06-01').toString()) / 1000; // The value of the start time is the number of seconds elapsed since the Epoch time. -let endTime: Number = Date.parse(new Date('2023-06-01').toString()) / 1000; // The value of the end time is the number of seconds elapsed since the Epoch time. -let date_added: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.DATE_ADDED; -predicates.between(date_added, startTime, endTime); -predicates.orderByDesc(date_added); // Sort the obtained records in descending order. -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [date_added], // The date_added attribute is not a default option and needs to be added. - predicates: predicates -}; -``` - -调用PhotoAccessHelper.getAssets接口获取图片资源。 -```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - console.info('getAssets count: ' + fetchResult.getCount()); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); - fetchResult.close(); -} catch (err) { - console.error('getAssets failed with err: ' + err); +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let startTime = Date.parse(new Date('2022-06-01').toString()) / 1000; // 查询起始时间距1970年1月1日的秒数值。 + let endTime = Date.parse(new Date('2023-06-01').toString()) / 1000; // 查询结束时间距1970年1月1日的秒数值。 + let date_added: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.DATE_ADDED; + predicates.between(date_added, startTime, endTime); + predicates.orderByDesc(date_added); // 查询结果按照降序排序。 + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [date_added], // date_added属性不属于默认查询列,需要自行添加。 + predicates: predicates + }; + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + console.info('getAssets count: ' + fetchResult.getCount()); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); + fetchResult.close(); + } catch (err) { + console.error('getAssets failed with err: ' + err); + } } ``` @@ -151,26 +129,27 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; import image from '@ohos.multimedia.image'; -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); - let size: image.Size = { width: 720, height: 720 }; - let pixelMap: image.PixelMap = await fileAsset.getThumbnail(size); - let imageInfo: image.ImageInfo = await pixelMap.getImageInfo() - console.info('getThumbnail successful, pixelMap ImageInfo size: ' + JSON.stringify(imageInfo.size)); - fetchResult.close(); -} catch (err) { - console.error('getThumbnail failed with err: ' + err); +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); + let size: image.Size = { width: 720, height: 720 }; + let pixelMap: image.PixelMap = await fileAsset.getThumbnail(size); + let imageInfo: image.ImageInfo = await pixelMap.getImageInfo() + console.info('getThumbnail successful, pixelMap ImageInfo size: ' + JSON.stringify(imageInfo.size)); + fetchResult.close(); + } catch (err) { + console.error('getThumbnail failed with err: ' + err); + } } ``` @@ -193,18 +172,18 @@ try { 2. 调用createAsset接口创建图片资源。 ```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let displayName: string = 'testPhoto' + Date.now() + '.jpg'; - let createOption: photoAccessHelper.CreateOptions = { - subType: photoAccessHelper.PhotoSubtype.DEFAULT - }; - - let fileAsset: photoAccessHelper.photoAsset = await phAccessHelper.createAsset(displayName, createOption); - console.info('createAsset successfully, file displayName: ' + fileAsset.displayName); -} catch (err) { - console.error('createAsset failed, message = ', err); +async function example() { + try { + let displayName: string = 'testPhoto' + Date.now() + '.jpg'; + let createOption: photoAccessHelper.PhotoCreateOptions = { + subtype: photoAccessHelper.PhotoSubtype.DEFAULT + }; + + let fileAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(displayName, createOption); + console.info('createAsset successfully, file displayName: ' + fileAsset.displayName); + } catch (err) { + console.error('createAsset failed, message = ', err); + } } ``` @@ -233,26 +212,27 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: ['title'], - predicates: predicates -}; -let newTitle = 'newTestPhoto'; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE; - let fileAssetTitle: photoAccessHelper.MemberType = fileAsset.get(title); - console.info('getAssets fileAsset.title : ' + fileAssetTitle); - fileAsset.set(title, newTitle); - await fileAsset.commitModify(); - fetchResult.close(); -} catch (err) { - console.error('commitModify failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: ['title'], + predicates: predicates + }; + let newTitle = 'newTestPhoto'; + + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE; + let fileAssetTitle: photoAccessHelper.MemberType = fileAsset.get(title); + console.info('getAssets fileAsset.title : ' + fileAssetTitle); + fileAsset.set(title, newTitle); + await fileAsset.commitModify(); + fetchResult.close(); + } catch (err) { + console.error('commitModify failed with err: ' + err); + } } ``` @@ -278,21 +258,22 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); - console.info('getAssets fileAsset.uri : ' + fileAsset.uri); - await phAccessHelper.deleteAssets([fileAsset.uri]); - fetchResult.close(); -} catch (err) { - console.error('deleteAssets failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); + console.info('getAssets fileAsset.uri : ' + fileAsset.uri); + await phAccessHelper.deleteAssets([fileAsset.uri]); + fetchResult.close(); + } catch (err) { + console.error('deleteAssets failed with err: ' + err); + } } ``` diff --git a/zh-cn/application-dev/file-management/photoAccessHelper-systemAlbum-guidelines.md b/zh-cn/application-dev/file-management/photoAccessHelper-systemAlbum-guidelines.md index 11fdee073158b8bddb39d5c8736ef63cad4e37a1..18c3c2b7352d1a48066316be58f7244955a68acb 100644 --- a/zh-cn/application-dev/file-management/photoAccessHelper-systemAlbum-guidelines.md +++ b/zh-cn/application-dev/file-management/photoAccessHelper-systemAlbum-guidelines.md @@ -29,15 +29,15 @@ photoAccessHelper仅提供开发者对收藏夹、视频相册、截屏和录屏 2. 调用getAlbums接口获取收藏夹对象。 ```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); - let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); - console.info('get favorite Album successfully, albumUri: ' + album.albumUri); - fetchResult.close(); -} catch (err) { - console.error('get favorite Album failed with err: ' + err); +async function example() { + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); + let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); + console.info('get favorite Album successfully, albumUri: ' + album.albumUri); + fetchResult.close(); + } catch (err) { + console.error('get favorite Album failed with err: ' + err); + } } ``` @@ -60,23 +60,24 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let photoFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); - console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); - let favoriteState = true; - await fileAsset.setFavorite(favoriteState); -} catch (err) { - console.error('setFavorite failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let photoFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); + console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName); + let favoriteState = true; + await fileAsset.setFavorite(favoriteState); + } catch (err) { + console.error('setFavorite failed with err: ' + err); + } } ``` @@ -100,26 +101,27 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); - let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); - console.info('get favorite Album successfully, albumUri: ' + album.albumUri); - - let photoFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); - console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName); - photoFetchResult.close(); - albumFetchResult.close(); -} catch (err) { - console.error('favorite failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); + let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); + console.info('get favorite Album successfully, albumUri: ' + album.albumUri); + + let photoFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); + console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName); + photoFetchResult.close(); + albumFetchResult.close(); + } catch (err) { + console.error('favorite failed with err: ' + err); + } } ``` @@ -143,28 +145,29 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); - let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); - console.info('get favorite Album successfully, albumUri: ' + album.albumUri); - - let photoFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); - console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName); - let favoriteState = false; - await fileAsset.setFavorite(favoriteState); - photoFetchResult.close(); - albumFetchResult.close(); -} catch (err) { - console.error('setFavorite failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE); + let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); + console.info('get favorite Album successfully, albumUri: ' + album.albumUri); + + let photoFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); + console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName); + let favoriteState = false; + await fileAsset.setFavorite(favoriteState); + photoFetchResult.close(); + albumFetchResult.close(); + } catch (err) { + console.error('setFavorite failed with err: ' + err); + } } ``` @@ -187,15 +190,15 @@ try { 2. 调用getAlbums接口获取视频相册。 ```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO); - let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); - console.info('get video Album successfully, albumUri: ' + album.albumUri); - fetchResult.close(); -} catch (err) { - console.error('get video Album failed with err: ' + err); +async function example() { + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO); + let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); + console.info('get video Album successfully, albumUri: ' + album.albumUri); + fetchResult.close(); + } catch (err) { + console.error('get video Album failed with err: ' + err); + } } ``` @@ -219,26 +222,27 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO); - let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); - console.info('get video Album successfully, albumUri: ' + album.albumUri); - - let videoFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await videoFetchResult.getFirstObject(); - console.info('video album getAssets successfully, albumName: ' + fileAsset.displayName); - videoFetchResult.close(); - albumFetchResult.close(); -} catch (err) { - console.error('video failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO); + let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); + console.info('get video Album successfully, albumUri: ' + album.albumUri); + + let videoFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await videoFetchResult.getFirstObject(); + console.info('video album getAssets successfully, albumName: ' + fileAsset.displayName); + videoFetchResult.close(); + albumFetchResult.close(); + } catch (err) { + console.error('video failed with err: ' + err); + } } ``` @@ -261,15 +265,15 @@ try { 2. 调用getAlbums接口获取截屏和录屏相册。 ```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT); - let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); - console.info('get screenshot Album successfully, albumUri: ' + album.albumUri); - fetchResult.close(); -} catch (err) { - console.error('get screenshot Album failed with err: ' + err); +async function example() { + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT); + let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); + console.info('get screenshot Album successfully, albumUri: ' + album.albumUri); + fetchResult.close(); + } catch (err) { + console.error('get screenshot Album failed with err: ' + err); + } } ``` @@ -293,25 +297,26 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT); - let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); - console.info('get screenshot album successfully, albumUri: ' + album.albumUri); - - let screenshotFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await screenshotFetchResult.getFirstObject(); - console.info('screenshot album getAssets successfully, albumName: ' + fileAsset.displayName); - screenshotFetchResult.close(); - albumFetchResult.close(); -} catch (err) { - console.error('screenshot album failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT); + let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); + console.info('get screenshot album successfully, albumUri: ' + album.albumUri); + + let screenshotFetchResult: photoAccessHelper.FetchResult = await album.getAssets(fetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await screenshotFetchResult.getFirstObject(); + console.info('screenshot album getAssets successfully, albumName: ' + fileAsset.displayName); + screenshotFetchResult.close(); + albumFetchResult.close(); + } catch (err) { + console.error('screenshot album failed with err: ' + err); + } } ``` diff --git a/zh-cn/application-dev/file-management/photoAccessHelper-userAlbum-guidelines.md b/zh-cn/application-dev/file-management/photoAccessHelper-userAlbum-guidelines.md index 4ab3997cb988e3e05895c8b4c79595e307d940b4..5206f5cde46589440d7577c93439b30b9c583b5d 100644 --- a/zh-cn/application-dev/file-management/photoAccessHelper-userAlbum-guidelines.md +++ b/zh-cn/application-dev/file-management/photoAccessHelper-userAlbum-guidelines.md @@ -34,14 +34,14 @@ photoAccessHelper提供用户相册相关的接口,供开发者创建、删除 2. 调用createAlbum接口创建相册。 ```ts -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -try { - let albumName = 'albumName'; - let album: photoAccessHelper.Album = await phAccessHelper.createAlbum(albumName); - console.info('createAlbum successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri); -} catch (err) { - console.error('createAlbum failed with err: ' + err); +async function example() { + try { + let albumName = 'albumName'; + let album: photoAccessHelper.Album = await phAccessHelper.createAlbum(albumName); + console.info('createAlbum successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri); + } catch (err) { + console.error('createAlbum failed with err: ' + err); + } } ``` @@ -64,23 +64,24 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let albumName: photoAccessHelper.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; -predicates.equalTo(albumName, 'albumName'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); - let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); - console.info('getAlbums successfully, albumName: ' + album.albumName); - fetchResult.close(); -} catch (err) { - console.error('getAlbums failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; + predicates.equalTo(albumName, 'albumName'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); + let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); + console.info('getAlbums successfully, albumName: ' + album.albumName); + fetchResult.close(); + } catch (err) { + console.error('getAlbums failed with err: ' + err); + } } ``` @@ -111,25 +112,26 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; -predicates.equalTo(albumName, 'albumName'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); - let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); - console.info('getAlbums successfully, albumName: ' + album.albumName); - album.albumName = 'newAlbumName'; - await album.commitModify(); - fetchResult.close(); -} catch (err) { - console.error('commitModify failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; + predicates.equalTo(albumName, 'albumName'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); + let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); + console.info('getAlbums successfully, albumName: ' + album.albumName); + album.albumName = 'newAlbumName'; + await album.commitModify(); + fetchResult.close(); + } catch (err) { + console.error('commitModify failed with err: ' + err); + } } ``` @@ -156,34 +158,35 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; -albumPredicates.equalTo(albumName, 'albumName'); -let albumFetchOptions: dataSharePredicates.FetchOptions = { - fetchColumns: [], - predicates: albumPredicates -}; - -let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let photoFetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: photoPredicates -}; - -try { - let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); - let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); - console.info('getAlbums successfully, albumName: ' + album.albumName); - let photoFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(photoFetchOptions); - let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); - console.info('getAssets successfully, albumName: ' + fileAsset.displayName); - await album.addAssets([fileAsset]); - albumFetchResult.close(); - photoFetchResult.close(); -} catch (err) { - console.error('addAssets failed with err: ' + err); + +async function example() { + let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; + albumPredicates.equalTo(albumName, 'albumName'); + let albumFetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: albumPredicates + }; + + let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let photoFetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: photoPredicates + }; + + try { + let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); + let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); + console.info('getAlbums successfully, albumName: ' + album.albumName); + let photoFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAssets(photoFetchOptions); + let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); + console.info('getAssets successfully, albumName: ' + fileAsset.displayName); + await album.addAssets([fileAsset]); + albumFetchResult.close(); + photoFetchResult.close(); + } catch (err) { + console.error('addAssets failed with err: ' + err); + } } ``` @@ -209,33 +212,34 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let albumName = photoAccessHelper.AlbumKey.ALBUM_NAME; -albumPredicates.equalTo(albumName, 'albumName'); -let albumFetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: albumPredicates -}; - -let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let photoFetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: photoPredicates -}; - -try { - let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); - let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); - console.info('getAlbums successfully, albumName: ' + album.albumName); - let photoFetchResult = await album.getAssets(photoFetchOptions); - let fileAsset = await photoFetchResult.getFirstObject(); - console.info('album getAssets successfully, albumName: ' + fileAsset.displayName); - albumFetchResult.close(); - photoFetchResult.close(); -} catch (err) { - console.error('album getAssets failed with err: ' + err); + +async function example() { + let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; + albumPredicates.equalTo(albumName, 'albumName'); + let albumFetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: albumPredicates + }; + + let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let photoFetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: photoPredicates + }; + + try { + let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); + let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); + console.info('getAlbums successfully, albumName: ' + album.albumName); + let photoFetchResult = await album.getAssets(photoFetchOptions); + let fileAsset = await photoFetchResult.getFirstObject(); + console.info('album getAssets successfully, albumName: ' + fileAsset.displayName); + albumFetchResult.close(); + photoFetchResult.close(); + } catch (err) { + console.error('album getAssets failed with err: ' + err); + } } ``` @@ -264,34 +268,35 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; -albumPredicates.equalTo(albumName, 'albumName'); -let albumFetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: albumPredicates -}; - -let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let photoFetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: photoPredicates -}; - -try { - let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); - let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); - console.info('getAlbums successfully, albumName: ' + album.albumName); - let photoFetchResult = await album.getAssets(photoFetchOptions); - let fileAsset = await photoFetchResult.getFirstObject(); - console.info('album getAssets successfully, albumName: ' + fileAsset.displayName); - await album.removeAssets([fileAsset]); - albumFetchResult.close(); - photoFetchResult.close(); -} catch (err) { - console.error('removeAssets failed with err: ' + err); + +async function example() { + let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; + albumPredicates.equalTo(albumName, 'albumName'); + let albumFetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: albumPredicates + }; + + let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let photoFetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: photoPredicates + }; + + try { + let albumFetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); + let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); + console.info('getAlbums successfully, albumName: ' + album.albumName); + let photoFetchResult = await album.getAssets(photoFetchOptions); + let fileAsset = await photoFetchResult.getFirstObject(); + console.info('album getAssets successfully, albumName: ' + fileAsset.displayName); + await album.removeAssets([fileAsset]); + albumFetchResult.close(); + photoFetchResult.close(); + } catch (err) { + console.error('removeAssets failed with err: ' + err); + } } ``` @@ -315,23 +320,24 @@ try { ```ts import dataSharePredicates from '@ohos.data.dataSharePredicates'; -import photoAccessHelper from '@ohos.file.photoAccessHelper'; - -let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); -let albumName: photoAccessHelper.AlbumKey.ALBUM_NAME = photoAccessHelper.AlbumKey.ALBUM_NAME; -predicates.equalTo(albumName, '%albumName%'); -let fetchOptions: photoAccessHelper.FetchOptions = { - fetchColumns: [], - predicates: predicates -}; - -try { - let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); - let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); - console.info('getAlbums successfully, albumName: ' + album.albumName); - phAccessHelper.deleteAlbums([album]); - fetchResult.close(); -} catch (err) { - console.error('deleteAlbums failed with err: ' + err); + +async function example() { + let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); + let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; + predicates.equalTo(albumName, '%albumName%'); + let fetchOptions: photoAccessHelper.FetchOptions = { + fetchColumns: [], + predicates: predicates + }; + + try { + let fetchResult: photoAccessHelper.FetchResult = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); + let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); + console.info('getAlbums successfully, albumName: ' + album.albumName); + phAccessHelper.deleteAlbums([album]); + fetchResult.close(); + } catch (err) { + console.error('deleteAlbums failed with err: ' + err); + } } ```