• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Managing Media Assets
2<!--Kit: Media Library Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @yixiaoff-->
5<!--SE: @liweilu1-->
6<!--TSE: @xchaosioda-->
7
8Applications can call photoAccessHelper APIs to manage media assets (images and videos).
9
10> **NOTE**
11>
12> - Before you get started, obtain a PhotoAccessHelper instance and apply for required permissions. For details, see [Before You Start](photoAccessHelper-preparation.md).
13>
14> - Unless otherwise specified, the PhotoAccessHelper instance obtained in [Before You Start](photoAccessHelper-preparation.md) 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.
15
16To 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 [Module Description](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper.md).
17
18## Obtaining Media Assets
19
20You can obtain media assets based on the specified conditions, such as the media type, date, or album name.
21
22Use [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#getassets-1) with the [FetchOptions](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-i.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, check whether the media assets exist in the database.
23
24To 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/arkts-apis-photoAccessHelper-FetchResult.md).
25
26**Prerequisites**
27
28- A PhotoAccessHelper instance is obtained.
29- The application has the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
30- The [dataSharePredicates](../../reference/apis-arkdata/js-apis-data-dataSharePredicates.md) module is imported.
31
32### Obtaining an Image or Video by Name
33
34Example: Obtain the image **test.jpg**.
35
36```ts
37import { dataSharePredicates } from '@kit.ArkData';
38import { photoAccessHelper } from '@kit.MediaLibraryKit';
39
40async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
41  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
42  predicates.equalTo(photoAccessHelper.PhotoKeys.DISPLAY_NAME, 'test.jpg');
43  let fetchOptions: photoAccessHelper.FetchOptions = {
44    fetchColumns: [],
45    predicates: predicates
46  };
47  try {
48    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
49    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
50    console.info('getAssets photoAsset.displayName : ' + photoAsset.displayName);
51    fetchResult.close();
52  } catch (err) {
53    console.error('getAssets failed with err: ' + err);
54  }
55}
56```
57
58## Obtaining an Image or Video Thumbnail
59
60To display images and videos in Gallery or to preview edits, applications must obtain image and video thumbnails.
61
62The thumbnails offer a quick preview on images and videos. You can use [PhotoAsset.getThumbnail](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAsset.md#getthumbnail-2) with the thumbnail size specified to obtain the image or video thumbnail.
63
64**Prerequisites**
65
66- A PhotoAccessHelper instance is obtained.
67- The application has the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
68- The [dataSharePredicates](../../reference/apis-arkdata/js-apis-data-dataSharePredicates.md) module is imported.
69
70For example, obtain the file descriptor (FD) of an image, and decode the image into a PixelMap for display or processing. For details, see [Image Decoding](../image/image-decoding.md).
71
72Example: Obtain the thumbnail at the size of 720 x 720 of an image.
73
74**How to Develop**
75
761. Set the fetch options.
772. Call [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#getassets-1) to obtain image assets.
783. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first image from the result set.
794. Call **PhotoAsset.getThumbnail** to obtain the [PixelMap](../../reference/apis-image-kit/arkts-apis-image-PixelMap.md) of the thumbnail of the image.
80
81```ts
82import { dataSharePredicates } from '@kit.ArkData';
83import { image } from '@kit.ImageKit';
84import { photoAccessHelper } from '@kit.MediaLibraryKit';
85
86async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
87  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
88  let fetchOptions: photoAccessHelper.FetchOptions = {
89    fetchColumns: [],
90    predicates: predicates
91  };
92
93  try {
94    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
95    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
96    console.info('getAssets photoAsset.displayName : ' + photoAsset.displayName);
97    let size: image.Size = { width: 720, height: 720 };
98    let pixelMap: image.PixelMap =  await photoAsset.getThumbnail(size);
99    let imageInfo: image.ImageInfo = await pixelMap.getImageInfo()
100    console.info('getThumbnail successful, pixelMap ImageInfo size: ' + JSON.stringify(imageInfo.size));
101    fetchResult.close();
102  } catch (err) {
103    console.error('getThumbnail failed with err: ' + err);
104  }
105}
106```
107
108<!--Del-->
109## Creating a Media Asset
110
111Use [MediaAssetChangeRequest](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md) to create a media asset change request object for a media asset, and use [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to apply the changes.
112
113**Prerequisites**
114
115- A PhotoAccessHelper instance is obtained.
116- The application has the ohos.permission.WRITE_IMAGEVIDEO permission. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
117
118### Creating an Image or Video Asset (for System Applications Only)
119
120Example: Create an image asset.
121
122**How to Develop**
123
1241. Set the file name and **createOption** for setting attributes for the image asset to create.
1252. Call **MediaAssetChangeRequest.createAssetRequest** to create a media asset change request object.
1263. Call **MediaAssetChangeRequest.getWriteCacheHandler** to obtain the handle of the file to write and write the content of the image asset.
1274. Call **PhotoAccessHelper.applyChanges** to apply the changes to the image.
128
129```ts
130import { photoAccessHelper } from '@kit.MediaLibraryKit';
131import { fileIo } from '@kit.CoreFileKit';
132
133async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) {
134  try {
135    let displayName: string = 'testPhoto' + Date.now() + '.jpg';
136    let createOption: photoAccessHelper.PhotoCreateOptions = {
137      subtype: photoAccessHelper.PhotoSubtype.DEFAULT
138    };
139    let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = photoAccessHelper.MediaAssetChangeRequest.createAssetRequest(context, displayName, createOption);
140    let fd: number = await assetChangeRequest.getWriteCacheHandler();
141    // write date into fd.
142    await fileIo.close(fd);
143    await phAccessHelper.applyChanges(assetChangeRequest);
144  } catch (err) {
145    console.error(`create asset failed with error: ${err.code}, ${err.message}`);
146  }
147}
148```
149
150You can also use **MediaAssetChangeRequest.addResource** to specify the data source information of the media asset, including the [application sandbox](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#addresource11), [ArrayBuffer](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#addresource11-1), and [PhotoProxy](../../reference/apis-media-library-kit/js-apis-photoAccessHelper-sys.md#addresource11).
151<!--DelEnd-->
152
153## Renaming a Media Asset
154
155To rename a media asset, change the **PhotoAsset.displayName** attribute, that is, the file name (including the file name extension) displayed.
156
157Use [FetchResult](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md) to obtain the file to rename, use [MediaAssetChangeRequest.setTitle](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#settitle11) to set the new name, and then use [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to apply the changes to the database.
158
159**Prerequisites**
160
161- A PhotoAccessHelper instance is obtained.
162- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
163
164Example: Rename the first image in the obtained image assets.
165
166**How to Develop**
167
1681. Set the fetch options.
1692. Call [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#getassets-1) to obtain image assets.
1703. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first image from the obtained file assets.
1714. Call [MediaAssetChangeRequest.setTitle](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#settitle11) to rename the image.
1725. Call [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to save the modification to the database.
173
174```ts
175import { dataSharePredicates } from '@kit.ArkData';
176import { photoAccessHelper } from '@kit.MediaLibraryKit';
177
178async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
179  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
180  let fetchOptions: photoAccessHelper.FetchOptions = {
181    fetchColumns: ['title'],
182    predicates: predicates
183  };
184  let newTitle: string = 'newTestPhoto';
185
186  try {
187    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
188    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
189    let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = new photoAccessHelper.MediaAssetChangeRequest(photoAsset);
190    assetChangeRequest.setTitle(newTitle);
191    await phAccessHelper.applyChanges(assetChangeRequest);
192    fetchResult.close();
193  } catch (err) {
194    console.error(`rename failed with error: ${err.code}, ${err.message}`);
195  }
196}
197```
198
199## Moving a Media Asset to the Trash
200
201You can use [MediaAssetChangeRequest.deleteAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#deleteassets11) to move files to the trash.
202
203The 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 **Files** or **Gallery**.
204
205**Prerequisites**
206
207- A PhotoAccessHelper instance is obtained.
208- The application has the ohos.permission.READ_IMAGEVIDEO and ohos.permission.WRITE_IMAGEVIDEO permissions. For details, see [Requesting Permissions](photoAccessHelper-preparation.md#requesting-permissions).
209
210Example: Move the first file in the result set to the trash.
211
212**How to Develop**
213
2141. Set the fetch options.
2152. Call [PhotoAccessHelper.getAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#getassets-1) to obtain image assets.
2163. Call [FetchResult.getFirstObject](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-FetchResult.md#getfirstobject-1) to obtain the first image.
2174. Call [MediaAssetChangeRequest.deleteAssets](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#deleteassets11) to move the image to the trash.
218
219```ts
220import { dataSharePredicates } from '@kit.ArkData';
221import { photoAccessHelper } from '@kit.MediaLibraryKit';
222
223async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) {
224  let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
225  let fetchOptions: photoAccessHelper.FetchOptions = {
226    fetchColumns: [],
227    predicates: predicates
228  };
229
230  try {
231    let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions);
232    let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
233    await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]);
234    fetchResult.close();
235  } catch (err) {
236    console.error(`deleteAssets failed with error: ${err.code}, ${err.message}`);
237  }
238}
239```
240