1# Saving Media Assets 2<!--Kit: Media Library Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @yixiaoff--> 5<!--SE: @liweilu1--> 6<!--TSE: @xchaosioda--> 7 8To save images, videos, or similar files to Gallery, it is not necessary for the application to request the ohos.permission.WRITE_IMAGEVIDEO permission. Instead, the application can use the [SaveButton](#creating-a-media-asset-using-savebutton) or [authorization pop-up](#saving-a-media-asset-using-an-authorization-pop-up) to save the media assets to Gallery. 9 10> **NOTE** 11> 12> Media Library Kit handles image and video management. If you need to read and save audio files, use [AudioViewPicker](../../reference/apis-core-file-kit/js-apis-file-picker.md#audioviewpicker). 13 14## Obtaining Supported Resource Formats for Saving 15 16The following describes how to obtain image resource formats that can be saved. 17 18**How to Develop** 19 20Call [phAccessHelper.getSupportedPhotoFormats](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#getsupportedphotoformats18) to obtain the supported image formats that can be saved. 21 22```ts 23import photoAccessHelper from '@ohos.file.photoAccessHelper'; 24import { common } from '@kit.AbilityKit'; 25 26@Entry 27@Component 28struct Index { 29 @State outputText: string = 'Supported formats:\n'; 30 31 build() { 32 Row() { 33 Button("example").onClick(async () => { 34 let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; 35 let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 36 example(phAccessHelper); 37 }).width('100%') 38 } 39 .height('90%') 40 } 41} 42 43async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper){ 44 try { 45 let outputText = 'Supported formats:\n'; 46 // The value 1 means the supported image formats, and 2 means the supported video formats. 47 let imageFormat = await phAccessHelper.getSupportedPhotoFormats(1); 48 let result = ""; 49 for (let i = 0; i < imageFormat.length; i++) { 50 result += imageFormat[i]; 51 if (i !== imageFormat.length - 1) { 52 result += ', '; 53 } 54 } 55 outputText += result; 56 console.info('getSupportedPhotoFormats success, data is ' + outputText); 57 } catch (error) { 58 console.error('getSupportedPhotoFormats failed, errCode is', error); 59 } 60} 61``` 62 63## Creating a Media Asset Using SaveButton 64 65For details about the **SaveButton** component, see [SaveButton](../../reference/apis-arkui/arkui-ts/ts-security-components-savebutton.md). 66 67The following walks you through on how to create an image using the **SaveButton** security component. 68 69**How to Develop** 70 711. Set the attributes of the security component. 722. Create a button with the security component. 733. Use [MediaAssetChangeRequest.createImageAssetRequest](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#createimageassetrequest11) and [PhotoAccessHelper.applyChanges](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#applychanges11) to create an image asset. 74 75```ts 76import { photoAccessHelper } from '@kit.MediaLibraryKit'; 77import { common } from '@kit.AbilityKit'; 78 79@Entry 80@Component 81struct Index { 82 saveButtonOptions: SaveButtonOptions = { 83 icon: SaveIconStyle.FULL_FILLED, 84 text: SaveDescription.SAVE_IMAGE, 85 buttonType: ButtonType.Capsule 86 } // Set properties of SaveButton. 87 88 build() { 89 Row() { 90 Column() { 91 SaveButton(this.saveButtonOptions) // Create a button with SaveButton. 92 .onClick(async (event, result: SaveButtonOnClickResult) => { 93 if (result == SaveButtonOnClickResult.SUCCESS) { 94 try { 95 let context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; 96 let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 97 // Ensure that the asset specified by fileUri exists. 98 let fileUri = 'file://com.example.temptest/data/storage/el2/base/haps/entry/files/test.jpg'; 99 let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(context, fileUri); 100 await phAccessHelper.applyChanges(assetChangeRequest); 101 console.info('createAsset successfully, uri: ' + assetChangeRequest.getAsset().uri); 102 } catch (err) { 103 console.error(`create asset failed with error: ${err.code}, ${err.message}`); 104 } 105 } else { 106 console.error('SaveButtonOnClickResult create asset failed'); 107 } 108 }) 109 } 110 .width('100%') 111 } 112 .height('100%') 113 } 114} 115``` 116 117In addition to specifying the asset in the application sandbox directory using **fileUri**, you can add the asset using ArrayBuffer. For details, see the [addResource](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#addresource11-1). 118 119## Saving a Media Asset Using an Authorization Pop-Up 120 121The following walks you through on how to save an image using an authorization pop-up. 122 123**How to Develop** 124 1251. Specify the URI of the [application file](../../file-management/app-file-access.md) to be saved to the media library. (The file must be in the application sandbox.) 1262. Set parameters such as the file name extension, image file type, title (optional) and image subtype (optional) of the image to save. 1273. Call [showAssetsCreationDialog](../../reference/apis-media-library-kit/arkts-apis-photoAccessHelper-PhotoAccessHelper.md#showassetscreationdialog12) to obtain the target [media file URI](../../file-management/user-file-uri-intro.md#media-file-uri) through an authorization pop-up. 128 129 To display the application name in the dialog box, the API relies on the configuration of **label** and **icon** under **abilities** in the **module.json5** file. If they are not configured, no application name is displayed in the dialog box. If the passed URI is a sandbox path, images or videos can be saved but cannot be previewed. 1304. Write the image content from the application sandbox directory to the file specified by the target URI in the media library. 131 132```ts 133import { photoAccessHelper } from '@kit.MediaLibraryKit'; 134import { fileIo } from '@kit.CoreFileKit'; 135 136async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper){ 137 try { 138 // Specify the URI of the image in the application sandbox directory to be saved. 139 let srcFileUri = 'file://com.example.temptest/data/storage/el2/base/haps/entry/files/test.jpg'; 140 let srcFileUris: Array<string> = [ 141 srcFileUri 142 ]; 143 // Set parameters such as the file name extension, image file type, title (optional) and image subtype (optional) of the image to save. 144 let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [ 145 { 146 title: 'test', // This parameter is optional. 147 fileNameExtension: 'jpg', 148 photoType: photoAccessHelper.PhotoType.IMAGE, 149 subtype: photoAccessHelper.PhotoSubtype.DEFAULT, // This parameter is optional. 150 } 151 ]; 152 // Obtain the target URI in the media library based on pop-up authorization. 153 let desFileUris: Array<string> = await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs); 154 // Write the image content from the application sandbox directory to the file specified by the target URI in the media library. 155 let desFile: fileIo.File = await fileIo.open(desFileUris[0], fileIo.OpenMode.WRITE_ONLY); 156 let srcFile: fileIo.File = await fileIo.open(srcFileUri, fileIo.OpenMode.READ_ONLY); 157 await fileIo.copyFile(srcFile.fd, desFile.fd); 158 fileIo.closeSync(srcFile); 159 fileIo.closeSync(desFile); 160 console.info('create asset by dialog successfully'); 161 } catch (err) { 162 console.error(`failed to create asset by dialog successfully errCode is: ${err.code}, ${err.message}`); 163 } 164} 165``` 166