1# Managing User Albums 2<!--Kit: Media Library Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @yixiaoff--> 5<!--SE: @liweilu1--> 6<!--TSE: @xchaosioda--> 7 8The photoAccessHelper module provides APIs for user album management, including creating or deleting a user album, adding images and videos to a user album, and deleting images and videos from a user album. 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 16To ensure application running efficiency, most PhotoAccessHelper APIs are asynchronously implemented in callback or promise mode. The following examples use promise-based APIs. For details about the 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<!--Del--> 21## Creating a User Album (for System Applications Only) 22 23Use [MediaAlbumChangeRequest.createAlbumRequest](../../reference/apis-media-library-kit/js-apis-photoAccessHelper-sys.md#createalbumrequest11) and [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to create a user album. 24 25The album name must meet the following requirements: 26 27- The album name cannot exceed 255 characters. 28- The album name cannot contain any of the following characters:<br>. \ / : * ? " ' ` < > | { } [ ] 29- The album name is case-insensitive. 30- Duplicate album names are not allowed. 31 32**Prerequisites** 33 34- A PhotoAccessHelper instance is obtained. 35- The application has the ohos.permission.WRITE_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 36 37Example: Create a user album. 38 39**How to Develop** 40 411. Set the name of the album. 422. Call **MediaAlbumChangeRequest.createAlbumRequest** to create an album change request object. 433. Call **PhotoAccessHelper.applyChanges** to apply the changes. 44 45```ts 46import { photoAccessHelper } from '@kit.MediaLibraryKit'; 47 48async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 49 try { 50 let albumName = 'albumName'; 51 let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = photoAccessHelper.MediaAlbumChangeRequest.createAlbumRequest(context, albumName); 52 await phAccessHelper.applyChanges(albumChangeRequest); 53 let album: photoAccessHelper.Album = albumChangeRequest.getAlbum(); 54 console.info('create album successfully, album name: ' + album.albumName + ' uri: ' + album.albumUri); 55 } catch (err) { 56 console.error('create album failed with err: ' + err); 57 } 58} 59``` 60<!--DelEnd--> 61 62## Obtaining a User Album 63 64Use [PhotoAccessHelper.getAlbums](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#getalbums-2) to obtain user albums. 65 66**Prerequisites** 67 68- A PhotoAccessHelper instance is obtained. 69- The application has the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 70 71Example: Obtain the user album **albumName**. 72 73**How to Develop** 74 751. Set **fetchOptions** for obtaining the user album. 762. Call **PhotoAccessHelper.getAlbums** to obtain user albums. 773. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first user album. 78 79```ts 80import { dataSharePredicates } from '@kit.ArkData'; 81import { photoAccessHelper } from '@kit.MediaLibraryKit'; 82 83async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 84 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 85 let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 86 predicates.equalTo(albumName, 'albumName'); 87 let fetchOptions: photoAccessHelper.FetchOptions = { 88 fetchColumns: [], 89 predicates: predicates 90 }; 91 92 try { 93 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); 94 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 95 console.info('getAlbums successfully, albumName: ' + album.albumName); 96 fetchResult.close(); 97 } catch (err) { 98 console.error('getAlbums failed with err: ' + err); 99 } 100} 101``` 102 103## Renaming a User Album 104 105To rename a user album, modify the **Album.albumName** attribute of the album. 106 107Use [FetchResult](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md) to obtain the user album to rename, use [MediaAlbumChangeRequest.setAlbumName](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAlbumChangeRequest.md#setalbumname11) to set the new name, and then use [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to apply the changes to the database. 108 109The new user album names must comply with the following requirements: 110 111- The album name cannot exceed 255 characters. 112- The album name cannot contain any of the following characters:<br>. \ / : * ? " ' ` < > | { } [ ] 113- The album name is case-insensitive. 114- Duplicate album names are not allowed. 115 116**Prerequisites** 117 118- A PhotoAccessHelper instance is obtained. 119- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 120 121Example: Rename the user album **albumName**. 122 123**How to Develop** 124 1251. Set **fetchOptions** for obtaining the user album. 1262. Call **PhotoAccessHelper.getAlbums** to obtain user albums. 1273. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first user album. 1284. Call **MediaAlbumChangeRequest.setAlbumName** to set a new album name. 1295. Call **PhotoAccessHelper.applyChanges** to save the new album name to the database. 130 131```ts 132import { dataSharePredicates } from '@kit.ArkData'; 133import { photoAccessHelper } from '@kit.MediaLibraryKit'; 134 135async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 136 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 137 let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 138 predicates.equalTo(albumName, 'albumName'); 139 let fetchOptions: photoAccessHelper.FetchOptions = { 140 fetchColumns: [], 141 predicates: predicates 142 }; 143 144 try { 145 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); 146 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 147 console.info('getAlbums successfully, albumName: ' + album.albumName); 148 let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = new photoAccessHelper.MediaAlbumChangeRequest(album); 149 let newAlbumName: string = 'newAlbumName'; 150 albumChangeRequest.setAlbumName(newAlbumName); 151 await phAccessHelper.applyChanges(albumChangeRequest); 152 console.info('setAlbumName successfully, new albumName: ' + album.albumName); 153 fetchResult.close(); 154 } catch (err) { 155 console.error('setAlbumName failed with err: ' + err); 156 } 157} 158``` 159 160## Adding Images or Videos to a User Album 161 162[Obtain the user album](#obtaining-a-user-album) and the images or videos to be added to the user album, and then call [MediaAlbumChangeRequest.addAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAlbumChangeRequest.md#addassets11) and [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to add the images or videos to the user album. 163 164**Prerequisites** 165 166- A PhotoAccessHelper instance is obtained. 167- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 168 169Example: Add an image to the user album **albumName**. 170 171**How to Develop** 172 1731. Set **albumFetchOptions** for obtaining the user album. 1742. Set **photoFetchOptions** for obtaining the image. 1753. Call **PhotoAccessHelper.getAlbums** to obtain user albums. 1764. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject) to obtain the first user album. 1775. Call [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#getassets) to obtain image assets. 1786. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject) to obtain the first image from the result set. 1797. Call **MediaAlbumChangeRequest.addAssets** to add the image to the user album. 1808. Call **PhotoAccessHelper.applyChanges** to apply the changes. 181 182```ts 183import { dataSharePredicates } from '@kit.ArkData'; 184import { photoAccessHelper } from '@kit.MediaLibraryKit'; 185 186async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 187 let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 188 let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 189 albumPredicates.equalTo(albumName, 'albumName'); 190 let albumFetchOptions: photoAccessHelper.FetchOptions = { 191 fetchColumns: [], 192 predicates: albumPredicates 193 }; 194 195 let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 196 let photoFetchOptions: photoAccessHelper.FetchOptions = { 197 fetchColumns: [], 198 predicates: photoPredicates 199 }; 200 201 try { 202 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 203 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 204 console.info('getAlbums successfully, albumName: ' + album.albumName); 205 let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(photoFetchOptions); 206 let photoAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject(); 207 console.info('getAssets successfully, albumName: ' + photoAsset.displayName); 208 let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = new photoAccessHelper.MediaAlbumChangeRequest(album); 209 albumChangeRequest.addAssets([photoAsset]); 210 await phAccessHelper.applyChanges(albumChangeRequest); 211 console.info('succeed to add ' + photoAsset.displayName + ' to ' + album.albumName); 212 albumFetchResult.close(); 213 photoFetchResult.close(); 214 } catch (err) { 215 console.error('addAssets failed with err: ' + err); 216 } 217} 218``` 219 220## Obtaining Images and Videos in a User Album 221 222[Obtain the user album](#obtaining-a-user-album), and call [Album.getAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-AbsAlbum.md#getassets-1) to obtain the assets in the user album. 223 224**Prerequisites** 225 226- A PhotoAccessHelper instance is obtained. 227- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 228 229Example: Obtain an image in the user album **albumName**. 230 231**How to Develop** 232 2331. Set **albumFetchOptions** for obtaining the user album. 2342. Set **photoFetchOptions** for obtaining the image. 2353. Call **PhotoAccessHelper.getAlbums** to obtain user albums. 2364. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first user album. 2375. Call **Album.getAssets** to obtain the media assets in the user album. 2386. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first image from the result set. 239 240```ts 241import { dataSharePredicates } from '@kit.ArkData'; 242import { photoAccessHelper } from '@kit.MediaLibraryKit'; 243 244async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 245 let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 246 let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 247 albumPredicates.equalTo(albumName, 'albumName'); 248 let albumFetchOptions: photoAccessHelper.FetchOptions = { 249 fetchColumns: [], 250 predicates: albumPredicates 251 }; 252 253 let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 254 let photoFetchOptions: photoAccessHelper.FetchOptions = { 255 fetchColumns: [], 256 predicates: photoPredicates 257 }; 258 259 try { 260 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 261 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 262 console.info('getAlbums successfully, albumName: ' + album.albumName); 263 let photoFetchResult = await album.getAssets(photoFetchOptions); 264 let photoAsset = await photoFetchResult.getFirstObject(); 265 console.info('album getAssets successfully, albumName: ' + photoAsset.displayName); 266 albumFetchResult.close(); 267 photoFetchResult.close(); 268 } catch (err) { 269 console.error('album getAssets failed with err: ' + err); 270 } 271} 272``` 273 274## Removing Images and Videos from a User Album 275 276[Obtain the user album](#obtaining-a-user-album), and call [Album.getAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-AbsAlbum.md#getassets-1) to obtain the assets in the user album. 277 278Select the assets to remove, and use [MediaAlbumChangeRequest.removeAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAlbumChangeRequest.md#removeassets11) and [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to remove the selected media assets. 279 280**Prerequisites** 281 282- A PhotoAccessHelper instance is obtained. 283- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 284 285Example: Remove an image from the user album **albumName**. 286 287**How to Develop** 288 2891. Set **albumFetchOptions** for obtaining the user album. 2902. Set **photoFetchOptions** for obtaining the image. 2913. Call **PhotoAccessHelper.getAlbums** to obtain user albums. 2924. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first user album. 2935. Call **Album.getAssets** to obtain the media assets. 2946. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first image from the result set. 2957. Call **MediaAlbumChangeRequest.removeAssets** to remove the image from the user album. 2968. Call **PhotoAccessHelper.applyChanges** to apply the changes. 297 298```ts 299import { dataSharePredicates } from '@kit.ArkData'; 300import { photoAccessHelper } from '@kit.MediaLibraryKit'; 301 302async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 303 let albumPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 304 let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 305 albumPredicates.equalTo(albumName, 'albumName'); 306 let albumFetchOptions: photoAccessHelper.FetchOptions = { 307 fetchColumns: [], 308 predicates: albumPredicates 309 }; 310 311 let photoPredicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 312 let photoFetchOptions: photoAccessHelper.FetchOptions = { 313 fetchColumns: [], 314 predicates: photoPredicates 315 }; 316 317 try { 318 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 319 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 320 console.info('getAlbums successfully, albumName: ' + album.albumName); 321 let photoFetchResult = await album.getAssets(photoFetchOptions); 322 let photoAsset = await photoFetchResult.getFirstObject(); 323 console.info('album getAssets successfully, albumName: ' + photoAsset.displayName); 324 let albumChangeRequest: photoAccessHelper.MediaAlbumChangeRequest = new photoAccessHelper.MediaAlbumChangeRequest(album); 325 albumChangeRequest.removeAssets([photoAsset]); 326 await phAccessHelper.applyChanges(albumChangeRequest); 327 console.info('succeed to remove ' + photoAsset.displayName + ' from ' + album.albumName); 328 albumFetchResult.close(); 329 photoFetchResult.close(); 330 } catch (err) { 331 console.error('removeAssets failed with err: ' + err); 332 } 333} 334``` 335 336<!--Del--> 337## Deleting a User Album (for System Applications Only) 338 339[Obtain the user album](#obtaining-a-user-album), and call [MediaAlbumChangeRequest.deleteAlbums](../../reference/apis-media-library-kit/js-apis-photoAccessHelper-sys.md#deletealbums11) to delete the user album. 340 341**Prerequisites** 342 343- A PhotoAccessHelper instance is obtained. 344- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions). 345 346Example: Delete the user album **albumName**. 347 348**How to Develop** 349 3501. Set **fetchOptions** for obtaining the user album. 3512. Call **PhotoAccessHelper.getAlbums** to obtain user albums. 3523. Call **FetchResult.getFirstObject** to obtain the first user album. 3534. Call **MediaAlbumChangeRequest.deleteAlbums** to delete the user album. 354 355```ts 356import { dataSharePredicates } from '@kit.ArkData'; 357import { photoAccessHelper } from '@kit.MediaLibraryKit'; 358 359async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 360 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 361 let albumName: photoAccessHelper.AlbumKeys = photoAccessHelper.AlbumKeys.ALBUM_NAME; 362 predicates.equalTo(albumName, 'albumName'); 363 let fetchOptions: photoAccessHelper.FetchOptions = { 364 fetchColumns: [], 365 predicates: predicates 366 }; 367 368 try { 369 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); 370 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 371 console.info('getAlbums successfully, albumName: ' + album.albumName); 372 await photoAccessHelper.MediaAlbumChangeRequest.deleteAlbums(context, [album]); 373 fetchResult.close(); 374 } catch (err) { 375 console.error('deleteAlbums failed with err: ' + err); 376 } 377} 378``` 379<!--DelEnd--> 380