photoAccessHelper-resource-guidelines.md 12.4 KB
Newer Older
A
Annie_wang 已提交
1
# Managing Media Assets
A
Annie_wang 已提交
2 3 4 5 6

Applications can call **photoAccessHelper** APIs to manage media assets (images and videos).

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

## Obtaining the Specified Media Assets

A
Annie_wang 已提交
14
You can obtain media assets by media type, date, or album name.
A
Annie_wang 已提交
15 16 17 18 19 20 21

Use [PhotoAccessHelper.getAssets](../reference/apis/js-apis-photoAccessHelper.md#getassets) with the [FetchOptions](../reference/apis/js-apis-photoAccessHelper.md#fetchoptions) object to specify search criteria. 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.

To obtain the object at the specified position (for example, the first, the last, or object with the specified index) in the result set, use [FetchFileResult](../reference/apis/js-apis-photoAccessHelper.md#fetchresult).

**Prerequisites**

A
Annie_wang 已提交
22
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
23 24 25 26 27 28 29 30 31
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.
- The [dataSharePredicates](../reference/apis/js-apis-data-dataSharePredicates.md) module is imported.

### Obtaining an Image or Video by Name

Example: Obtain the image **test.jpg**.

**How to Develop**

A
Annie_wang 已提交
32
1. Create a **FetchOptions** object and specify **test.jpg**.
A
Annie_wang 已提交
33 34 35

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

N
nwx1279094 已提交
38
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
A
Annie_wang 已提交
39
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
N
nwx1279094 已提交
40
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
41 42 43 44 45
  fetchColumns: [],
  predicates: predicates
};
```

A
Annie_wang 已提交
46
2. Call **PhotoAccessHelper.getAssets** to obtain image assets.
A
Annie_wang 已提交
47 48

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

A
Annie_wang 已提交
51
try {
N
nwx1279094 已提交
52 53
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
A
Annie_wang 已提交
54 55 56 57 58 59 60
  console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
  fetchResult.close();
} catch (err) {
  console.error('getAssets failed with err: ' + err);
}
```

A
Annie_wang 已提交
61
### Obtaining an Image or Video by URI
A
Annie_wang 已提交
62 63 64 65 66

Example: Obtain the image with the file URI **file://media/Photo/1**.

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

N
nwx1279094 已提交
69
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
A
Annie_wang 已提交
70
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, 'file://media/Photo/1');
N
nwx1279094 已提交
71
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
72 73 74 75 76
  fetchColumns: [],
  predicates: predicates
};
```

A
Annie_wang 已提交
77
Call **PhotoAccessHelper.getAssets** to obtain image assets.
A
Annie_wang 已提交
78 79

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

A
Annie_wang 已提交
82
try {
N
nwx1279094 已提交
83 84
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
A
Annie_wang 已提交
85 86 87 88 89 90 91 92 93 94
  console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
  fetchResult.close();
} catch (err) {
  console.error('getAssets failed with err: ' + err);
}
```


### Obtaining Images or Videos by Time

A
Annie_wang 已提交
95
Example: Obtain the media assets added between 2022-06-01 and 2023-06-01.
A
Annie_wang 已提交
96 97 98

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

N
nwx1279094 已提交
101
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
A
Annie_wang 已提交
102 103
let startTime = Date.parse(new Date('2022-06-01').toString()) / 1000; // The value of the start time is the number of seconds elapsed since the Epoch time.
let endTime = Date.parse(new Date('2023-06-01').toString()) / 1000;  // The value of the end time is the number of seconds elapsed since the Epoch time.
N
nwx1279094 已提交
104
let date_added: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.DATE_ADDED;
A
Annie_wang 已提交
105
predicates.between(date_added, startTime, endTime);
A
Annie_wang 已提交
106
predicates.orderByDesc(date_added); // Sort the obtained records in descending order.
N
nwx1279094 已提交
107
let fetchOptions: photoAccessHelper.FetchOptions = {
A
Annie_wang 已提交
108 109 110 111 112
  fetchColumns: [date_added], // The date_added attribute is not a default option and needs to be added.
  predicates: predicates
};
```

A
Annie_wang 已提交
113
Call **PhotoAccessHelper.getAssets** to obtain image assets.
A
Annie_wang 已提交
114 115

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

A
Annie_wang 已提交
118
try {
N
nwx1279094 已提交
119
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
A
Annie_wang 已提交
120
  console.info('getAssets count: ' + fetchResult.getCount());
N
nwx1279094 已提交
121
  let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
A
Annie_wang 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134
  console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
  fetchResult.close();
} catch (err) {
  console.error('getAssets failed with err: ' + err);
}
```

## Obtaining an Image or Video Thumbnail

