06-14-2019 22:51
06-14-2019 22:51
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.
Best Answer06-19-2019 19:38
06-19-2019 19:38
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
Best Answer06-18-2019 14:42
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
06-18-2019 14:42
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/
Best Answer06-19-2019 19:38
06-19-2019 19:38
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
Best Answer06-19-2019 23:56
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
06-19-2019 23:56
Ah yeah. PNG gets converted to a file format that is natively supported by the hardware. Glad you got it working.
Best Answer06-20-2019 12:55
06-20-2019 12:55
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.
Best Answer06-21-2019 10:16
Fitbit Developers oversee the SDK and API forums. We're here to answer questions about Fitbit developer tools, assist with projects, and make sure your voice is heard by the development team.
06-21-2019 10:16
Fair point. I'll arrange for the documentation to be updated.
Best Answer