Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Check for image before trying to set it

ANSWERED

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`;
}

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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

View best answer in original post

Best Answer
0 Votes
5 REPLIES 5

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 Answer
0 Votes

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 Answer
0 Votes

Ah yeah. PNG gets converted to a file format that is natively supported by the hardware. Glad you got it working.

Best Answer
0 Votes

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 Answer
0 Votes

Fair point. I'll arrange for the documentation to be updated.

Best Answer
0 Votes