photoAccessHelper-resource-guidelines.md 10.7 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 36 37 38 39 40 41 42 43 44

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
let fetchOptions = {
  fetchColumns: [],
  predicates: predicates
};
```

A
Annie_wang 已提交
45
2. Call **PhotoAccessHelper.getAssets** to obtain image assets.
A
Annie_wang 已提交
46 47 48 49 50 51 52 53 54 55 56 57

```ts
try {
  let fetchResult = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset = await fetchResult.getFirstObject();
  console.info('getAssets fileAsset.displayName : ' + fileAsset.displayName);
  fetchResult.close();
} catch (err) {
  console.error('getAssets failed with err: ' + err);
}
```

A
Annie_wang 已提交
58
### Obtaining an Image or Video by URI
A
Annie_wang 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72

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

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.PhotoKeys.URI, 'file://media/Photo/1');
let fetchOptions = {
  fetchColumns: [],
  predicates: predicates
};
```

A
Annie_wang 已提交
73
Call **PhotoAccessHelper.getAssets** to obtain image assets.
A
Annie_wang 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

```ts
try {
  let fetchResult = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset = await fetchResult.getFirstObject();
  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 已提交
89
Example: Obtain the media assets added between 2022-06-01 and 2023-06-01.
A
Annie_wang 已提交
90 91 92 93 94 95 96 97 98

```ts
import dataSharePredicates from '@ohos.data.dataSharePredicates';

let predicates = new dataSharePredicates.DataSharePredicates();
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.
let date_added = photoAccessHelper.PhotoKeys.DATE_ADDED;
predicates.between(date_added, startTime, endTime);
A
Annie_wang 已提交
99
predicates.orderByDesc(date_added); // Sort the obtained records in descending order.
A
Annie_wang 已提交
100 101 102 103 104 105
let fetchOptions = {
  fetchColumns: [date_added], // The date_added attribute is not a default option and needs to be added.
  predicates: predicates
};
```

A
Annie_wang 已提交
106
Call **PhotoAccessHelper.getAssets** to obtain image assets.
A
Annie_wang 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

```ts
try {
  let fetchResult = await phAccessHelper.getAssets(fetchOptions);
  console.info('getAssets count: ' + fetchResult.getCount());
  let fileAsset = await fetchResult.getFirstObject();
  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 已提交
126
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
- 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';

let predicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions = {
  fetchColumns: [],
  predicates: predicates
};

try {
  let fetchResult = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset = await fetchResult.getFirstObject();
  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 已提交
172
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
173 174
- The application has the **ohos.permission.WRITE_IMAGEVIDEO** permission.

A
Annie_wang 已提交
175
### Creating an Image or Video
A
Annie_wang 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

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
try {
  let displayName = 'testPhoto' + Date.now() + '.jpg';
  let createOption = {
    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 已提交
200 201
Before renaming a file, use [FetchResult](../reference/apis/js-apis-photoAccessHelper.md#fetchresult) to obtain the file.

A
Annie_wang 已提交
202 203 204 205 206 207
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 已提交
208
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
- 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';

let predicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions = {
  fetchColumns: ['title'],
  predicates: predicates
};
let newTitle = 'newTestPhoto';

try {
  let fetchResult = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset = await fetchResult.getFirstObject();
  let title = photoAccessHelper.PhotoKeys.TITLE;
  let fileAssetTitle = fileAsset.get(title);
  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 已提交
249
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 已提交
250 251 252

**Prerequisites**

A
Annie_wang 已提交
253
- A **PhotoAccessHelper** instance is obtained.
A
Annie_wang 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
- 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';

let predicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions = {
  fetchColumns: [],
  predicates: predicates
};

try {
  let fetchResult = await phAccessHelper.getAssets(fetchOptions);
  let fileAsset = await fetchResult.getFirstObject();
  console.info('getAssets fileAsset.uri : ' + fileAsset.uri);
  await phAccessHelper.deleteAssets([fileAsset.uri]);
  fetchResult.close();
} catch (err) {
  console.error('deleteAssets failed with err: ' + err);
}
```