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. Use getContext to obtain the application context.
  3. Obtain a PhotoAccessHelper instance.
import photoAccessHelper from '@ohos.file.photoAccessHelper';

// 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.
const context = getContext(this);
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. Declare the required permissions in the module.json5 file.
  2. 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.