1e41f4b71Sopenharmony_ci# Observing Media Assets
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe **photoAccessHelper** module provides APIs for listening for media asset (image, video, and album) changes.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> - Before you get started, obtain a **PhotoAccessHelper** instance and apply for required permissions. For details, see [Before You Start](photoAccessHelper-preparation.md).
8e41f4b71Sopenharmony_ci> - Unless otherwise specified, the **PhotoAccessHelper** instance obtained in the **Before You Start** section 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.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ciThe 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 [Album Management](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md).
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciUnless 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.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## Listening for a URI
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciUse [registerChange](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#registerchange) to listen for a URI. When the observed object changes, the registered callback will be invoked to return the value.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci### Listening for a Media Asset
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciRegister a listener for a **PhotoAsset** instance. When the observed **PhotoAsset** changes, the registered callback will be invoked to return the change.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**Prerequisites**
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
25e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ciThe 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.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**How to Develop**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci1. [Obtain a media asset](photoAccessHelper-resource-guidelines.md#obtaining-media-assets).
32e41f4b71Sopenharmony_ci2. Register a listener for the media asset.
33e41f4b71Sopenharmony_ci3. Delete the media asset.
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci```ts
36e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
37e41f4b71Sopenharmony_ciimport { photoAccessHelper } from '@kit.MediaLibraryKit';
38e41f4b71Sopenharmony_ciconst context = getContext(this);
39e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciasync function example() {
42e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
43e41f4b71Sopenharmony_ci  predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
44e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
45e41f4b71Sopenharmony_ci    fetchColumns: [],
46e41f4b71Sopenharmony_ci    predicates: predicates
47e41f4b71Sopenharmony_ci  };
48e41f4b71Sopenharmony_ci  try {
49e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
50e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
51e41f4b71Sopenharmony_ci    console.info('getAssets photoAsset.uri : ' + photoAsset.uri);
52e41f4b71Sopenharmony_ci    let onCallback = (changeData: photoAccessHelper.ChangeData) => {
53e41f4b71Sopenharmony_ci      console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
54e41f4b71Sopenharmony_ci    }
55e41f4b71Sopenharmony_ci    phAccessHelper.registerChange(photoAsset.uri, false, onCallback);
56e41f4b71Sopenharmony_ci    await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]);
57e41f4b71Sopenharmony_ci    fetchResult.close();
58e41f4b71Sopenharmony_ci  } catch (err) {
59e41f4b71Sopenharmony_ci    console.error('onCallback failed with err: ' + err);
60e41f4b71Sopenharmony_ci  }
61e41f4b71Sopenharmony_ci}
62e41f4b71Sopenharmony_ci```
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci### Listening for an Album
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciRegister a listener for an album. When the observed album changes, the registered callback will be invoked to return the change.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci**Prerequisites**
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
71e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ciThe 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.
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci**How to Develop**
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci1. [Obtain a user album](photoAccessHelper-userAlbum-guidelines.md#obtaining-a-user-album).
78e41f4b71Sopenharmony_ci2. Register a listener for the user album.
79e41f4b71Sopenharmony_ci3. Rename the user album.
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci```ts
83e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
84e41f4b71Sopenharmony_ciimport { photoAccessHelper } from '@kit.MediaLibraryKit';
85e41f4b71Sopenharmony_ciconst context = getContext(this);
86e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ciasync function example() {
89e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
90e41f4b71Sopenharmony_ci  let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME;
91e41f4b71Sopenharmony_ci  predicates.equalTo(albumName, 'albumName');
92e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
93e41f4b71Sopenharmony_ci    fetchColumns: [],
94e41f4b71Sopenharmony_ci    predicates: predicates
95e41f4b71Sopenharmony_ci  };
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci  try {
98e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
99e41f4b71Sopenharmony_ci    let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
100e41f4b71Sopenharmony_ci    console.info('getAlbums successfully, albumUri: ' + album.albumUri);
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci    let onCallback = (changeData: photoAccessHelper.ChangeData) => {
103e41f4b71Sopenharmony_ci      console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
104e41f4b71Sopenharmony_ci    }
105e41f4b71Sopenharmony_ci    phAccessHelper.registerChange(album.albumUri, false, onCallback);
106e41f4b71Sopenharmony_ci    album.albumName = 'newAlbumName' + Date.now();
107e41f4b71Sopenharmony_ci    await album.commitModify();
108e41f4b71Sopenharmony_ci    fetchResult.close();
109e41f4b71Sopenharmony_ci  } catch (err) {
110e41f4b71Sopenharmony_ci    console.error('onCallback failed with err: ' + err);
111e41f4b71Sopenharmony_ci  }
112e41f4b71Sopenharmony_ci}
113e41f4b71Sopenharmony_ci```
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci## Fuzzy Listening
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ciYou can set **forChildUris** to **true** to enable fuzzy listening.<br>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. <br>If **uri** is the URI of a **photoAsset**, there is no difference between **true** and **false** for **forChildUris**.<br>If **uri** is **DefaultChangeUri**, **forChildUris** must be set to **true**. If **forChildUris** is **false**, the URI cannot be found and no notification can be received.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci### Listening for All Media Assets
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ciRegister a listener for all media assets. When a media asset changes, the registered callback will be invoked.
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci**Prerequisites**
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
126e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ciThe 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.
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**How to Develop**
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci1. Register a listener for all media assets.
133e41f4b71Sopenharmony_ci2. [Obtain a media asset](photoAccessHelper-resource-guidelines.md#obtaining-media-assets).
134e41f4b71Sopenharmony_ci3. Delete the media asset.
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci```ts
137e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
138e41f4b71Sopenharmony_ciimport { photoAccessHelper } from '@kit.MediaLibraryKit';
139e41f4b71Sopenharmony_ciconst context = getContext(this);
140e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ciasync function example() {
143e41f4b71Sopenharmony_ci  let onCallback = (changeData: photoAccessHelper.ChangeData) => {
144e41f4b71Sopenharmony_ci    console.info('onCallback successfully, changData: ' + JSON.stringify(changeData));
145e41f4b71Sopenharmony_ci  }
146e41f4b71Sopenharmony_ci  phAccessHelper.registerChange(photoAccessHelper.DefaultChangeUri.DEFAULT_PHOTO_URI, true, onCallback);
147e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
148e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
149e41f4b71Sopenharmony_ci    fetchColumns: [],
150e41f4b71Sopenharmony_ci    predicates: predicates
151e41f4b71Sopenharmony_ci  };
152e41f4b71Sopenharmony_ci  try {
153e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
154e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
155e41f4b71Sopenharmony_ci    console.info('getAssets photoAsset.uri : ' + photoAsset.uri);
156e41f4b71Sopenharmony_ci    await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]);
157e41f4b71Sopenharmony_ci    fetchResult.close();
158e41f4b71Sopenharmony_ci  } catch (err) {
159e41f4b71Sopenharmony_ci    console.error('onCallback failed with err: ' + err);
160e41f4b71Sopenharmony_ci  }
161e41f4b71Sopenharmony_ci}
162e41f4b71Sopenharmony_ci```
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci## Unregistering Listening for a URI
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ciUse [unRegisterChange](../../reference/apis-media-library-kit/js-apis-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.
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci**Prerequisites**
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
171e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ciThe 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.
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci**How to Develop**
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci1. [Obtain a media asset](photoAccessHelper-resource-guidelines.md#obtaining-media-assets).
178e41f4b71Sopenharmony_ci2. Unregister listening for the URI of the media asset obtained.
179e41f4b71Sopenharmony_ci3. Delete the media asset.
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci```ts
182e41f4b71Sopenharmony_ciimport { dataSharePredicates } from '@kit.ArkData';
183e41f4b71Sopenharmony_ciimport { photoAccessHelper } from '@kit.MediaLibraryKit';
184e41f4b71Sopenharmony_ciconst context = getContext(this);
185e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ciasync function example() {
188e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
189e41f4b71Sopenharmony_ci  predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
190e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
191e41f4b71Sopenharmony_ci    fetchColumns: [],
192e41f4b71Sopenharmony_ci    predicates: predicates
193e41f4b71Sopenharmony_ci  };
194e41f4b71Sopenharmony_ci  try {
195e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
196e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
197e41f4b71Sopenharmony_ci    console.info('getAssets photoAsset.uri : ' + photoAsset.uri);
198e41f4b71Sopenharmony_ci    let onCallback1 = (changeData: photoAccessHelper.ChangeData) => {
199e41f4b71Sopenharmony_ci      console.info('onCallback1, changData: ' + JSON.stringify(changeData));
200e41f4b71Sopenharmony_ci    }
201e41f4b71Sopenharmony_ci    let onCallback2 = (changeData: photoAccessHelper.ChangeData) => {
202e41f4b71Sopenharmony_ci      console.info('onCallback2, changData: ' + JSON.stringify(changeData));
203e41f4b71Sopenharmony_ci    }
204e41f4b71Sopenharmony_ci    phAccessHelper.registerChange(photoAsset.uri, false, onCallback1);
205e41f4b71Sopenharmony_ci    phAccessHelper.registerChange(photoAsset.uri, false, onCallback2);
206e41f4b71Sopenharmony_ci    phAccessHelper.unRegisterChange(photoAsset.uri, onCallback1);
207e41f4b71Sopenharmony_ci    await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]);
208e41f4b71Sopenharmony_ci    fetchResult.close();
209e41f4b71Sopenharmony_ci  } catch (err) {
210e41f4b71Sopenharmony_ci    console.error('onCallback failed with err: ' + err);
211e41f4b71Sopenharmony_ci  }
212e41f4b71Sopenharmony_ci}
213e41f4b71Sopenharmony_ci```
214