Use [FileAsset.getThumbnail](../reference/apis/js-apis-photoAccessHelper.md#getthumbnail) with the thumbnail size passed in to obtain the image or video thumbnail. The thumbnails offer a quick preview on images and videos.

**Prerequisites**

A
Annie_wang 已提交
135
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
- The application has the **ohos.permission.READ_IMAGEVIDEO** permission.
- The [dataSharePredicates](../reference/apis/js-apis-data-dataSharePredicates.md) module is imported.

### Obtaining the Thumbnail of an Image

Your application may need to obtain the thumbnail of an image or video for preview purposes.

Example: Obtain the thumbnail of 720 x 720 of an image.

**How to Develop**

1. Set the fetch options.
2. Call **PhotoAccessHelper.getAssets** to obtain image assets.
3. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first image from the result set.
4. Call **getThumbnail** to obtain the [PixelMap](../reference/apis/js-apis-image.md#pixelmap7) of the thumbnail of the image.

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

N
nwx1279094 已提交
156 157
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
158 159 160 161 162
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
163 164
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
A
Annie_wang 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
  let size = { width: 720, height: 720 };
  let pixelMap =  await fileAsset.getThumbnail(size);
  let imageInfo = await pixelMap.getImageInfo()
  console.info('getThumbnail successful, pixelMap ImageInfo size: ' + JSON.stringify(imageInfo.size));
  fetchResult.close();
} catch (err) {
  console.error('getThumbnail failed with err: ' + err);
}
```

## Creating a Media Asset

Use [createAsset](../reference/apis/js-apis-photoAccessHelper.md#createasset) to create a media asset.

**Prerequisites**

A
Annie_wang 已提交
182
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
183 184
- The application has the **ohos.permission.WRITE_IMAGEVIDEO** permission.

A
Annie_wang 已提交
185
### Creating an Image or Video
A
Annie_wang 已提交
186 187 188 189 190 191 192 193 194

Example: Create an image asset.

**How to Develop**

1. Set the file name and create **createOption** for setting attributes for the image asset to create.
2. Call **createAsset** to create an image asset.

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

A
Annie_wang 已提交
197 198
try {
  let displayName = 'testPhoto' + Date.now() + '.jpg';
N
nwx1279094 已提交
199
  let createOption: photoAccessHelper.CreateOptions = {
A
Annie_wang 已提交
200 201 202 203 204 205 206 207 208 209 210 211
    subType: photoAccessHelper.PhotoSubtype.DEFAULT
  };

  let fileAsset = await phAccessHelper.createAsset(displayName, createOption);
  console.info('createAsset successfully, file displayName: ' + fileAsset.displayName);
} catch (err) {
  console.error('createAsset failed, message = ', err);
}
```

## Renaming a Media Asset

A
Annie_wang 已提交
212 213
Before renaming a file, use [FetchResult](../reference/apis/js-apis-photoAccessHelper.md#fetchresult) to obtain the file.

A
Annie_wang 已提交
214 215 216 217 218 219
Set the **FileAsset.displayName** attribute to modify the file name (including the file name extension) displayed.

After the modification, use [FileAsset.commitModify](../reference/apis/js-apis-photoAccessHelper.md#commitmodify) to update the modification to the database.

**Prerequisites**

A
Annie_wang 已提交
220
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234
- The application has the **ohos.permission.WRITE_IMAGEVIDEO** and **ohos.permission.READ_IMAGEVIDEO** permissions.

Example: Rename the first file in the obtained image assets.

**How to Develop**

1. Set the fetch options.
2. Call **getAssets** to obtain image assets.
3. Call [FetchResult.getFirstObject](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first image from the obtained file assets.
4. Call **FileAsset.set** to rename the image.
5. Call **FileAsset.commitModify** to update the modified image attributes to the database.

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

N
nwx1279094 已提交
237 238
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
239 240 241 242 243 244
  fetchColumns: ['title'],
  predicates: predicates
};
let newTitle = 'newTestPhoto';

try {
N
nwx1279094 已提交
245 246 247 248
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
  let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE;
  let fileAssetTitle: photoAccessHelper.MemberType = fileAsset.get(title);
A
Annie_wang 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261
  console.info('getAssets fileAsset.title : ' + fileAssetTitle);
  fileAsset.set(title, newTitle);
  await fileAsset.commitModify();
  fetchResult.close();
} catch (err) {
  console.error('commitModify failed with err: ' + err);
}
```

## Moving Media Assets to the Trash

You can use [deleteAssets](../reference/apis/js-apis-photoAccessHelper.md#deleteassets) to move files to the trash.

A
Annie_wang 已提交
262
The files moved to the trash will be retained for 30 days, and deleted permanently after 30 days. Before a file is deleted permanently from the trash, the user can restore it using the system application **FileManager** or **Gallery**.
A
Annie_wang 已提交
263 264 265

**Prerequisites**

A
Annie_wang 已提交
266
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279
- The application has the **ohos.permission.WRITE_IMAGEVIDEO** and **ohos.permission.READ_IMAGEVIDEO** permissions.

Example: Move the first file in the result set to the trash.

**How to Develop**

1. Set the fetch options.
2. Call **PhotoAccessHelper.getAssets** to obtain image assets.
3. Call [**FetchResult.getFirstObject**](../reference/apis/js-apis-photoAccessHelper.md#getfirstobject) to obtain the first image, that is, the image object to be moved to the trash.
4. Call **deleteAssets** to move the file to the trash.

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

N
nwx1279094 已提交
282 283
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions: dataSharePredicates.FetchOptions = {
A
Annie_wang 已提交
284 285 286 287 288
  fetchColumns: [],
  predicates: predicates
};

try {
N
nwx1279094 已提交
289 290
  let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
A
Annie_wang 已提交
291 292 293 294 295 296 297
  console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
  await phAccessHelper.deleteAssets([fileAsset.uri]);
  fetchResult.close();
} catch (err) {
  console.error('deleteAssets failed with err: ' + err);
}
```