06-14-2019 22:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-14-2019 22:51
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Is there a way to check if an image file exists before trying to use it? I tried stating the file, but it always fails to find it. I am getting the icon name from a weather web site, so I would like to protect against them changing/adding icons. This is what I tried, but the stat call always fails to find the file. I tried a bunch of different ways to write the file path, nothing worked. Thanks
const filename = `images/weather/${icon}.png`;
try {
const stats = fs.statSync(filename);
forecastElement.image = filename;
} catch (e) {
console.log(`Failed to find '${filename}', using 'unknown' instead`);
forecastElement.image = `images/weather/unknown.png`;
}
Answered! Go to the Best Answer.

Accepted Solutions
06-19-2019 19:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-19-2019 19:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Following up here in case anyone else has a similar problem. That path works, but the image files are 'image.png.txi' for some reason. Not sure why it has 2 extensions, but when I add the second one it finds them. Thanks for the help

06-18-2019 14:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


06-18-2019 14:42
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Your /resources folder exists in: /mnt/assets/resources/
const filename = `/mnt/assets/resources/images/weather/${icon}.png`; const stats = fs.statSync(filename);
if (stats) {
// Tada!
}
https://dev.fitbit.com/build/guides/file-system/

06-19-2019 19:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-19-2019 19:38
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Following up here in case anyone else has a similar problem. That path works, but the image files are 'image.png.txi' for some reason. Not sure why it has 2 extensions, but when I add the second one it finds them. Thanks for the help

06-19-2019 23:56
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


06-19-2019 23:56
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Ah yeah. PNG gets converted to a file format that is natively supported by the hardware. Glad you got it working.

06-20-2019 12:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

06-20-2019 12:55
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Is that documented somewhere and I just missed it? If not, it would be worth adding to the file system and/or image docs so others don't have to stumble around to figure it out.

06-21-2019 10:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post


06-21-2019 10:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Fair point. I'll arrange for the documentation to be updated.

