harmony 鸿蒙Before You Start

  • 2025-06-12
  • 浏览 (4)

Before You Start

An application needs to obtain a PhotoAccessHelper instance before accessing or modifying the media data in an album. User personal data is involved in the photoAccessHelper module. Therefore, the application must also apply for the related read and write permissions from the user. Unless otherwise specified, the APIs of the photoAccessHelper module are used in pages/index.ets of the project or other customized .ets files.

Obtaining a PhotoAccessHelper Instance

The application needs to call getPhotoAccessHelper to obtain a PhotoAccessHelper instance based on the application context. Then, the application can use the instance obtained to access or modify the media data (such as images and videos) in an album.

How to Develop

  1. Import the photoAccessHelper module.
  2. Call getContext to obtain the application context.
  3. Obtain a PhotoAccessHelper instance.
import { photoAccessHelper } from '@kit.MediaLibraryKit';
import { common } from '@kit.AbilityKit';

// The photoAccessHelper instance obtained here is a global object. Unless otherwise specified, the object obtained here is used in subsequent operations in this document. If an undefined error is reported, add the code snippet here.
// Obtain the context from the component and ensure that the return value of this.getUiContext().getHostContext() is UIAbilityContext.
let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext;
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);

Requesting Permissions

Before requesting the permissions for the PhotoAccessHelper module, ensure that the basic principles for using permissions are met. The following permissions are required.

Permission Description Authorization Mode
ohos.permission.READ_IMAGEVIDEO Allows an application to read images and videos in the media library. user_grant
ohos.permission.WRITE_IMAGEVIDEO Allows an application to read and write images and videos in the media library. user_grant

The required permissions must be authorized by the user. After adding the permissions in the module.json5 file, use abilityAccessCtrl.requestPermissionsFromUser to check whether the required permissions are granted by the user. If yes, the application can access the data. Otherwise, display a dialog box to request user authorization.

How to Develop 1. Request the required permissions via the ACL. For details, see Requesting Restricted Permissions. 2. Declare the required permissions in the module.json5 file. 3. Request user authorization.

NOTE

Even if the user has granted the permission, the permission will still be checked before an API protected by the permission is called. The permission granted status should not be persisted, because the user can revoke the permission through the system application Settings.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Media Library Kit (Media File Management Service)

harmony 鸿蒙Playing Moving Photos with MovingPhotoView

harmony 鸿蒙Accessing and Managing Moving Photos

harmony 鸿蒙Observing Media Assets

harmony 鸿蒙Introduction to Media Library Kit

harmony 鸿蒙Selecting Media Assets Using Picker

harmony 鸿蒙Managing Media Assets

harmony 鸿蒙Saving Media Assets

harmony 鸿蒙Managing System Albums

harmony 鸿蒙Managing User Albums

0  赞