• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Before You Start
2
3An 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.
4
5## Obtaining a PhotoAccessHelper Instance
6
7The application needs to call [getPhotoAccessHelper](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#photoaccesshelpergetphotoaccesshelper) 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.
8
9**How to Develop**
10
111. Import the **photoAccessHelper** module.
122. Use **getContext** to obtain the application context.
133. Obtain a **PhotoAccessHelper** instance.
14
15```ts
16import { photoAccessHelper } from '@kit.MediaLibraryKit';
17
18// 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.
19const context = getContext(this);
20let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
21```
22
23## Requesting Permissions
24
25Before requesting the permissions for the **PhotoAccessHelper** module, ensure that the [basic principles for using permissions](../../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. The following permissions are required.
26
27| Permission                        | Description                                      | Authorization Mode  |
28| ------------------------------ | ------------------------------------------ | ---------- |
29| ohos.permission.READ_IMAGEVIDEO     | Allows an application to read images and videos in the media library.| user_grant |
30| ohos.permission.WRITE_IMAGEVIDEO    | Allows an application to read and write images and videos in the media library.| user_grant |
31
32The required permissions must be authorized by the user. After adding the permissions in the **module.json5** file, use [abilityAccessCtrl.requestPermissionsFromUser](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9) 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.
33
34**How to Develop**
35<!--RP1-->
361. Request the required permissions via the ACL. For details, see [Requesting Restricted Permissions](../../security/AccessToken/declare-permissions-in-acl.md).
37<!--RP1End-->
382. [Declare the required permissions in the **module.json5** file](../../security/AccessToken/declare-permissions.md).
393. [Request user authorization](../../security/AccessToken/request-user-authorization.md).
40
41> **NOTE**
42>
43> 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**.
44