Skip to content

Commit 77848e5

Browse files
committed
docs(angular): use correct method
1 parent 8cbe764 commit 77848e5

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

docs/angular/your-first-app/5-adding-mobile.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,33 +69,39 @@ private async readAsBase64(photo: Photo) {
6969

7070
Next, update the `savePicture()` method. When running on mobile, set `filepath` to the result of the `writeFile()` operation - `savedFile.uri`. When setting the `webviewPath`, use the special `Capacitor.convertFileSrc()` method ([details on the File Protocol](../../core-concepts/webview.md#file-protocol)). To use this method, we'll need to import Capacitor into `photo.service.ts`.
7171

72-
```tsx
72+
```ts
7373
import { Capacitor } from '@capacitor/core';
7474
```
7575

7676
Then update `savePicture()` to look like the following:
7777

7878
```ts
79-
// CHANGE: Update `loadSaved` method.
80-
public async loadSaved() {
81-
// Retrieve cached photo array data
82-
const { value } = await Preferences.get({ key: this.PHOTO_STORAGE });
83-
this.photos = (value ? JSON.parse(value) : []) as UserPhoto[];
84-
85-
// Easiest way to detect when running on the web:
86-
// “when the platform is NOT hybrid, do this”
87-
if (!this.platform.is('hybrid')) {
88-
// Display the photo by reading into base64 format
89-
for (let photo of this.photos) {
90-
// Read each saved photo's data from the Filesystem
91-
const readFile = await Filesystem.readFile({
92-
path: photo.filepath,
93-
directory: Directory.Data
94-
});
79+
// CHANGE: Update `savePicture()` method.
80+
private async savePicture(photo: Photo) {
81+
// Convert photo to base64 format, required by Filesystem API to save
82+
const base64Data = await this.readAsBase64(photo);
83+
84+
// Write the file to the data directory
85+
const fileName = Date.now() + '.jpeg';
86+
const savedFile = await Filesystem.writeFile({
87+
path: fileName,
88+
data: base64Data,
89+
directory: Directory.Data
90+
});
9591

96-
// Web platform only: Load the photo as base64 data
97-
photo.webviewPath = `data:image/jpeg;base64,${readFile.data}`;
98-
}
92+
if (this.platform.is('hybrid')) {
93+
// Display the new image by rewriting the 'file://' path to HTTP
94+
return {
95+
filepath: savedFile.uri,
96+
webviewPath: Capacitor.convertFileSrc(savedFile.uri),
97+
};
98+
} else {
99+
// Use webPath to display the new image instead of base64 since it's
100+
// already loaded into memory
101+
return {
102+
filepath: fileName,
103+
webviewPath: photo.webPath
104+
};
99105
}
100106
}
101107
```

0 commit comments

Comments
 (0)