Saving Photos to the Filesystem
We’re now able to take multiple photos and display them in a photo gallery on the second tab of our app. These photos, however, are not currently being stored permanently, so when the app is closed, they will be lost.
Filesystem API
Fortunately, saving them to the filesystem only takes a few steps. Begin by opening the usePhotoGallery
hook (src/hooks/usePhotoGallery.ts
), and get access to the writeFile
method from the Filesystem
class:
注記
We will use the writeFile
method initially, but we will use the others coming up shortly, so we'll go ahead and import them now.
Next, create a couple of new functions in usePhotoGallery
:
export function usePhotoGallery() {
const savePicture = async (photo: Photo, fileName: string): Promise<UserPhoto> => {
const base64Data = await base64FromPath(photo.webPath!);
const savedFile = await Filesystem.writeFile({
path: fileName,
data: base64Data,
directory: Directory.Data,
});
// Use webPath to display the new image instead of base64 since it's
// already loaded into memory
return {
filepath: fileName,
webviewPath: photo.webPath,
};
};
}
export async function base64FromPath(path: string): Promise<string> {
const response = await fetch(path);
const blob = await response.blob();
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = reject;
reader.onload = () => {
if (typeof reader.result === 'string') {
resolve(reader.result);
} else {
reject('method did not return a string');
}
};
reader.readAsDataURL(blob);
});
}