1e41f4b71Sopenharmony_ci# Using SaveButton 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **SaveButton** component comes with the privilege for saving data, which allows an application to temporarily save data without any authorization. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciWhen it is tapped, the application obtains the permission to access the **mediaLibrary** APIs within 10 seconds. You can use this component when your application needs to save images or videos to the media library. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThis component allows for simpler operations than Pickers, which have to start a system application and have the user select a directory for saving the image or video. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciThe following figure shows the effect of the **SaveButton** component. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Constraints 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci- When a user clicks **SaveButton** in the application for the first time, a dialog box will be displayed to request user authorization. If the user clicks **Deny**, the dialog box will be closed and the application does not have the permission. When the user clicks **LocationButton** again, the user authorization dialog box will be displayed again. If the user clicks **Allow**, the dialog box will be closed and the application is granted the temporary location permission. After that, if the user clicks **LocationButton** again, no dialog box will be displayed. 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci- The interval for calling **onClick()** to trigger a **mediaLibrary** API cannot exceed 10 seconds after **SaveButton** is tapped. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- Each time the component is tapped, the application obtains only one-time perform for API calling. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci- The **SaveButton** component must be visible and legible to users. You need to properly configure the component attributes such as the size and color to prevent authorization failures. If the authorization fails due to invalid component style, check the device error logs. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## How to Develop 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ciFor example, to save the image in the dialog box shown above, the application only needs to use the image saving feature for a short period of time in the foreground. In this case, you can the **SaveButton** component to obtain temporary permission to save the image without requesting the related permission for the application. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci1. Import the dependencies. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci ```ts 30e41f4b71Sopenharmony_ci import { photoAccessHelper } from '@kit.MediaLibraryKit'; 31e41f4b71Sopenharmony_ci import { fileIo } from '@kit.CoreFileKit'; 32e41f4b71Sopenharmony_ci ``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci2. Set the image asset and add the **SaveButton** component. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci **SaveButton** is a button-like component consisting of an icon, text, and background. Either the icon or text is mandatory, and the background is mandatory. The icon and text cannot be customized. You can only select from the existing options. When declaring the API for creating a security component, you can determine whether to pass in parameters. If parameters are passed in, the component is created based on the specified parameters. If no parameter is passed in, a component with default icon, text, and background is created. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci The following example uses the default parameters. For details, see [SaveButton](../../reference/apis-arkui/arkui-ts/ts-security-components-savebutton.md). In addition, all security components inherit the [Security Component Universal Attributes](../../reference/apis-arkui/arkui-ts/ts-securitycomponent-attributes.md), which can be used to customize styles. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci ```ts 41e41f4b71Sopenharmony_ci import { photoAccessHelper } from '@kit.MediaLibraryKit'; 42e41f4b71Sopenharmony_ci import { fileIo } from '@kit.CoreFileKit'; 43e41f4b71Sopenharmony_ci import { common } from '@kit.AbilityKit'; 44e41f4b71Sopenharmony_ci import { promptAction } from '@kit.ArkUI'; 45e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci async function savePhotoToGallery(context: common.UIAbilityContext) { 48e41f4b71Sopenharmony_ci let helper = photoAccessHelper.getPhotoAccessHelper(context); 49e41f4b71Sopenharmony_ci try { 50e41f4b71Sopenharmony_ci // After onClick is triggered, call createAsset API within 5 seconds to create an image. After 5 seconds have elapsed, the permission to call createAsset is revoked. 51e41f4b71Sopenharmony_ci let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg'); 52e41f4b71Sopenharmony_ci // Open the file based on its URI. The write process is not time bound. 53e41f4b71Sopenharmony_ci let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 54e41f4b71Sopenharmony_ci // Replace $r('app.media.startIcon') with the image resource file you use. 55e41f4b71Sopenharmony_ci context.resourceManager.getMediaContent($r('app.media.startIcon').id, 0) 56e41f4b71Sopenharmony_ci .then(async value => { 57e41f4b71Sopenharmony_ci let media = value.buffer; 58e41f4b71Sopenharmony_ci // Write data to the file in the media library. 59e41f4b71Sopenharmony_ci await fileIo.write(file.fd, media); 60e41f4b71Sopenharmony_ci await fileIo.close(file.fd); 61e41f4b71Sopenharmony_ci promptAction.showToast({message: 'Saved to album.'}); 62e41f4b71Sopenharmony_ci }); 63e41f4b71Sopenharmony_ci } 64e41f4b71Sopenharmony_ci catch (error) { 65e41f4b71Sopenharmony_ci const err: BusinessError = error as BusinessError; 66e41f4b71Sopenharmony_ci console.error(`Failed to save photo. Code is ${err.code}, message is ${err.message}`); 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci @Entry 71e41f4b71Sopenharmony_ci @Component 72e41f4b71Sopenharmony_ci struct Index { 73e41f4b71Sopenharmony_ci build() { 74e41f4b71Sopenharmony_ci Row() { 75e41f4b71Sopenharmony_ci Column({ space: 10 }) { 76e41f4b71Sopenharmony_ci // Replace $r('app.media.startIcon') with the image resource file you use. 77e41f4b71Sopenharmony_ci Image($r('app.media.startIcon')) 78e41f4b71Sopenharmony_ci .height(400) 79e41f4b71Sopenharmony_ci .width('100%') 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci SaveButton() 82e41f4b71Sopenharmony_ci .padding({top: 12, bottom: 12, left: 24, right: 24}) 83e41f4b71Sopenharmony_ci .onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => { 84e41f4b71Sopenharmony_ci if (result === SaveButtonOnClickResult.SUCCESS) { 85e41f4b71Sopenharmony_ci const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 86e41f4b71Sopenharmony_ci // Obtain temporary authorization to save the image without requesting the related permission for the application. 87e41f4b71Sopenharmony_ci savePhotoToGallery(context); 88e41f4b71Sopenharmony_ci } else { 89e41f4b71Sopenharmony_ci promptAction.showToast ({ message: 'Failed to set the permission.' }) 90e41f4b71Sopenharmony_ci } 91e41f4b71Sopenharmony_ci }) 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci .width('100%') 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci .height('100%') 96e41f4b71Sopenharmony_ci .backgroundColor(0xF1F3F5) 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci ``` 100