loadImage only if file exist

i use loadImage() for a sketch, to read a lot of images from web and export it to pdf.
its working fine.
Is there a way to ask, if the image-file exist before using loadImage()?
In my case, if some files dont exist, the pdf-exporter stops rendering with a error.

Thank you!


This works:

void setup() 
  File tempFile = new File("c:/test/test.txt");
  boolean exists = tempFile.exists();


1 Like

thank you very much for your quick answer.
is there a way to test “on the fly”?
i have a for-to-loop (10000 urls) and im looking for something like:

for-to-loop {
if (file_exist(url)){

My images are named like image1, 2, 3, 4, 5, 6, 7… image10000.jpg!
But just few files are missing.

According to loadImage()'s reference:

It simply returns null when it fails:

If the file is not available or an error occurs, null will be returned and an error message will be printed to the console. The error message does not halt the program, however the null value may cause a NullPointerException if your code does not check whether the value returned is null.

Let’s say you’ve got an ArrayList to store all your loaded images:

import java.util.List;
final List<PImage> images = new ArrayList<PImage>();

Then you could just check for null before add() each PImage:

PImage img;

for (final String url : loadStrings("urls.txt"))
  if ((img = loadImage(url)) != null)  images.add(img);

If that’s so the previous loop can be adapted like this:

final String url = "https://SiteName/FolderName/", name = "image", ext = ".jpg";
PImage img;
int i = 0;

while (++i <= 10000)
  if ((img = loadImage(url + name + i + ext)) != null)  images.add(img);

thank you very much, i will check this.
it will help me for sure :smiley:

1 Like

it works perfect! thanx again!!! great!

1 Like