1# Using SaveButton
2
3The **SaveButton** component comes with the privilege for saving data, which allows an application to temporarily save data without any authorization.
4
5When 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.
6
7This 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.
8
9The following figure shows the effect of the **SaveButton** component.
10
11![](figures/SaveButton_effect.png)
12
13## Constraints
14
15- 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.
16
17- The interval for calling **onClick()** to trigger a **mediaLibrary** API cannot exceed 10 seconds after **SaveButton** is tapped.
18
19- Each time the component is tapped, the application obtains only one-time perform for API calling.
20
21- 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.
22
23## How to Develop
24
25For 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.
26
271. Import the dependencies.
28
29   ```ts
30   import { photoAccessHelper } from '@kit.MediaLibraryKit';
31   import { fileIo } from '@kit.CoreFileKit';
32   ```
33
342. Set the image asset and add the **SaveButton** component.
35
36   **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.
37
38   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.
39
40   For details about how to save images to the media library, see [Saving Media Assets](../../media/medialibrary/photoAccessHelper-savebutton.md).
41
42   ```ts
43   import { photoAccessHelper } from '@kit.MediaLibraryKit';
44   import { fileIo } from '@kit.CoreFileKit';
45   import { common } from '@kit.AbilityKit';
46   import { promptAction } from '@kit.ArkUI';
47   import { BusinessError } from '@kit.BasicServicesKit';
48
49   async function savePhotoToGallery(context: common.UIAbilityContext) {
50     let helper = photoAccessHelper.getPhotoAccessHelper(context);
51     try {
52       // Call createAsset() within 10 seconds after onClick is triggered to create an image file. After 10 seconds have elapsed, the permission for calling createAsset is revoked.
53       let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg');
54       // Open the file based on its URI. The write process is not time bound.
55       let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
56       // Replace $r('app.media.startIcon') with the image resource file you use.
57       context.resourceManager.getMediaContent($r('app.media.startIcon').id, 0)
58         .then(async value => {
59           let media = value.buffer;
60           // Write data to the file in the media library.
61           await fileIo.write(file.fd, media);
62           await fileIo.close(file.fd);
63           promptAction.showToast({message: 'Saved to album.'});
64         });
65     }
66     catch (error) {
67       const err: BusinessError = error as BusinessError;
68       console.error(`Failed to save photo. Code is ${err.code}, message is ${err.message}`);
69     }
70   }
71
72   @Entry
73   @Component
74   struct Index {
75     build() {
76       Row() {
77         Column({ space: 10 }) {
78           // Replace $r('app.media.startIcon') with the image resource file you use.
79           Image($r('app.media.startIcon'))
80             .height(400)
81             .width('100%')
82
83           SaveButton()
84             .padding({top: 12, bottom: 12, left: 24, right: 24})
85             .onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => {
86               if (result === SaveButtonOnClickResult.SUCCESS) {
87                 const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
88                 // Obtain temporary authorization to save the image without requesting the related permission for the application.
89                 savePhotoToGallery(context);
90               } else {
91                 promptAction.showToast ({ message: 'Failed to set the permission.' })
92               }
93             })
94         }
95         .width('100%')
96       }
97       .height('100%')
98       .backgroundColor(0xF1F3F5)
99     }
100   }
101   ```
102