photoAccessHelper-userAlbum-guidelines.md 14.8 KB
Newer Older
A
Annie_wang 已提交
1
# Managing User Albums
A
Annie_wang 已提交
2 3 4 5 6

The **photoAccessHelper** module provides APIs for user album management, including creating or deleting a user album, adding images and videos to a user album, and deleting image and videos from a user album.

> **NOTE**
>
A
Annie_wang 已提交
7 8
> Before you start, you need to obtain a **PhotoAccessHelper** instance and apply for required permissions. For details, see [photoAccessHelper Overview](photoAccessHelper-overview.md).<br>
> By default, the **PhotoAccessHelper** instance obtained in [photoAccessHelper Overview](photoAccessHelper-overview.md) is used when **photoAccessHelper** APIs are used. If the code for obtaining the **PhotoAccessHelper** instance is not added, an error indicating that **photoAccessHelper** is not defined is reported.
A
Annie_wang 已提交
9

A
Annie_wang 已提交
10
To ensure application running efficiency, most **photoAccessHelper** APIs are asynchronously implemented in callback or promise mode. The following code samples use promise-based APIs. For details about the APIs, see [Album Management](../reference/apis/js-apis-photoAccessHelper.md).
A
Annie_wang 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Unless otherwise specified, all the media assets to be obtained in this document exist in the database. If no media asset is obtained when the sample code is executed, check whether the media assets exist in the database.

## Creating a User Album

Use [createAlbum](../reference/apis/js-apis-photoAccessHelper.md#createalbum) to create a user album.

The album name must meet the following requirements:

- The album name is a string of 1 to 255 characters.
- The album name cannot contain any of the following characters:<br>\ / : * ? " ' ` < > | { } [ ]
- The album name is case-insensitive.
- Duplicate album names are not allowed.

**Prerequisites**

A
Annie_wang 已提交
26
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
27 28 29 30 31 32
- The application has the **ohos.permission.WRITE_IMAGEVIDEO** permission.

Example: Create a user album.

**How to Develop**

A
Annie_wang 已提交
33
1. Set the name of the album.
A
Annie_wang 已提交
34 35 36
2. Use **createAlbum** to create an album.

```ts
N
nwx1279094 已提交
37 38
import photoAccessHelper from '@ohos.file.photoAccessHelper';

A
Annie_wang 已提交
39
try {
N
nwx1279094 已提交
40 41
  let albumName: photoAccessHelper.AlbumName = 'albumName';
  let album: photoAccessHelper.Album = await phAccessHelper.createAlbum(albumName);
A
Annie_wang 已提交
42 43 44 45 46 47 48 49 50 51 52 53
  console.info('createAlbum successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri);
} catch (err) {
  console.error('createAlbum failed with err: ' + err);
}
```

## Obtaining a User Album

Use [getAlbums](../reference/apis/js-apis-photoAccessHelper.md#getalbums) to obtain a user album.

**Prerequisites**

A
Annie_wang 已提交
54
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
55 56
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.

A
Annie_wang 已提交
57
Example: Obtain the user album named **albumName**.
A
Annie_wang 已提交
58 59 60 61 62 63 64 65 66

**How to Develop**

1. Set **fetchOptions** for obtaining the user album.
2. Call **getAlbums** to obtain user albums.
3. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first user album.

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
N
nwx1279094 已提交
67
import photoAccessHelper from '@ohos.file.photoAccessHelper';
A
Annie_wang 已提交
68

N
nwx1279094 已提交
69 70
let predicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper = photoAccessHelper.AlbumKey.ALBUM_NAME;
A
Annie_wang 已提交
71
predicates.equalTo(albumName, 'albumName');
N
nwx1279094 已提交
72
let fetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
73 74 75 76 77
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
78 79
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
  let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
A
Annie_wang 已提交
80 81 82 83 84 85 86 87 88
  console.info('getAlbums successfully, albumName: ' + album.albumName);
  fetchResult.close();
} catch (err) {
  console.error('getAlbums failed with err: ' + err);
}
```

## Renaming a User Album

A
Annie_wang 已提交
89
Modify the **Albums.albumName** attribute of the album, and use [Album.commitModify](../reference/apis/js-apis-photoAccessHelper.md#commitmodify-2) to update the modification to the database.
A
Annie_wang 已提交
90

A
Annie_wang 已提交
91
Before renaming a user album, you need to obtain an album object. You can use the [FetchResult](../reference/apis/js-apis-photoAccessHelper.md#fetchresult) APIs to obtain the user album of the specified location.
A
Annie_wang 已提交
92 93 94 95 96

The new user album names must also comply with the user name requirements in [Creating a User Album](#creating-a-user-album).

**Prerequisites**

A
Annie_wang 已提交
97
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
98 99 100 101 102 103 104 105 106 107
- The application has the **ohos.permission.READ_IMAGEVIDEO** and **ohos.permission.WRITE_IMAGEVIDEO** permissions.

Example: Rename an album named **albumName**.

**How to Develop**

1. Set **fetchOptions** for obtaining the user album.
2. Call **getAlbums** to obtain user albums.
3. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first user album.
4. Set a new album name.
A
Annie_wang 已提交
108
5. Call **Album.commitModify** to update the new album name to the database.
A
Annie_wang 已提交
109 110 111

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
N
nwx1279094 已提交
112
import photoAccessHelper from '@ohos.file.photoAccessHelper';
A
Annie_wang 已提交
113

N
nwx1279094 已提交
114 115
let predicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper = photoAccessHelper.AlbumKey.ALBUM_NAME;
A
Annie_wang 已提交
116
predicates.equalTo(albumName, 'albumName');
N
nwx1279094 已提交
117
let fetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
118 119 120 121 122
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
123 124
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
  let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
A
Annie_wang 已提交
125 126 127 128 129 130 131 132 133
  console.info('getAlbums successfully, albumName: ' + album.albumName);
  album.albumName = 'newAlbumName';
  await album.commitModify();
  fetchResult.close();
} catch (err) {
  console.error('commitModify failed with err: ' + err);
}
```

A
Annie_wang 已提交
134
## Adding Images or Videos to a User Album
A
Annie_wang 已提交
135

A
Annie_wang 已提交
136
[Obtain the user album](#obtaining-a-user-album) and the images or videos to be added to the album, and then call [Album.addAssets](../reference/apis/js-apis-photoAccessHelper.md#addassets) to add the images or videos to the user album.
A
Annie_wang 已提交
137 138 139

**Prerequisites**

A
Annie_wang 已提交
140
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
141 142
- The application has the **ohos.permission.READ_IMAGEVIDEO** and **ohos.permission.WRITE_IMAGEVIDEO** permissions.

A
Annie_wang 已提交
143
Example: Add an image to the album **albumName**.
A
Annie_wang 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156

**How to Develop**

1. Set **albumFetchOptions** for obtaining the user album.
2. Set **photoFetchOptions** for obtaining the image.
3. Call **getAlbums** to obtain user albums.
4. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first user album.
5. Call [PhotoAccessHelper.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets) to obtain image assets.
6. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first image from the result set.
7. Call **Album.addAssets** to add the image to the user album.

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
N
nwx1279094 已提交
157
import photoAccessHelper from '@ohos.file.photoAccessHelper';
A
Annie_wang 已提交
158

N
nwx1279094 已提交
159 160
let albumPredicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper = photoAccessHelper.AlbumKey.ALBUM_NAME;
A
Annie_wang 已提交
161
albumPredicates.equalTo(albumName, 'albumName');
N
nwx1279094 已提交
162
let albumFetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
163 164 165 166
  fetchColumns: [],
  predicates: albumPredicates
};

N
nwx1279094 已提交
167 168
let photoPredicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
169 170 171 172 173
  fetchColumns: [],
  predicates: photoPredicates
};

