PhotoAccessHelper Overview

PhotoAccessHelper provides album management capabilities, including creating and accessing an album and accessing and modifying media data in albums. You can use the APIs provided by PhotoAccessHelper to:

NOTE

This development guide applies only to API version 10 based on the stage model.

An application needs to obtain a PhotoAccessHelper instance before accessing and modifying the media data in an album. User personal data is involved in the photoAccessHelper module. Therefore, the application needs to 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.

Before using the PhotoAccessHelper APIs, you need to:

Obtaining a PhotoAccessHelper Instance

The application needs to call getPhotoAccessHelper to obtain a PhotoAccessHelper instance based on the application context. The application can then 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);

Applying for Permissions

Before applying for permissions, ensure that the basic principles for permission management are complied with. Apply for the following permissions.

Permission Description Authorization Mode
ohos.permission.READ_IMAGEVIDEO Allows an application to read image and video file information from a user's external storage. user_grant
ohos.permission.WRITE_IMAGEVIDEO Allows an application to read and write image and video file information on a user's external storage. 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, a dialog box will be displayed to request user authorization.

How to Develop

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