photoAccessHelper-systemAlbum-guidelines.md 13.4 KB
Newer Older
A
Annie_wang 已提交
1
# Managing System Albums
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
The **photoAccessHelper** module provides APIs for managing system albums, including the **Favorites**, **Videos**, **Screenshots**.
A
Annie_wang 已提交
4 5 6

> **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
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.

## Favorites

A
Annie_wang 已提交
15
**Favorites** is a system album. Favoriting a photo or video adds the photo or video to **Favorites**; unfavoriting a photo or video removes the photo or video from **Favorites**.
A
Annie_wang 已提交
16 17 18 19 20 21 22

### Obtaining a Favorites Object

Use [getAlbums](../reference/apis/js-apis-photoAccessHelper.md#getalbums) to obtain a **Favorites** object.

**Prerequisites**

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

**How to Develop**

1. Set the album type to **photoAccessHelper.AlbumType.SYSTEM** and the album subtype to **photoAccessHelper.AlbumSubtype.FAVORITE**.
2. Call **getAlbums** to obtain a **Favorites** object.

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

A
Annie_wang 已提交
34
try {
N
nwx1279094 已提交
35 36
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
  let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
A
Annie_wang 已提交
37 38 39 40 41 42 43 44 45
  console.info('get favorite Album successfully, albumUri: ' + album.albumUri);
  fetchResult.close();
} catch (err) {
  console.error('get favorite Album failed with err: ' + err);
}
```

### Favoriting an Image or Video

A
Annie_wang 已提交
46
Use [setFavorite](../reference/apis/js-apis-photoAccessHelper.md#setfavorite) to add an image or video to **Favorites**.
A
Annie_wang 已提交
47 48 49

**Prerequisites**

A
Annie_wang 已提交
50
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
51 52 53 54 55 56 57 58
- The application has the **ohos.permission.READ_IMAGEVIDEO** and **ohos.permission.WRITE_IMAGEVIDEO** permissions.

Example: Favorite an image.

**How to Develop**

1. [Obtain media assets](photoAccessHelper-resource-guidelines.md#obtaining-the-specified-media-assets).
2. Set **favoriteState** to **true** to favorite the image.
A
Annie_wang 已提交
59
3. Call **FileAsset.setFavorite** to add the image to **Favorites**.
A
Annie_wang 已提交
60 61 62

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

N
nwx1279094 已提交
65
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
A
Annie_wang 已提交
66
predicates.equalTo(photoAccessHelper.ImageVideoKey.DISPLAY_NAME, 'test.jpg');
N
nwx1279094 已提交
67
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
68 69 70 71 72
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
73 74
  let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
A
Annie_wang 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
  console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
  let favoriteState = true;
  await fileAsset.setFavorite(favoriteState);
} catch (err) {
  console.error('setFavorite failed with err: ' + err);
}
```

### Obtaining Images and Videos in Favorites

[Obtain a **Favorites** object](#obtaining-a-favorites-object), and call [Album.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets-2) to obtain the assets in **Favorites**.

**Prerequisites**

A
Annie_wang 已提交
89
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.

Example: Obtain an image from **Favorites**.

**How to Develop**

1. [Obtain a **Favorites** object](#obtaining-a-favorites-object).
2. Set **fetchOptions** for obtaining the image.
3. Call **Album.getAssets** to obtain the image assets.
4. 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 已提交
103
import photoAccessHelper from '@ohos.file.photoAccessHelper';
A
Annie_wang 已提交
104

N
nwx1279094 已提交
105 106
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
107 108 109 110 111
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
112 113
  let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
  let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
A
Annie_wang 已提交
114 115
  console.info('get favorite Album successfully, albumUri: ' + album.albumUri);

N
nwx1279094 已提交
116 117
  let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
A
Annie_wang 已提交
118 119 120 121 122 123 124 125 126 127
  console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName);
  photoFetchResult.close();
  albumFetchResult.close();
} catch (err) {
  console.error('favorite failed with err: ' + err);
}
```

### Unfavoriting an Image or Video

A
Annie_wang 已提交
128
Use [setFavorite](../reference/apis/js-apis-photoAccessHelper.md#setfavorite) to remove images or videos from **Favorites**.
A
Annie_wang 已提交
129 130 131

**Prerequisites**

A
Annie_wang 已提交
132
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
133 134 135 136 137 138
- The application has the **ohos.permission.READ_IMAGEVIDEO** and **ohos.permission.WRITE_IMAGEVIDEO** permissions.

Example: Unfavorite an image.

**How to Develop**

A
Annie_wang 已提交
139
1. [Obtain the images and videos in **Favorites**](#obtaining-images-and-videos-in-favorites).
A
Annie_wang 已提交
140
2. Set **favoriteState** to **false**.
A
Annie_wang 已提交
141
3. Call **FileAsset.setFavorite** to unfavorite an image or video.
A
Annie_wang 已提交
142 143 144 145


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

N
nwx1279094 已提交
148 149
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
150 151 152 153 154
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
155 156
  let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE);
  let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
A
Annie_wang 已提交
157 158
  console.info('get favorite Album successfully, albumUri: ' + album.albumUri);

N
nwx1279094 已提交
159 160
  let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getFirstObject();
A
Annie_wang 已提交
161 162 163 164 165 166 167 168 169 170
  console.info('favorite album getAssets successfully, albumName: ' + fileAsset.displayName);
  let favoriteState = false;
  await fileAsset.setFavorite(favoriteState);
  photoFetchResult.close();
  albumFetchResult.close();
} catch (err) {
  console.error('setFavorite failed with err: ' + err);
}
```

A
Annie_wang 已提交
171
## Videos
A
Annie_wang 已提交
172

A
Annie_wang 已提交
173
**Videos** is a system album that holds media assets of the video type in user files.
A
Annie_wang 已提交
174 175 176 177 178 179 180

### Obtaining a Video Album Object

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

**Prerequisites**

A
Annie_wang 已提交
181
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
182 183 184 185 186 187 188 189
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.

**How to Develop**

1. Set the album type to **photoAccessHelper.AlbumType.SYSTEM** and the album subtype to **photoAccessHelper.AlbumSubtype.VIDEO**.
2. Use **getAlbums** to obtain the video album object.

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

A
Annie_wang 已提交
192
try {
N
nwx1279094 已提交
193 194
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
  let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
A
Annie_wang 已提交
195 196 197 198 199 200 201
  console.info('get video Album successfully, albumUri: ' + album.albumUri);
  fetchResult.close();
} catch (err) {
  console.error('get video Album failed with err: ' + err);
}
```

A
Annie_wang 已提交
202
### Obtaining a Video from Videos
A
Annie_wang 已提交
203

A
Annie_wang 已提交
204
[Obtain a video album object](#obtaining-a-video-album-object), and use [Album.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets-2) to obtain video assets in **Videos**.
A
Annie_wang 已提交
205 206 207

**Prerequisites**

A
Annie_wang 已提交
208
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
209 210
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.

A
Annie_wang 已提交
211
Example: Obtain a video in **Videos**.
A
Annie_wang 已提交
212 213 214 215 216 217 218 219 220 221

**How to Develop**

1. [Obtain a video album object](#obtaining-a-video-album-object).
2. Set **fetchOptions** for obtaining the video.
3. Call **Album.getAssets** to obtain video assets.
4. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first video.

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

N
nwx1279094 已提交
224 225
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
226 227 228 229 230
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
231 232
  let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO);
  let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
A
Annie_wang 已提交
233 234
  console.info('get video Album successfully, albumUri: ' + album.albumUri);

N
nwx1279094 已提交
235 236
  let videoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await videoFetchResult.getFirstObject();
A
Annie_wang 已提交
237 238 239 240 241 242 243 244
  console.info('video album getAssets successfully, albumName: ' + fileAsset.displayName);
  videoFetchResult.close();
  albumFetchResult.close();
} catch (err) {
  console.error('video failed with err: ' + err);
}
```

A
Annie_wang 已提交
245
## Screenshots
A
Annie_wang 已提交
246

A
Annie_wang 已提交
247
**Screenshots** is a system album that holds user's screenshots and screen recording files.
A
Annie_wang 已提交
248 249 250

### Obtaining a Screenshot Album Object

A
Annie_wang 已提交
251
Use [getAlbums](../reference/apis/js-apis-photoAccessHelper.md#getalbums) to obtain a screenshot album object.
A
Annie_wang 已提交
252 253 254

**Prerequisites**

A
Annie_wang 已提交
255
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
256 257 258 259 260 261 262 263
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.

**How to Develop**

1. Set the album type to **photoAccessHelper.AlbumType.SYSTEM** and the album subtype to **photoAccessHelper.AlbumSubtype.SCREENSHOT**.
2. Use **getAlbums** to obtain a screenshot album object.

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

A
Annie_wang 已提交
266
try {
N
nwx1279094 已提交
267 268
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
  let album: photoAccessHelper.Album = await fetchResult.getFirstObject();
A
Annie_wang 已提交
269 270 271 272 273 274 275
  console.info('get screenshot Album successfully, albumUri: ' + album.albumUri);
  fetchResult.close();
} catch (err) {
  console.error('get screenshot Album failed with err: ' + err);
}
```

A
Annie_wang 已提交
276
### Obtaining Media Assets in Screenshots
A
Annie_wang 已提交
277 278 279 280 281

[Obtain a screenshot album object](#obtaining-a-screenshot-album-object), and call [Album.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets-2) to obtain the media assets in the album.

**Prerequisites**

A
Annie_wang 已提交
282
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
283 284
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.

A
Annie_wang 已提交
285
Example: Obtain a media asset from **Screenshots**.
A
Annie_wang 已提交
286 287 288 289 290 291 292 293 294 295

**How to Develop**

1. [Obtain a screenshot album object](#obtaining-a-screenshot-album-object).
2. Set **fetchOptions** for obtaining the media asset.
3. Call **Album.getAssets** to obtain media assets.
4. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first media asset.

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

N
nwx1279094 已提交
298 299
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
300 301 302 303 304
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
305 306
  let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.SCREENSHOT);
  let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject();
A
Annie_wang 已提交
307 308
  console.info('get screenshot album successfully, albumUri: ' + album.albumUri);

N
nwx1279094 已提交
309 310
  let screenshotFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await screenshotFetchResult.getFirstObject();
A
Annie_wang 已提交
311 312 313 314 315 316 317
  console.info('screenshot album getAssets successfully, albumName: ' + fileAsset.displayName);
  screenshotFetchResult.close();
  albumFetchResult.close();
} catch (err) {
  console.error('screenshot album failed with err: ' + err);
}
```