try {
N
nwx1279094 已提交
174 175
  let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
  let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
A
Annie_wang 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189
  console.info('getAlbums successfully, albumName: ' + album.albumName);
  let photoFetchResult = await phAccessHelper.getAssets(photoFetchOptions);
  let fileAsset = await photoFetchResult.getFirstObject();
  console.info('getAssets successfully, albumName: ' + fileAsset.displayName);
  await album.addAssets([fileAsset]);
  albumFetchResult.close();
  photoFetchResult.close();
} catch (err) {
  console.error('addAssets failed with err: ' + err);
}
```

## Obtaining Images and Videos in a User Album

A
Annie_wang 已提交
190
[Obtain the user album](#obtaining-a-user-album), and call [Album.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets-2) to obtain the media assets in the user album.
A
Annie_wang 已提交
191 192 193

**Prerequisites**

A
Annie_wang 已提交
194
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
195 196
- The application has the **ohos.permission.READ_IMAGEVIDEO** and **ohos.permission.WRITE_IMAGEVIDEO** permissions.

A
Annie_wang 已提交
197
Example: Obtain an image in the user album **albumName**.
A
Annie_wang 已提交
198 199 200 201 202 203 204 205 206 207 208 209

**How to Develop**

1. Set **albumFetchOptions** for obtaining the user album.
2. Set **photoFetchOptions** for obtaining the image.
3. Call **getAlbums** to obtain user albums.
4. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first user album.
5. Call **Album.getAssets** to obtain the image assets in the user album.
6. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first image from the result set.

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
N
nwx1279094 已提交
210
import photoAccessHelper from '@ohos.file.photoAccessHelper';
A
Annie_wang 已提交
211

N
nwx1279094 已提交
212 213
let albumPredicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper = photoAccessHelper.AlbumKey.ALBUM_NAME;
A
Annie_wang 已提交
214
albumPredicates.equalTo(albumName, 'albumName');
N
nwx1279094 已提交
215
let albumFetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
216 217 218 219
  fetchColumns: [],
  predicates: albumPredicates
};

N
nwx1279094 已提交
220 221
let photoPredicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
222 223 224 225 226
  fetchColumns: [],
  predicates: photoPredicates
};

try {
N
nwx1279094 已提交
227 228
  let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
  let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
A
Annie_wang 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241
  console.info('getAlbums successfully, albumName: ' + album.albumName);
  let photoFetchResult = await album.getAssets(photoFetchOptions);
  let fileAsset = await photoFetchResult.getFirstObject();
  console.info('album getAssets successfully, albumName: ' + fileAsset.displayName);
  albumFetchResult.close();
  photoFetchResult.close();
} catch (err) {
  console.error('album getAssets failed with err: ' + err);
}
```

