1# Observing Media Assets 2<!--Kit: Media Library Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @yixiaoff--> 5<!--SE: @liweilu1--> 6<!--TSE: @xchaosioda--> 7 8The photoAccessHelper module provides APIs to listen for changes of specified media assets. 9 10> **NOTE** 11> 12> Before you get started, obtain a PhotoAccessHelper instance and apply for required permissions. For details, see [Before You Start](photoAccessHelper-preparation.md). 13> 14> Unless otherwise specified, the PhotoAccessHelper instance obtained in [Before You Start](photoAccessHelper-preparation.md) is used to call photoAccessHelper APIs. If the code for obtaining the PhotoAccessHelper instance is missing, an error will be reported to indicate that photoAccessHelper is not defined. 15 16The APIs related to media asset change notifications can be called asynchronously only in callback mode. This topic covers only some of these APIs. For details about all available APIs, see [Module Description](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper.md). 17 18Unless otherwise specified, all the media assets to be obtained in this document exist in the database. If no media asset is obtained when the sample code is executed, check whether the media assets exist in the database. 19 20## Listening for a URI 21 22Use [registerChange](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#registerchange) to listen for a URI. When the observed object changes, the registered callback will be invoked to return the value. 23 24### Listening for a Media Asset 25 26Register a listener for a PhotoAsset instance. When the observed PhotoAsset changes, the registered callback will be invoked to return the change. 27 28**Prerequisites** 29 30- A PhotoAccessHelper instance is obtained. 31- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 32 33The following example describes how to register a listener for an image and then delete the image. A callback will be invoked when the image is deleted. 34 35**How to Develop** 36 371. [Obtain a media asset](photoAccessHelper-resource-guidelines.md#obtaining-media-assets). 382. Register a listener for the media asset. 393. Delete the media asset. 40 41```ts 42import { dataSharePredicates } from '@kit.ArkData'; 43import { photoAccessHelper } from '@kit.MediaLibraryKit'; 44 45async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 46 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 47 predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg'); 48 let fetchOptions: photoAccessHelper.FetchOptions = { 49 fetchColumns: [], 50 predicates: predicates 51 }; 52 try { 53 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 54 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 55 console.info('getAssets photoAsset.uri : ' + photoAsset.uri); 56 let onCallback = (changeData: photoAccessHelper.ChangeData) => { 57 console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); 58 } 59 phAccessHelper.registerChange(photoAsset.uri, false, onCallback); 60 await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]); 61 fetchResult.close(); 62 } catch (err) { 63 console.error('onCallback failed with err: ' + err); 64 } 65} 66``` 67 68### Listening for an Album 69 70Register a listener for an album. When the observed album changes, the registered callback will be invoked to return the change. 71 72**Prerequisites** 73 74- A PhotoAccessHelper instance is obtained. 75- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 76 77The following example describes how to register a listener for a user album and then rename the album. A callback will be invoked when the album is renamed. 78 79**How to Develop** 80 811. [Obtain a user album](photoAccessHelper-userAlbum-guidelines.md#obtaining-a-user-album). 822. Register a listener for the user album. 833. Rename the user album. 84 85 86```ts 87import { dataSharePredicates } from '@kit.ArkData'; 88import { photoAccessHelper } from '@kit.MediaLibraryKit'; 89 90async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 91 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 92 let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 93 predicates.equalTo(albumName, 'albumName'); 94 let fetchOptions: photoAccessHelper.FetchOptions = { 95 fetchColumns: [], 96 predicates: predicates 97 }; 98 99 try { 100 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); 101 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 102 console.info('getAlbums successfully, albumUri: ' + album.albumUri); 103 104 let onCallback = (changeData: photoAccessHelper.ChangeData) => { 105 console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); 106 } 107 phAccessHelper.registerChange(album.albumUri, false, onCallback); 108 album.albumName = 'newAlbumName' + Date.now(); 109 await album.commitModify(); 110 fetchResult.close(); 111 } catch (err) { 112 console.error('onCallback failed with err: ' + err); 113 } 114} 115``` 116 117## Fuzzy Listening 118 119You can set **forChildUris** to **true** to enable fuzzy listening. 120 121- If **uri** is an album URI, the value **true** of **forChildUris** enables listening for the changes of the files in the album, and the value **false** enables listening for only the changes of the album itself. 122- If **uri** is **photoAsset**, there is no difference whether **forChildUris** is **true** or **false**. 123- If **uri** is **DefaultChangeUri**, **forChildUris** must be **true**. If **forChildUris** is set to **false**, the URI cannot be found and no message can be received. 124 125### Listening for All PhotoAssets 126 127Register a listener for all PhotoAsset instance. When a PhotoAsset instance changes, the registered callback will be invoked. 128 129**Prerequisites** 130 131- A PhotoAccessHelper instance is obtained. 132- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 133 134The following example describes how to register a listener for all media assets and then delete a media asset. A callback will be invoked when the media asset is deleted. 135 136**How to Develop** 137 1381. Register a listener for all media assets. 1392. [Obtain a media asset](photoAccessHelper-resource-guidelines.md#obtaining-media-assets). 1403. Delete the media asset. 141 142```ts 143import { dataSharePredicates } from '@kit.ArkData'; 144import { photoAccessHelper } from '@kit.MediaLibraryKit'; 145 146async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 147 let onCallback = (changeData: photoAccessHelper.ChangeData) => { 148 console.info('onCallback successfully, changData: ' + JSON.stringify(changeData)); 149 } 150 phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback); 151 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 152 let fetchOptions: photoAccessHelper.FetchOptions = { 153 fetchColumns: [], 154 predicates: predicates 155 }; 156 try { 157 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 158 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 159 console.info('getAssets photoAsset.uri : ' + photoAsset.uri); 160 await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]); 161 fetchResult.close(); 162 } catch (err) { 163 console.error('onCallback failed with err: ' + err); 164 } 165} 166``` 167 168## Unregistering Listening for a URI 169 170Use [unRegisterChange](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#unregisterchange) to unregister the listening for the specified URI. Multiple listeners can be registered for a URI. If multiple listener callbacks exist, you can specify a callback to unregister a specific listener. If no callback is specified, all listeners of the URI will be unregistered. 171 172**Prerequisites** 173 174- A PhotoAccessHelper instance is obtained. 175- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 176 177The following example describes how to unregister the listening for an image and then delete the image. The unregistered listener callback will not be invoked when the image is deleted. 178 179**How to Develop** 180 1811. [Obtain a media asset](photoAccessHelper-resource-guidelines.md#obtaining-media-assets). 1822. Unregister listening for the URI of the media asset obtained. 1833. Delete the media asset. 184 185```ts 186import { dataSharePredicates } from '@kit.ArkData'; 187import { photoAccessHelper } from '@kit.MediaLibraryKit'; 188 189async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 190 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 191 predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg'); 192 let fetchOptions: photoAccessHelper.FetchOptions = { 193 fetchColumns: [], 194 predicates: predicates 195 }; 196 try { 197 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 198 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 199 console.info('getAssets photoAsset.uri : ' + photoAsset.uri); 200 let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { 201 console.info('onCallback1, changData: ' + JSON.stringify(changeData)); 202 } 203 let onCallback2 = (changeData: photoAccessHelper.ChangeData) => { 204 console.info('onCallback2, changData: ' + JSON.stringify(changeData)); 205 } 206 phAccessHelper.registerChange(photoAsset.uri, false, onCallback1); 207 phAccessHelper.registerChange(photoAsset.uri, false, onCallback2); 208 phAccessHelper.unRegisterChange(photoAsset.uri, onCallback1); 209 await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]); 210 fetchResult.close(); 211 } catch (err) { 212 console.error('onCallback failed with err: ' + err); 213 } 214} 215``` 216