• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SaveButton
2
3The **SaveButton** security component represents a save button that allows you to obtain temporary storage permissions from users with a simple button touch, eliminating the need for a permission request dialog box.
4
5> **NOTE**
6>
7> This component is supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.
8
9## Child Components
10
11Not supported
12
13## APIs
14
15### SaveButton
16
17SaveButton()
18
19Creates a **SaveButton** component with an icon, text, and background.
20
21You may want to learn the [restrictions on security component styles](../../../security/AccessToken/security-component-overview.md#constraints) to avoid authorization failures caused by incompliant styles.
22
23**Atomic service API**: This API can be used in atomic services since API version 11.
24
25**System capability**: SystemCapability.ArkUI.ArkUI.Full
26
27### SaveButton
28
29SaveButton(options: SaveButtonOptions)
30
31Creates a **SaveButton** component that contains the specified elements.
32
33You may want to learn the [restrictions on security component styles](../../../security/AccessToken/security-component-overview.md#constraints) to avoid authorization failures caused by incompliant styles.
34
35**Atomic service API**: This API can be used in atomic services since API version 11.
36
37**System capability**: SystemCapability.ArkUI.ArkUI.Full
38
39**Parameters**
40
41| Name| Type| Mandatory| Description|
42| -------- | -------- | -------- | -------- |
43| options | [SaveButtonOptions](#savebuttonoptions) | Yes| Options for creating the **SaveButton** component.|
44
45## SaveButtonOptions
46
47Describes the icon, text, and other specific elements for the **SaveButton** component.
48
49> **NOTE**
50>
51> - At least one of **icon** or **text** must be provided.<br>
52> - If neither **icon** nor **text** is provided, the **options** parameter in [SaveButton](#savebutton-1) will not take effect, and the created **SaveButton** component will use the default style:
53>
54>   The default value of **SaveIconStyle** is **FULL_FILLED**.
55>
56>   The default style of **SaveDescription** is **DOWNLOAD**.
57>
58>   The default value of **ButtonType** is **Capsule**.
59> - The **icon**, **text**, and **buttonType** parameters do not support dynamic modification.
60
61**Atomic service API**: This API can be used in atomic services since API version 11.
62
63**System capability**: SystemCapability.ArkUI.ArkUI.Full
64
65| Name| Type| Mandatory| Description|
66| -------- | -------- | -------- | -------- |
67| icon | [SaveIconStyle](#saveiconstyle) | No| Icon style of the **SaveButton** component.<br>If this parameter is not specified, there is no icon.|
68| text | [SaveDescription](#savedescription) | No| Text on the **SaveButton** component.<br>If this parameter is not specified, there is no text description.|
69| buttonType | [ButtonType](ts-securitycomponent-attributes.md#buttontype) | No| Background type of the **SaveButton** component.<br>If this parameter is not specified, the system uses a capsule-type button as the **SaveButton** component.|
70
71## SaveIconStyle
72
73**Atomic service API**: This API can be used in atomic services since API version 11.
74
75**System capability**: SystemCapability.ArkUI.ArkUI.Full
76
77| Name| Value| Description|
78| -------- | -------- | -------- |
79| FULL_FILLED | 0 | Filled style icon.|
80| LINES | 1 | Line style icon.|
81
82## SaveDescription
83
84**System capability**: SystemCapability.ArkUI.ArkUI.Full
85
86| Name| Value| Description|
87| -------- | -------- | -------- |
88| DOWNLOAD | 0 | The text on the **SaveButton** component is **Download**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
89| DOWNLOAD_FILE | 1 | The text on the **SaveButton** component is **Download file**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
90| SAVE | 2 | The text on the **SaveButton** component is **Save**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
91| SAVE_IMAGE | 3 | The text on the **SaveButton** component is **Save image**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
92| SAVE_FILE | 4 | The text on the **SaveButton** component is **Save file**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
93| DOWNLOAD_AND_SHARE | 5 | The text on the **SaveButton** component is **Download & share**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
94| RECEIVE | 6 | The text on the **SaveButton** component is **Receive**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
95| CONTINUE_TO_RECEIVE | 7 | The text on the **SaveButton** component is **Continue**.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
96| SAVE_TO_GALLERY<sup>12+</sup> | 8 | The text on the **SaveButton** component is **Save to Gallery**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
97| EXPORT_TO_GALLERY<sup>12+</sup> | 9 | The text on the **SaveButton** component is **Export**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
98| QUICK_SAVE_TO_GALLERY<sup>12+</sup> | 10 | The text on the **SaveButton** component is **Save to Gallery**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
99| RESAVE_TO_GALLERY<sup>12+</sup> | 11 | The text on the **SaveButton** component is **Save**.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
100| SAVE_ALL<sup>18+</sup> | 12 | The text on the **SaveButton** component is **Save all**.<br>**Atomic service API**: This API can be used in atomic services since API version 18.|
101
102## SaveButtonOnClickResult
103
104**Atomic service API**: This API can be used in atomic services since API version 11.
105
106**System capability**: SystemCapability.ArkUI.ArkUI.Full
107
108| Name| Value| Description|
109| -------- | -------- | -------- |
110| SUCCESS | 0 | The **SaveButton** component is touched successfully.|
111| TEMPORARY_AUTHORIZATION_FAILED | 1 | Temporary authorization fails after the **SaveButton** component is touched.|
112
113## SaveButtonCallback<sup>18+</sup>
114
115type SaveButtonCallback = (event: ClickEvent, result: SaveButtonOnClickResult, error?: BusinessError&lt;void&gt;) =&gt; void
116
117Triggered when the **SaveButton** component is clicked.
118
119**Atomic service API**: This API can be used in atomic services since API version 18.
120
121**System capability**: SystemCapability.ArkUI.ArkUI.Full
122
123| Name| Type                  | Mandatory| Description                  |
124|------------|------|-------|---------|
125| event | [ClickEvent](ts-universal-events-click.md#clickevent) |Yes|See **ClickEvent**.|
126| result | [SaveButtonOnClickResult](#savebuttononclickresult)| Yes| Authorization result. The authorization is effective for 10 seconds. This means that, a specific media library API can be called, an unlimited number of times, within 10 seconds of the touch. If the API is not called within the 10 seconds, the authorization fails.|
127| error | [BusinessError&lt;void&gt;](../../apis-basic-services-kit/js-apis-base.md#businesserror) | No| Error code and message when the component is clicked.<br>Error code 0 indicates successful authorization.<br>Error code 1 indicates an internal system error.<br>Error code 2 indicates attribute setting errors, including but not limited to:<br>1. The font or icon size is too small.<br>2. The font or icon color is too similar to the background color.<br>3. The font or icon color is too transparent.<br>4. The padding is negative.<br>5. The component is obscured by other components or windows.<br>6. The text exceeds the background range.<br>7. The component exceeds the window or screen bounds.<br>8. The component size is too large.<br>9. The component text is truncated and not fully displayed.<br>10. Related attribute settings affect the display of security components.|
128
129## Attributes
130
131This component can only inherit the [universal attributes of security components](ts-securitycomponent-attributes.md).
132
133## Events
134
135Only the following events are supported.
136
137### onClick
138
139onClick(event: SaveButtonCallback)
140
141Called when a click event occurs.
142
143**Atomic service API**: This API can be used in atomic services since API version 11.
144
145**System capability**: SystemCapability.ArkUI.ArkUI.Full
146
147**Parameters**
148
149| Name| Type                  | Mandatory| Description                  |
150|------------|------|-------|---------|
151| event | [SaveButtonCallback](#savebuttoncallback18) |Yes|See **SaveButtonCallback**.<br>In API versions 10 to 17, the parameter type is event: [ClickEvent](ts-universal-events-click.md#clickevent), result: [SaveButtonOnClickResult](#savebuttononclickresult)) => void.<br>Since API version 18, the parameter type changes into SaveButtonCallback.|
152
153## Example
154
155```ts
156// xxx.ets
157import { photoAccessHelper } from '@kit.MediaLibraryKit';
158import { fileIo } from '@kit.CoreFileKit';
159import { BusinessError } from '@kit.BasicServicesKit';
160
161@Entry
162@Component
163struct Index {
164  handleSaveButtonClick: SaveButtonCallback =
165    async (event: ClickEvent, result: SaveButtonOnClickResult, error: BusinessError<void>) => {
166      if (result == SaveButtonOnClickResult.SUCCESS) {
167        try {
168          const context = getContext(this);
169          let helper = photoAccessHelper.getPhotoAccessHelper(context);
170          // After onClick is triggered, the createAsset API can be called within 10 seconds to create an image file. After 10 seconds have elapsed, the permission to call createAsset is revoked.
171          let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'png');
172          // Use the URI to open the file. The write process is not time bound.
173          let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE);
174          // Write to the file.
175          await fileIo.write(file.fd, "context");
176          // Close the file.
177          await fileIo.close(file.fd);
178        } catch (error) {
179          console.error("error is " + JSON.stringify(error));
180        }
181      } else {
182        console.info("errCode: " + error.code);
183        console.info("errMessage: " + error.message);
184      }
185    };
186
187  build() {
188    Row() {
189      Column({ space: 10 }) {
190        // Create a default button with an icon, text, and background.
191        SaveButton().onClick((this.handleSaveButtonClick))
192        // Whether the button has an icon, text, and background depends on whether the corresponding parameter is passed in. If buttonType is not passed in, the button uses the ButtonType.Capsule settings.
193        SaveButton({ icon: SaveIconStyle.FULL_FILLED })
194        // Create a button with only an icon and background. If the alpha value of the most significant eight bits of the background color is less than 0x1A, the system forcibly adjusts the alpha value to 0xFF.
195        SaveButton({ icon: SaveIconStyle.FULL_FILLED, buttonType: ButtonType.Capsule })
196          .backgroundColor(0x10007dff)
197        // Create a button with an icon, text, and background. If the alpha value of the most significant eight bits of the background color is less than 0x1A, the system forcibly adjusts the alpha value to 0xFF.
198        SaveButton({ icon: SaveIconStyle.FULL_FILLED, text: SaveDescription.DOWNLOAD, buttonType: ButtonType.Capsule })
199        // Create a button with an icon, text, and background. If the set width is less than the minimum allowed, the button's text will wrap to guarantee full text display.
200        SaveButton({ icon: SaveIconStyle.FULL_FILLED, text: SaveDescription.DOWNLOAD, buttonType: ButtonType.Capsule })
201          .fontSize(16)
202          .width(30)
203        // Create a button with an icon, text, and background. If the set width is less than the minimum allowed, the button's text will wrap to guarantee full text display.
204        SaveButton({ icon: SaveIconStyle.FULL_FILLED, text: SaveDescription.DOWNLOAD, buttonType: ButtonType.Capsule })
205          .fontSize(16)
206          .size({ width: 30, height: 30 })
207        // Create a button with an icon, text, and background. If the set width is less than the minimum allowed, the button's text will wrap to guarantee full text display.
208        SaveButton({ icon: SaveIconStyle.FULL_FILLED, text: SaveDescription.DOWNLOAD, buttonType: ButtonType.Capsule })
209          .fontSize(16)
210          .constraintSize({
211            minWidth: 0,
212            maxWidth: 30,
213            minHeight: 0,
214            maxHeight: 30
215          })
216      }.width('100%')
217    }.height('100%')
218  }
219}
220```
221
222![en-us_image_0000001643320073](figures/en-us_image_0000001643320073.png)
223