harmony 鸿蒙Album Management

  • 2022-10-28
  • 浏览 (729)

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:

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

  1. Call getPublicDirectory to obtain the public directory that stores files of a certain type.

For details about the operation, see Obtaining a Public Directory.

  1. 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

  1. Create a retrieval condition for obtaining the target album.
  2. Call getAlbums to obtain the album list.
  3. Rename the album newAlbum.
  4. 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 Management

harmony 鸿蒙File Access Framework Overview

harmony 鸿蒙FilePicker Guide

harmony 鸿蒙File Path Management

harmony 鸿蒙MediaLibrary Development Overview

harmony 鸿蒙Media Asset Management

0  赞