harmony 鸿蒙Album Management
Album Management
You can use the APIs provided by the mediaLibrary module to create and delete an album and obtain images in the album.
NOTE
Before developing features, read MediaLibrary Overview to learn how to obtain a MediaLibrary instance and request the permissions to call the APIs of MediaLibrary.
To 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.
Obtaining Images and Videos in an Album
You can obtain images and videos in an album in either of the following ways:
Call MediaLibrary.getFileAssets with an album specified to obtain the media assets. For details, see Querying Media Assets with the Specified Album Name.
Call Album.getFileAssets to obtain an Album instance, so as to obtain the media assets in it. For details, see Obtaining Images and Videos in an Album.
Creating an Album
You can use MediaLibrary.createAsset, with the relative path set, to create an album while adding a media asset to the album. The relative path is the album name.
Prerequisites
- You have obtained a MediaLibrary instance.
- You have granted the permission ohos.permission.WRITE_MEDIA.
The following describes how to create an album named myAlbum.
How to Develop
- Call getPublicDirectory to obtain the public directory that stores files of a certain type.
For details about the operation, see Obtaining a Public Directory.
- Call createAsset to create an image, with the relative path set to path + ‘myAlbum/’.
This operation creates an album and adds an image to it.
async function example() {
let mediaType = mediaLibrary.MediaType.IMAGE;
let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
const context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
const path = await media.getPublicDirectory(DIR_IMAGE);
// myAlbum is the path for storing the new file and the name of the new album.
media.createAsset(mediaType, 'test.jpg', path + 'myAlbum/', (err, fileAsset) => {
if (fileAsset != undefined) {
console.info('createAlbum successfully, message = ' + fileAsset);
} else {
console.info('createAlbum failed, message = ' + err);
}
});
}
Renaming an Album
Renaming modifies the FileAsset.albumName attribute of the album, that is, the album name. After the modification, call Album.commitModify to commit the modification to the database.
Prerequisites
- You have obtained a MediaLibrary instance.
- You have granted the permission ohos.permission.WRITE_MEDIA.
The following describes how to rename the album newAlbum.
How to Develop
- Create a retrieval condition for obtaining the target album.
- Call getAlbums to obtain the album list.
- Rename the album newAlbum.
- Call Album.commitModify to commit the modification of the attributes to the database.
async function example() {
let AlbumNoArgsfetchOp = {
selections: '',
selectionArgs: [],
};
const context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
let albumList = await media.getAlbums(AlbumNoArgsfetchOp);
let album = albumList[0];
album.albumName = 'newAlbum';
// Void callback.
album.commitModify().then(function() {
console.info("albumRename successfully");
}).catch(function(err){
console.info("albumRename failed with error: " + err);
});
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙File Access Framework Overview
harmony 鸿蒙File Path Management
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