## Removing Images and Videos from a User Album

A
Annie_wang 已提交
242
[Obtain the user album](#obtaining-a-user-album), and call [Album.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets-2) to obtain the media assets in the user album.
A
Annie_wang 已提交
243

A
Annie_wang 已提交
244
Use [Album.removeAssets](../reference/apis/js-apis-photoAccessHelper.md#removeassets) to remove the specified media assets.
A
Annie_wang 已提交
245 246 247

**Prerequisites**

A
Annie_wang 已提交
248
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
- The application has the **ohos.permission.READ_IMAGEVIDEO** and **ohos.permission.WRITE_IMAGEVIDEO** permissions.

Example: Remove an image from the album named **albumName**.

**How to Develop**

1. Set **albumFetchOptions** for obtaining the user album.
2. Set **photoFetchOptions** for obtaining the image.
3. Call **getAlbums** to obtain user albums.
4. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first user album.
5. Call **Album.getAssets** to obtain the image assets.
6. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first image from the result set.
7. Call **Album.removeAssets** to remove the image from the user album.

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
N
nwx1279094 已提交
265
import photoAccessHelper from '@ohos.file.photoAccessHelper';
A
Annie_wang 已提交
266

N
nwx1279094 已提交
267 268
let albumPredicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper = photoAccessHelper.AlbumKey.ALBUM_NAME;
A
Annie_wang 已提交
269
albumPredicates.equalTo(albumName, 'albumName');
N
nwx1279094 已提交
270
let albumFetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
271 272 273 274
  fetchColumns: [],
  predicates: albumPredicates
};

N
nwx1279094 已提交
275 276
let photoPredicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let photoFetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
277 278 279 280 281
  fetchColumns: [],
  predicates: photoPredicates
};

try {
N
nwx1279094 已提交
282 283
  let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
  let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
A
Annie_wang 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297
  console.info('getAlbums successfully, albumName: ' + album.albumName);
  let photoFetchResult = await album.getAssets(photoFetchOptions);
  let fileAsset = await photoFetchResult.getFirstObject();
  console.info('album getAssets successfully, albumName: ' + fileAsset.displayName);
  await album.removeAssets([fileAsset]);
  albumFetchResult.close();
  photoFetchResult.close();
} catch (err) {
  console.error('removeAssets failed with err: ' + err);
}
```

## Deleting a User Album

A
Annie_wang 已提交
298
[Obtain the user album](##obtaining-a-user-album), and call [deleteAlbums](../reference/apis/js-apis-photoAccessHelper.md#deletealbums) to delete the user album.
A
Annie_wang 已提交
299 300 301

**Prerequisites**

A
Annie_wang 已提交
302
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315
- The application has the **ohos.permission.READ_IMAGEVIDEO** and **ohos.permission.WRITE_IMAGEVIDEO** permissions.

Example: Delete a user album named **albumName**.

**How to Develop**

1. Set **fetchOptions** for obtaining the user album.
2. Call **getAlbums** to obtain user albums.
3. Call **FetchResult.getFirstObject** to obtain the first user album.
4. Call **deleteAlbums** to delete the user album.

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';
N
nwx1279094 已提交
316
import photoAccessHelper from '@ohos.file.photoAccessHelper';
A
Annie_wang 已提交
317

N
nwx1279094 已提交
318 319
let predicates: dataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumName: photoAccessHelper.AlbumName = photoAccessHelper.AlbumKey.ALBUM_NAME;
A
Annie_wang 已提交
320
predicates.equalTo(albumName, '%albumName%');
N
nwx1279094 已提交
321
let fetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
322 323 324 325 326
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
327 328
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
  let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
A
Annie_wang 已提交
329 330 331 332 333 334 335
  console.info('getAlbums successfully, albumName: ' + album.albumName);
  phAccessHelper.deleteAlbums([album]);
  fetchResult.close();
} catch (err) {
  console.error('deleteAlbums failed with err: ' + err);
}
```