@@ -12,59 +12,206 @@ Fortunately, saving them to the filesystem only takes a few steps. Begin by open
12
12
13
13
The Filesystem API requires that files written to disk are passed in as base64 data, so this helper function will be used in a moment to assist with that:
14
14
15
- ``` tsx
16
- const convertBlobToBase64 = (blob : Blob ) =>
17
- new Promise ((resolve , reject ) => {
18
- const reader = new FileReader ();
19
- reader .onerror = reject ;
20
- reader .onload = () => {
21
- resolve (reader .result );
22
- };
23
- reader .readAsDataURL (blob );
15
+ ``` typescript
16
+ import { ref , onMounted , watch } from ' vue' ;
17
+ import { Camera , CameraResultType , CameraSource , Photo } from ' @capacitor/camera' ;
18
+ import { Filesystem , Directory } from ' @capacitor/filesystem' ;
19
+ import { Preferences } from ' @capacitor/preferences' ;
20
+
21
+ export const usePhotoGallery = () => {
22
+ const photos = ref <UserPhoto []>([]);
23
+
24
+ const takePhoto = async () => {
25
+ // Same old code from before.
26
+ };
27
+
28
+ // CHANGE: Add the `convertBloblToBase64` method.
29
+ const convertBlobToBase64 = (blob : Blob ) =>
30
+ new Promise ((resolve , reject ) => {
31
+ const reader = new FileReader ();
32
+ reader .onerror = reject ;
33
+ reader .onload = () => {
34
+ resolve (reader .result );
35
+ };
36
+ reader .readAsDataURL (blob );
24
37
});
38
+
39
+ return {
40
+ photos ,
41
+ takePhoto
42
+ };
43
+ };
44
+
45
+ export interface UserPhoto {
46
+ filepath: string ;
47
+ webviewPath? : string ;
48
+ }
25
49
```
26
50
27
- Next, add a function to save the photo to the filesystem. We pass in the ` photo ` object, which represents the newly captured device photo, as well as the fileName, which will provide a path for the file to be stored to .
51
+ Next, we'll add a function to save the photo to the filesystem. We pass in the ` photo ` object, which represents the newly captured device photo, as well as the fileName, the path where the file will be stored.
28
52
29
- Next we use the Capacitor [ Filesystem API] ( https://capacitorjs.com/docs/apis/filesystem ) to save the photo to the filesystem. We start by converting the photo to base64 format, then feed the data to the Filesystem’s ` writeFile ` function:
53
+ We use the Capacitor [ Filesystem API] ( https://capacitorjs.com/docs/apis/filesystem ) to save the photo to the filesystem. Create a new method inside ` usePhotoGallery ` called ` savePicture ` . This method will first convert the photo to base64 format, then feed the data to the Filesystem’s ` writeFile ` function:
30
54
31
- ``` tsx
32
- const savePicture = async (photo : Photo , fileName : string ): Promise <UserPhoto > => {
33
- // Fetch the photo, read as a blob, then convert to base64 format
34
- const response = await fetch (photo .webPath ! );
35
- const blob = await response .blob ();
36
- const base64Data = (await convertBlobToBase64 (blob )) as string ;
37
-
38
- const savedFile = await Filesystem .writeFile ({
39
- path: fileName ,
40
- data: base64Data ,
41
- directory: Directory .Data ,
42
- });
55
+ ``` typescript
56
+ import { ref , onMounted , watch } from ' vue' ;
57
+ import { Camera , CameraResultType , CameraSource , Photo } from ' @capacitor/camera' ;
58
+ import { Filesystem , Directory } from ' @capacitor/filesystem' ;
59
+ import { Preferences } from ' @capacitor/preferences' ;
60
+
61
+ export const usePhotoGallery = () => {
62
+ const photos = ref <UserPhoto []>([]);
63
+
64
+ const takePhoto = async () => {
65
+ // Same old code from before.
66
+ };
67
+
68
+ const convertBlobToBase64 = (blob : Blob ) => {
69
+ // Same old code from before.
70
+ };
71
+
72
+ // CHANGE: Add the `savePicture` method.
73
+ const savePicture = async (photo : Photo , fileName : string ): Promise <UserPhoto > => {
74
+ // Fetch the photo, read as a blob, then convert to base64 format
75
+ const response = await fetch (photo .webPath ! );
76
+ const blob = await response .blob ();
77
+ const base64Data = (await convertBlobToBase64 (blob )) as string ;
78
+
79
+ const savedFile = await Filesystem .writeFile ({
80
+ path: fileName ,
81
+ data: base64Data ,
82
+ directory: Directory .Data ,
83
+ });
84
+
85
+ // Use webPath to display the new image instead of base64 since it's
86
+ // already loaded into memory
87
+ return {
88
+ filepath: fileName ,
89
+ webviewPath: photo .webPath ,
90
+ };
91
+ };
92
+
93
+ return {
94
+ photos ,
95
+ takePhoto
96
+ };
97
+ };
98
+
99
+ export interface UserPhoto {
100
+ filepath: string ;
101
+ webviewPath? : string ;
102
+ }
103
+ ```
104
+
105
+ Last, update the ` takePhoto ` function to call ` savePicture ` :
106
+
107
+ ``` typescript
108
+ import { ref , onMounted , watch } from ' vue' ;
109
+ import { Camera , CameraResultType , CameraSource , Photo } from ' @capacitor/camera' ;
110
+ import { Filesystem , Directory } from ' @capacitor/filesystem' ;
111
+ import { Preferences } from ' @capacitor/preferences' ;
112
+
113
+ export const usePhotoGallery = () => {
114
+ const photos = ref <UserPhoto []>([]);
115
+
116
+ // CHANGE: Update the `takePhoto` method.
117
+ const takePhoto = async () => {
118
+ const photo = await Camera .getPhoto ({
119
+ resultType: CameraResultType .Uri ,
120
+ source: CameraSource .Camera ,
121
+ quality: 100 ,
122
+ });
123
+ const fileName = Date .now () + ' .jpeg' ;
124
+ // CHANGE: Update to call `savePicture` method.
125
+ const savedFileImage = await savePicture (photo , fileName );
126
+
127
+ photos .value = [savedFileImage , ... photos .value ];
128
+ };
129
+
130
+ const convertBlobToBase64 = (blob : Blob ) => {
131
+ // Same old code from before.
132
+ };
133
+
134
+ const savePicture = async (photo : Photo , fileName : string ): Promise <UserPhoto > => {
135
+ // Same old code from before.
136
+ };
43
137
44
- // Use webPath to display the new image instead of base64 since it's
45
- // already loaded into memory
46
138
return {
47
- filepath: fileName ,
48
- webviewPath: photo . webPath ,
139
+ photos ,
140
+ takePhoto
49
141
};
50
142
};
143
+
144
+ export interface UserPhoto {
145
+ filepath: string ;
146
+ webviewPath? : string ;
147
+ }
51
148
```
52
149
53
- Last, update the ` takePhoto ` function to call ` savePicture ` . Once the photo has been saved, insert it into the front of reactive ` photos ` array:
150
+ There we go! Each time a new photo is taken, it’s now automatically saved to the filesystem.
151
+
152
+ ` usePhotoGallery.ts ` should now look like this:
54
153
55
154
``` tsx
56
- const takePhoto = async () => {
57
- const photo = await Camera .getPhoto ({
58
- resultType: CameraResultType .Uri ,
59
- source: CameraSource .Camera ,
60
- quality: 100 ,
155
+ import { ref , onMounted , watch } from ' vue' ;
156
+ import { Camera , CameraResultType , CameraSource , Photo } from ' @capacitor/camera' ;
157
+ import { Filesystem , Directory } from ' @capacitor/filesystem' ;
158
+ import { Preferences } from ' @capacitor/preferences' ;
159
+
160
+ export const usePhotoGallery = () => {
161
+ const photos = ref <UserPhoto []>([]);
162
+
163
+ const takePhoto = async () => {
164
+ const photo = await Camera .getPhoto ({
165
+ resultType: CameraResultType .Uri ,
166
+ source: CameraSource .Camera ,
167
+ quality: 100 ,
168
+ });
169
+ const fileName = Date .now () + ' .jpeg' ;
170
+ const savedFileImage = await savePicture (photo , fileName );
171
+
172
+ photos .value = [savedFileImage , ... photos .value ];
173
+ };
174
+
175
+ const convertBlobToBase64 = (blob : Blob ) =>
176
+ new Promise ((resolve , reject ) => {
177
+ const reader = new FileReader ();
178
+ reader .onerror = reject ;
179
+ reader .onload = () => {
180
+ resolve (reader .result );
181
+ };
182
+ reader .readAsDataURL (blob );
61
183
});
62
184
63
- const fileName = Date .now () + ' .jpeg' ;
64
- const savedFileImage = await savePicture (photo , fileName );
185
+ const savePicture = async (photo : Photo , fileName : string ): Promise <UserPhoto > => {
186
+ // Fetch the photo, read as a blob, then convert to base64 format
187
+ const response = await fetch (photo .webPath ! );
188
+ const blob = await response .blob ();
189
+ const base64Data = (await convertBlobToBase64 (blob )) as string ;
190
+
191
+ const savedFile = await Filesystem .writeFile ({
192
+ path: fileName ,
193
+ data: base64Data ,
194
+ directory: Directory .Data ,
195
+ });
196
+
197
+ // Use webPath to display the new image instead of base64 since it's
198
+ // already loaded into memory
199
+ return {
200
+ filepath: fileName ,
201
+ webviewPath: photo .webPath ,
202
+ };
203
+ };
65
204
66
- photos .value = [savedFileImage , ... photos .value ];
205
+ return {
206
+ photos ,
207
+ takePhoto
208
+ };
67
209
};
210
+
211
+ export interface UserPhoto {
212
+ filepath: string ;
213
+ webviewPath? : string ;
214
+ }
68
215
```
69
216
70
- There we go! Each time a new photo is taken, it’s now automatically saved to the filesystem .
217
+ Next up, we'll load and display our saved images .
0 commit comments