1# Interface (MovingPhoto) 2<!--Kit: Media Library Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @yixiaoff--> 5<!--SE: @liweilu1--> 6<!--TSE: @xchaosioda--> 7 8> **NOTE** 9> 10> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 11> - The initial APIs of this interface are supported since API version 12. 12 13MovingPhoto provides APIs for managing a moving photo instance. 14 15**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 16 17## Modules to Import 18 19```ts 20import { photoAccessHelper } from '@kit.MediaLibraryKit'; 21``` 22 23## getUri<sup>12+</sup> 24 25getUri(): string 26 27Obtains the URI of this moving photo. 28 29**Atomic service API**: This API can be used in atomic services since API version 12. 30 31**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 32 33**Return value** 34 35| Type | Description | 36| --------------------------------------- | ----------------- | 37| string | URI of the moving photo obtained.| 38 39**Error codes** 40 41For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). 42 43| ID| Error Message| 44| -------- | ---------------------------------------- | 45| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 46| 14000011 | System inner fail. | 47 48**Example** 49 50For details about how to create a phAccessHelper instance, see the example provided in [photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper). 51 52```ts 53import { dataSharePredicates } from '@kit.ArkData'; 54 55class MovingPhotoHandler implements photoAccessHelper.MediaAssetDataHandler<photoAccessHelper.MovingPhoto> { 56 async onDataPrepared(movingPhoto: photoAccessHelper.MovingPhoto) { 57 if (movingPhoto === undefined) { 58 console.error('Error occurred when preparing data'); 59 return; 60 } 61 console.info("moving photo acquired successfully, uri: " + movingPhoto.getUri()); 62 } 63} 64 65async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 66 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 67 predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_SUBTYPE, photoAccessHelper.PhotoSubtype.MOVING_PHOTO); 68 let fetchOptions: photoAccessHelper.FetchOptions = { 69 fetchColumns: [], 70 predicates: predicates 71 }; 72 // Ensure that there are moving photos in Gallery. 73 let assetResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 74 let asset: photoAccessHelper.PhotoAsset = await assetResult.getFirstObject(); 75 let requestOptions: photoAccessHelper.RequestOptions = { 76 deliveryMode: photoAccessHelper.DeliveryMode.FAST_MODE, 77 } 78 const handler = new MovingPhotoHandler(); 79 try { 80 let requestId: string = await photoAccessHelper.MediaAssetManager.requestMovingPhoto(context, asset, requestOptions, handler); 81 console.info("moving photo requested successfully, requestId: " + requestId); 82 } catch (err) { 83 console.error(`failed to request moving photo, error code is ${err.code}, message is ${err.message}`); 84 } 85} 86``` 87 88## requestContent<sup>12+</sup> 89 90requestContent(imageFileUri: string, videoFileUri: string): Promise\<void> 91 92Requests the image data and video data of this moving photo and writes them to the specified URIs, respectively. 93 94**Atomic service API**: This API can be used in atomic services since API version 12. 95 96**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 97 98**Required permissions**: ohos.permission.READ_IMAGEVIDEO 99 100- When you call this API in Picker mode, you do not need to request the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Accessing and Managing Moving Photos](../../media/medialibrary/photoAccessHelper-movingphoto.md). 101- For the moving photos saved to the media library by this application, the application can access them without the ohos.permission.READ_IMAGEVIDEO permission. 102 103**Parameters** 104 105| Name | Type | Mandatory| Description | 106| -------- |----------------------------------------------------------------------| ---- | ------------------------- | 107| imageFileUri | string | Yes | URI to which the image data of the moving photo is to be written. Example: **"file://com.example.temptest/data/storage/el2/base/haps/ImageFile.jpg"**.| 108| videoFileUri | string | Yes | URI to which the video data of the moving photo is to be written. Example: **"file://com.example.temptest/data/storage/el2/base/haps/VideoFile.mp4"**.| 109 110**Return value** 111 112| Type | Description | 113| --------------------------------------- | ----------------- | 114| Promise\<void> | Promise that returns no value.| 115 116**Error codes** 117 118For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). 119 120| ID| Error Message| 121| -------- | ---------------------------------------- | 122| 201 | Permission denied | 123| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 124| 14000011 | System inner fail. | 125 126**Example** 127 128For details about how to create a phAccessHelper instance, see the example provided in [photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper). 129 130```ts 131import { dataSharePredicates } from '@kit.ArkData'; 132 133class MovingPhotoHandler implements photoAccessHelper.MediaAssetDataHandler<photoAccessHelper.MovingPhoto> { 134 async onDataPrepared(movingPhoto: photoAccessHelper.MovingPhoto) { 135 if (movingPhoto === undefined) { 136 console.error('Error occurred when preparing data'); 137 return; 138 } 139 // The URIs must be valid. 140 let imageFileUri: string = "file://com.example.temptest/data/storage/el2/base/haps/ImageFile.jpg"; 141 let videoFileUri: string = "file://com.example.temptest/data/storage/el2/base/haps/VideoFile.mp4"; 142 try { 143 await movingPhoto.requestContent(imageFileUri, videoFileUri); 144 console.log("moving photo contents retrieved successfully"); 145 } catch (err) { 146 console.error(`failed to retrieve contents of moving photo, error code is ${err.code}, message is ${err.message}`); 147 } 148 } 149} 150 151async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 152 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 153 predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_SUBTYPE, photoAccessHelper.PhotoSubtype.MOVING_PHOTO); 154 let fetchOptions: photoAccessHelper.FetchOptions = { 155 fetchColumns: [], 156 predicates: predicates 157 }; 158 // Ensure that there are moving photos in Gallery. 159 let assetResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 160 let asset: photoAccessHelper.PhotoAsset = await assetResult.getFirstObject(); 161 let requestOptions: photoAccessHelper.RequestOptions = { 162 deliveryMode: photoAccessHelper.DeliveryMode.FAST_MODE, 163 } 164 const handler = new MovingPhotoHandler(); 165 try { 166 let requestId: string = await photoAccessHelper.MediaAssetManager.requestMovingPhoto(context, asset, requestOptions, handler); 167 console.info("moving photo requested successfully, requestId: " + requestId); 168 } catch (err) { 169 console.error(`failed to request moving photo, error code is ${err.code}, message is ${err.message}`); 170 } 171} 172``` 173 174## requestContent<sup>12+</sup> 175 176requestContent(resourceType: ResourceType, fileUri: string): Promise\<void> 177 178Requests the moving photo content of the specified resource type and writes it to the specified URI. 179 180**Atomic service API**: This API can be used in atomic services since API version 12. 181 182**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 183 184**Required permissions**: ohos.permission.READ_IMAGEVIDEO 185 186- When you call this API in Picker mode, you do not need to request the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Accessing and Managing Moving Photos](../../media/medialibrary/photoAccessHelper-movingphoto.md). 187- For the moving photos saved to the media library by this application, the application can access them without the ohos.permission.READ_IMAGEVIDEO permission. 188 189**Parameters** 190 191| Name | Type | Mandatory| Description | 192| -------- |----------------------------------------------------------------------| ---- | ------------------------- | 193| resourceType | [ResourceType](arkts-apis-photoAccessHelper-e.md#resourcetype11) | Yes | Resource type of the moving photo content to request.| 194| fileUri | string | Yes |URI to which the moving photo content is to be written.| 195 196**Return value** 197 198| Type | Description | 199| --------------------------------------- | ----------------- | 200| Promise\<void> | Promise that returns no value.| 201 202**Error codes** 203 204For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). 205 206| ID| Error Message| 207| -------- | ---------------------------------------- | 208| 201 | Permission denied | 209| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 210| 14000011 | System inner fail. | 211 212**Example** 213 214For details about how to create a phAccessHelper instance, see the example provided in [photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper). 215 216```ts 217import { dataSharePredicates } from '@kit.ArkData'; 218 219class MovingPhotoHandler implements photoAccessHelper.MediaAssetDataHandler<photoAccessHelper.MovingPhoto> { 220 async onDataPrepared(movingPhoto: photoAccessHelper.MovingPhoto) { 221 if (movingPhoto === undefined) { 222 console.error('Error occurred when preparing data'); 223 return; 224 } 225 // The URIs must be valid. 226 let imageFileUri: string = "file://com.example.temptest/data/storage/el2/base/haps/ImageFile.jpg"; 227 try { 228 await movingPhoto.requestContent(photoAccessHelper.ResourceType.IMAGE_RESOURCE, imageFileUri); 229 console.log("moving photo image content retrieved successfully"); 230 } catch (err) { 231 console.error(`failed to retrieve image content of moving photo, error code is ${err.code}, message is ${err.message}`); 232 } 233 } 234} 235 236async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 237 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 238 predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_SUBTYPE, photoAccessHelper.PhotoSubtype.MOVING_PHOTO); 239 let fetchOptions: photoAccessHelper.FetchOptions = { 240 fetchColumns: [], 241 predicates: predicates 242 }; 243 // Ensure that there are moving photos in Gallery. 244 let assetResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 245 let asset: photoAccessHelper.PhotoAsset = await assetResult.getFirstObject(); 246 let requestOptions: photoAccessHelper.RequestOptions = { 247 deliveryMode: photoAccessHelper.DeliveryMode.FAST_MODE, 248 } 249 const handler = new MovingPhotoHandler(); 250 try { 251 let requestId: string = await photoAccessHelper.MediaAssetManager.requestMovingPhoto(context, asset, requestOptions, handler); 252 console.info("moving photo requested successfully, requestId: " + requestId); 253 } catch (err) { 254 console.error(`failed to request moving photo, error code is ${err.code}, message is ${err.message}`); 255 } 256} 257``` 258 259## requestContent<sup>12+</sup> 260 261requestContent(resourceType: ResourceType): Promise\<ArrayBuffer> 262 263Requests the moving photo content of the specified resource type and returns it in ArrayBuffer format. 264 265**Atomic service API**: This API can be used in atomic services since API version 12. 266 267**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 268 269**Required permissions**: ohos.permission.READ_IMAGEVIDEO 270 271- When you call this API in Picker mode, you do not need to request the ohos.permission.READ_IMAGEVIDEO permission. For details, see [Accessing and Managing Moving Photos](../../media/medialibrary/photoAccessHelper-movingphoto.md). 272- For the moving photos saved to the media library by this application, the application can access them without the ohos.permission.READ_IMAGEVIDEO permission. 273 274**Parameters** 275 276| Name | Type | Mandatory| Description | 277| -------- |----------------------------------------------------------------------| ---- | ------------------------- | 278| resourceType | [ResourceType](arkts-apis-photoAccessHelper-e.md#resourcetype11) | Yes | Resource type of the moving photo content to request.| 279 280**Return value** 281 282| Type | Description | 283| --------------------------------------- | ----------------- | 284| Promise\<ArrayBuffer> | Promise used to return the requested content in an ArrayBuffer.| 285 286**Error codes** 287 288For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). 289 290| ID| Error Message| 291| -------- | ---------------------------------------- | 292| 201 | Permission denied | 293| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 294| 14000011 | System inner fail. | 295 296**Example** 297 298For details about how to create a phAccessHelper instance, see the example provided in [photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper). 299 300```ts 301import { dataSharePredicates } from '@kit.ArkData'; 302 303class MovingPhotoHandler implements photoAccessHelper.MediaAssetDataHandler<photoAccessHelper.MovingPhoto> { 304 async onDataPrepared(movingPhoto: photoAccessHelper.MovingPhoto) { 305 if (movingPhoto === undefined) { 306 console.error('Error occurred when preparing data'); 307 return; 308 } 309 try { 310 let buffer: ArrayBuffer = await movingPhoto.requestContent(photoAccessHelper.ResourceType.IMAGE_RESOURCE); 311 console.log("moving photo image content retrieved successfully, buffer length: " + buffer.byteLength); 312 } catch (err) { 313 console.error(`failed to retrieve image content of moving photo, error code is ${err.code}, message is ${err.message}`); 314 } 315 } 316} 317 318async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 319 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 320 predicates.equalTo(photoAccessHelper.PhotoKeys.PHOTO_SUBTYPE, photoAccessHelper.PhotoSubtype.MOVING_PHOTO); 321 let fetchOptions: photoAccessHelper.FetchOptions = { 322 fetchColumns: [], 323 predicates: predicates 324 }; 325 // Ensure that there are moving photos in Gallery. 326 let assetResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 327 let asset: photoAccessHelper.PhotoAsset = await assetResult.getFirstObject(); 328 let requestOptions: photoAccessHelper.RequestOptions = { 329 deliveryMode: photoAccessHelper.DeliveryMode.FAST_MODE, 330 } 331 const handler = new MovingPhotoHandler(); 332 try { 333 let requestId: string = await photoAccessHelper.MediaAssetManager.requestMovingPhoto(context, asset, requestOptions, handler); 334 console.info("moving photo requested successfully, requestId: " + requestId); 335 } catch (err) { 336 console.error(`failed to request moving photo, error code is ${err.code}, message is ${err.message}`); 337 } 338} 339``` 340