1e41f4b71Sopenharmony_ci# Managing Media Assets
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciApplications can call **photoAccessHelper** APIs to manage media assets (images and videos).
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> - Before you get started, obtain a **PhotoAccessHelper** instance and apply for required permissions. For details, see [Before You Start](photoAccessHelper-preparation.md).
8e41f4b71Sopenharmony_ci> - Unless otherwise specified, the **PhotoAccessHelper** instance obtained in the **Before You Start** section is used to call **photoAccessHelper** APIs. If the code for obtaining the **PhotoAccessHelper** instance is missing, an error will be reported to indicate that **photoAccessHelper** is not defined.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ciTo ensure application running efficiency, most **PhotoAccessHelper** APIs are asynchronously implemented in callback or promise mode. The following examples use promise-based APIs. For details about the APIs, see [Album Management](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md).
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## Obtaining Media Assets
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ciYou can obtain media assets based on the specified conditions, such as the media type, date, or album name.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciUse [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getassets-1) with the [FetchOptions](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#fetchoptions) object to specify the 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.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ciTo obtain the object at the specified position (for example, the first one, the last one, or the one with the specified index) in the result set, use [FetchResult](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#fetchresult).
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci**Prerequisites**
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
23e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
24e41f4b71Sopenharmony_ci- The [dataSharePredicates](../../reference/apis-arkdata/js-apis-data-dataSharePredicates.md) module is imported.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci### Obtaining an Image or Video by Name
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciExample: Obtain the image **test.jpg**.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci```ts
31e41f4b71Sopenharmony_ciimport dataSharePredicates from '@ohos.data.dataSharePredicates';
32e41f4b71Sopenharmony_ciimport photoAccessHelper from '@ohos.file.photoAccessHelper';
33e41f4b71Sopenharmony_ciconst context = getContext(this);
34e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ciasync function example() {
37e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
38e41f4b71Sopenharmony_ci  predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
39e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
40e41f4b71Sopenharmony_ci    fetchColumns: [],
41e41f4b71Sopenharmony_ci    predicates: predicates
42e41f4b71Sopenharmony_ci  };
43e41f4b71Sopenharmony_ci  try {
44e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
45e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
46e41f4b71Sopenharmony_ci    console.info('getAssets photoAsset.displayName : ' + photoAsset.displayName);
47e41f4b71Sopenharmony_ci    fetchResult.close();
48e41f4b71Sopenharmony_ci  } catch (err) {
49e41f4b71Sopenharmony_ci    console.error('getAssets failed with err: ' + err);
50e41f4b71Sopenharmony_ci  }
51e41f4b71Sopenharmony_ci}
52e41f4b71Sopenharmony_ci```
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci## Obtaining an Image or Video Thumbnail
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ciThe thumbnails offer a quick preview on images and videos. You can use [PhotoAsset.getThumbnail](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getthumbnail-2)  with the thumbnail size specified to obtain the image or video thumbnail.
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci**Prerequisites**
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
61e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
62e41f4b71Sopenharmony_ci- The [dataSharePredicates](../../reference/apis-arkdata/js-apis-data-dataSharePredicates.md) module is imported.
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci### Obtaining the Thumbnail of an Image
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciYour application may need to obtain the thumbnail of an image or video for preview purposes.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciFor example, obtain the file descriptor (FD) of an image, and decode the image into a pixel map for display or processing. For details, see [Image Decoding](../image/image-decoding.md).
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ciExample: Obtain the thumbnail at the size of 720 x 720 of an image.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**How to Develop**
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci1. Set the fetch options.
75e41f4b71Sopenharmony_ci2. Call [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getassets-1) to obtain image assets.
76e41f4b71Sopenharmony_ci3. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getfirstobject-1) to obtain the first image from the result set.
77e41f4b71Sopenharmony_ci4. Call **PhotoAsset.getThumbnail** to obtain the [PixelMap](../../reference/apis-image-kit/js-apis-image.md#pixelmap7) of the thumbnail of the image.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci```ts
80e41f4b71Sopenharmony_ciimport dataSharePredicates from '@ohos.data.dataSharePredicates';
81e41f4b71Sopenharmony_ciimport image from '@ohos.multimedia.image';
82e41f4b71Sopenharmony_ciimport photoAccessHelper from '@ohos.file.photoAccessHelper';
83e41f4b71Sopenharmony_ciconst context = getContext(this);
84e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ciasync function example() {
87e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
88e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
89e41f4b71Sopenharmony_ci    fetchColumns: [],
90e41f4b71Sopenharmony_ci    predicates: predicates
91e41f4b71Sopenharmony_ci  };
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci  try {
94e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
95e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
96e41f4b71Sopenharmony_ci    console.info('getAssets photoAsset.displayName : ' + photoAsset.displayName);
97e41f4b71Sopenharmony_ci    let size: image.Size = { width: 720, height: 720 };
98e41f4b71Sopenharmony_ci    let pixelMap: image.PixelMap =  await photoAsset.getThumbnail(size);
99e41f4b71Sopenharmony_ci    let imageInfo: image.ImageInfo = await pixelMap.getImageInfo()
100e41f4b71Sopenharmony_ci    console.info('getThumbnail successful, pixelMap ImageInfo size: ' + JSON.stringify(imageInfo.size));
101e41f4b71Sopenharmony_ci    fetchResult.close();
102e41f4b71Sopenharmony_ci  } catch (err) {
103e41f4b71Sopenharmony_ci    console.error('getThumbnail failed with err: ' + err);
104e41f4b71Sopenharmony_ci  }
105e41f4b71Sopenharmony_ci}
106e41f4b71Sopenharmony_ci```
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci<!--Del-->
109e41f4b71Sopenharmony_ci## Creating a Media Asset
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ciUse [MediaAssetChangeRequest](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#mediaassetchangerequest11) to create a media asset change request object for a media asset, and use [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#applychanges11) to apply the changes.
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**Prerequisites**
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
116e41f4b71Sopenharmony_ci- The application has the ohos.permission.WRITE_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci### Creating an Image or Video Asset (for System Applications Only)
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ciExample: Create an image asset.
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**How to Develop**
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci1. Set the file name and create **createOption** for setting attributes for the image asset to create.
125e41f4b71Sopenharmony_ci2. Call **MediaAssetChangeRequest.createAssetRequest** to create a media asset change request object.
126e41f4b71Sopenharmony_ci3. Call **MediaAssetChangeRequest.getWriteCacheHandler** to obtain the handle of the file to write and write the content of the image asset.
127e41f4b71Sopenharmony_ci4. Call **PhotoAccessHelper.applyChanges** to apply the changes to the image.
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci```ts
130e41f4b71Sopenharmony_ciimport photoAccessHelper from '@ohos.file.photoAccessHelper';
131e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs';
132e41f4b71Sopenharmony_cilet context = getContext(this);
133e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ciasync function example() {
136e41f4b71Sopenharmony_ci  try {
137e41f4b71Sopenharmony_ci    let displayName: string = 'testPhoto' + Date.now() + '.jpg';
138e41f4b71Sopenharmony_ci    let createOption: photoAccessHelper.PhotoCreateOptions = {
139e41f4b71Sopenharmony_ci      subtype: photoAccessHelper.PhotoSubtype.DEFAULT
140e41f4b71Sopenharmony_ci    };
141e41f4b71Sopenharmony_ci    let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = photoAccessHelper.MediaAssetChangeRequest.createAssetRequest(context, displayName, createOption);
142e41f4b71Sopenharmony_ci    let fd: number = await assetChangeRequest.getWriteCacheHandler();
143e41f4b71Sopenharmony_ci    // write date into fd
144e41f4b71Sopenharmony_ci    await fs.close(fd);
145e41f4b71Sopenharmony_ci    await phAccessHelper.applyChanges(assetChangeRequest);
146e41f4b71Sopenharmony_ci  } catch (err) {
147e41f4b71Sopenharmony_ci    console.error(`create asset failed with error: ${err.code}, ${err.message}`);
148e41f4b71Sopenharmony_ci  }
149e41f4b71Sopenharmony_ci}
150e41f4b71Sopenharmony_ci```
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciYou can also use **MediaAssetChangeRequest.addResource** to specify the data source information of the media asset, including the [application sandbox](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#addresource11), [ArrayBuffer](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#addresource11-1), and [PhotoProxy](../../reference/apis-media-library-kit/js-apis-photoAccessHelper-sys.md#addresource11).
153e41f4b71Sopenharmony_ci<!--DelEnd-->
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci## Renaming a Media Asset
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ciTo rename a media asset, change the **PhotoAsset.displayName** attribute, that is, the file name (including the file name extension) displayed.
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ciObtain the media asset using [FetchResult](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#fetchresult) APIs, use [MediaAssetChangeRequest.setTitle](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#settitle11) to rename the file, and use use [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#applychanges11) to apply the changes to the database.
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci**Prerequisites**
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
164e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ciExample: Rename the first image in the obtained image assets.
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci**How to Develop**
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci1. Set the fetch options.
171e41f4b71Sopenharmony_ci2. Call [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getassets-1) to obtain image assets.
172e41f4b71Sopenharmony_ci3. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getfirstobject-1) to obtain the first image from the obtained file assets.
173e41f4b71Sopenharmony_ci4. Call [MediaAssetChangeRequest.setTitle](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#settitle11) to rename the image.
174e41f4b71Sopenharmony_ci5. Call [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#applychanges11) to save the modification to the database.
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci```ts
177e41f4b71Sopenharmony_ciimport dataSharePredicates from '@ohos.data.dataSharePredicates';
178e41f4b71Sopenharmony_ciimport photoAccessHelper from '@ohos.file.photoAccessHelper';
179e41f4b71Sopenharmony_cilet context = getContext(this);
180e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ciasync function example() {
183e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
184e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
185e41f4b71Sopenharmony_ci    fetchColumns: ['title'],
186e41f4b71Sopenharmony_ci    predicates: predicates
187e41f4b71Sopenharmony_ci  };
188e41f4b71Sopenharmony_ci  let newTitle: string = 'newTestPhoto';
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci  try {
191e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
192e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
193e41f4b71Sopenharmony_ci    let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset);
194e41f4b71Sopenharmony_ci    assetChangeRequest.setTitle(newTitle);
195e41f4b71Sopenharmony_ci    await phAccessHelper.applyChanges(assetChangeRequest);
196e41f4b71Sopenharmony_ci    fetchResult.close();
197e41f4b71Sopenharmony_ci  } catch (err) {
198e41f4b71Sopenharmony_ci    console.error(`rename failed with error: ${err.code}, ${err.message}`);
199e41f4b71Sopenharmony_ci  }
200e41f4b71Sopenharmony_ci}
201e41f4b71Sopenharmony_ci```
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci## Moving a Media Asset to the Trash
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ciYou can use [MediaAssetChangeRequest.deleteAssets](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#deleteassets11) to move files to the trash.
206e41f4b71Sopenharmony_ci
207e41f4b71Sopenharmony_ciThe file moved to the trash will be retained for 30 days before being deleted permanently. Before a file is deleted permanently from the trash, the user can restore it using the system application **FileManager** or **Gallery**.
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci**Prerequisites**
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci- A **PhotoAccessHelper** instance is obtained.
212e41f4b71Sopenharmony_ci- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ciExample: Move the first image in the result set to the trash.
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci**How to Develop**
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci1. Set the fetch options.
219e41f4b71Sopenharmony_ci2. Call [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getassets-1) to obtain image assets.
220e41f4b71Sopenharmony_ci3. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#getfirstobject-1) to obtain the first image.
221e41f4b71Sopenharmony_ci4. Call [MediaAssetChangeRequest.deleteAssets](../../reference/apis-media-library-kit/js-apis-photoAccessHelper.md#deleteassets11) to move the image to the trash.
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci```ts
224e41f4b71Sopenharmony_ciimport dataSharePredicates from '@ohos.data.dataSharePredicates';
225e41f4b71Sopenharmony_ciimport photoAccessHelper from '@ohos.file.photoAccessHelper';
226e41f4b71Sopenharmony_cilet context = getContext(this);
227e41f4b71Sopenharmony_cilet phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ciasync function example() {
230e41f4b71Sopenharmony_ci  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
231e41f4b71Sopenharmony_ci  let fetchOptions: photoAccessHelper.FetchOptions = {
232e41f4b71Sopenharmony_ci    fetchColumns: [],
233e41f4b71Sopenharmony_ci    predicates: predicates
234e41f4b71Sopenharmony_ci  };
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ci  try {
237e41f4b71Sopenharmony_ci    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
238e41f4b71Sopenharmony_ci    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
239e41f4b71Sopenharmony_ci    await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]);
240e41f4b71Sopenharmony_ci    fetchResult.close();
241e41f4b71Sopenharmony_ci  } catch (err) {
242e41f4b71Sopenharmony_ci    console.error(`deleteAssets failed with error: ${err.code}, ${err.message}`);
243e41f4b71Sopenharmony_ci  }
244e41f4b71Sopenharmony_ci}
245e41f4b71Sopenharmony_ci```
246