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 AnswerFollowing 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 Answer
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.
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 AnswerFollowing 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 Answer
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.
Ah yeah. PNG gets converted to a file format that is natively supported by the hardware. Glad you got it working.
Best AnswerIs 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 Answer
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.
Fair point. I'll arrange for the documentation to be updated.
Best Answer