1# Album Management 2 3You can use the APIs provided by the **mediaLibrary** module to create and delete an album and obtain images in the album. 4 5> **NOTE** 6> 7> Before developing features, read [MediaLibrary Overview](medialibrary-overview.md) to learn how to obtain a **MediaLibrary** instance and request the permissions to call the APIs of **MediaLibrary**. 8 9To ensure the application running efficiency, most **MediaLibrary** API calls are asynchronous, and both callback and promise modes are provided for these APIs. The following code samples use the promise mode. For details about the APIs, see [MediaLibrary API Reference](../reference/apis/js-apis-medialibrary.md). 10 11## Obtaining Images and Videos in an Album 12 13You can obtain images and videos in an album in either of the following ways: 14 15- Call [MediaLibrary.getFileAssets](../reference/apis/js-apis-medialibrary.md#getfileassets7-1) with an album specified to obtain the media assets. For details, see [Querying Media Assets with the Specified Album Name](medialibrary-resource-guidelines.md#querying-media-assets-with-the-specified-album-name). 16 17- Call [Album.getFileAssets](../reference/apis/js-apis-medialibrary.md#getfileassets7-3) to obtain an **Album** instance, so as to obtain the media assets in it. For details, see [Obtaining Images and Videos in an Album](medialibrary-resource-guidelines.md#obtaining-images-and-videos-in-an-album). 18 19## Creating an Album 20 21You can use [MediaLibrary.createAsset](../reference/apis/js-apis-medialibrary.md#createasset8-1), with the relative path set, to create an album while adding a media asset to the album. The relative path is the album name. 22 23**Prerequisites** 24 25- You have obtained a **MediaLibrary** instance. 26- You have granted the permission **ohos.permission.WRITE_MEDIA**. 27 28The following describes how to create an album named **myAlbum**. 29 30**How to Develop** 31 321. Call **getPublicDirectory** to obtain the public directory that stores files of a certain type. 33 34 For details about the operation, see [Obtaining a Public Directory](medialibrary-filepath-guidelines.md#obtaining-a-public-directory). 35 362. Call **createAsset** to create an image, with the relative path set to **path + 'myAlbum/'**. 37 38 This operation creates an album and adds an image to it. 39 40```ts 41async function example() { 42 let mediaType = mediaLibrary.MediaType.IMAGE; 43 let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; 44 const context = getContext(this); 45 let media = mediaLibrary.getMediaLibrary(context); 46 const path = await media.getPublicDirectory(DIR_IMAGE); 47 // myAlbum is the path for storing the new file and the name of the new album. 48 media.createAsset(mediaType, 'test.jpg', path + 'myAlbum/', (err, fileAsset) => { 49 if (fileAsset === undefined) { 50 console.error('createAlbum failed, message = ' + err); 51 } else { 52 console.info('createAlbum successfully, message = ' + JSON.stringify(fileAsset)); 53 } 54 }); 55} 56``` 57 58## Renaming an Album 59 60Renaming modifies the **FileAsset.albumName** attribute of the album, that is, the album name. After the modification, call [Album.commitModify](../reference/apis/js-apis-medialibrary.md#commitmodify8-3) to commit the modification to the database. 61 62**Prerequisites** 63 64- You have obtained a **MediaLibrary** instance. 65- You have granted the permission **ohos.permission.WRITE_MEDIA**. 66 67The following describes how to rename the album **newAlbum**. 68 69**How to Develop** 70 711. Create a retrieval condition for obtaining the target album. 722. Call **getAlbums** to obtain the album list. 733. Rename the album **newAlbum**. 744. Call **Album.commitModify** to commit the modification of the attributes to the database. 75 76```ts 77async function example() { 78 let AlbumNoArgsfetchOp = { 79 selections: '', 80 selectionArgs: [], 81 }; 82 const context = getContext(this); 83 let media = mediaLibrary.getMediaLibrary(context); 84 let albumList = await media.getAlbums(AlbumNoArgsfetchOp); 85 let album = albumList[0]; 86 album.albumName = 'newAlbum'; 87 // Void callback. 88 album.commitModify().then(() => { 89 console.info("albumRename successfully"); 90 }).catch((err) => { 91 console.error("albumRename failed with error: " + err); 92 }); 93} 94``` 95