Opening assets inside folders

Hi!
I’m doing a big project using processing. I have a bunch of files containing assets, and I want to store them in
data folder/font_assets
data folder/sprite assets
etc.

But when I try to load them, everything fails. I’ve tried the following:

String path="assets_fonts/";
  FFont=loadImage(path+"nokia_font_dark_strip.png");

This did not work. Then I tried replacing “/” with File.separator:

String path="assets_fonts"+File.separator;
  FFont=loadImage(path+"nokia_font_dark_strip.png");

That didn’t work. So I first tried prefixing path with “.” and, as a second chance, with dataPath():

String path=dataPath(File.separator)+File.separator+"assets_fonts"+File.separator;
  FFont=loadImage(path+"nokia_font_dark_strip.png");

Everytime I get this message:

The file (blah, blah) is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
File path is always an existing path that contains the file I’m looking for.

¿Any suggestion on how to access the file? ¿Should I gave some permission or something? I’m using Processing IDE for Windows.

dataPath()

Hi, @GoToLoop .

Thanks for your reply, but the threads you show do not seem to answer my question. They are all examples of either file saving or opening of files in the data folder, not subfolders of it.

I’m unsure the problem resides in dataPath() function, since the file I’m opening is not inside dataPath, but in a subfolder of it.

Here are some other failed options:

Option a:

String path=dataPath("assets_fonts")+File.separator;
FFont=loadImage(path+"nokia_font_dark_strip.png");

`/* Result: The file "C:\Users\JOSE\Documents\Processing\doorsanddrawers\data\assets_fonts\nokia_font_dark_strip.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.`
*/

Option b:

  FFont=loadImage(dataPath("assets_fonts"+File.separator+"nokia_font_dark_strip.png"));

/* Result: The file "C:\Users\JOSE\Documents\Processing\doorsanddrawers\data\assets_fonts\nokia_font_dark_strip.png" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
*/.

Option b:

  FFont=loadImage(dataPath("assets_fonts"+File.separator+"nokia_font_dark_strip.png"));

In both cases, the file name and path is OK, but processing can’t access the file.

The only solution I found was moving the file to the dataPath (i.e. not a subfolder of it) and using a simple:

  FFont=loadImage("nokia_font_dark_strip.png");

Is this some result of java sandboxing?

Function dataPath() also accepts relative paths w/ subfolders.

If you have a subfolder called “images/” inside sketch’s folder “data/” and a filename called “nokia_font_dark_strip.png” there, you just concatenate both as 1 argument for dataPath():

static final String
  IMAGE_SUBFOLDER = "images/",
  IMAGE_FILENAME  = "nokia_font_dark_strip.png";

PImage img;

void setup() {
  img = loadImage(dataPath(IMAGE_SUBFOLDER + IMAGE_FILENAME));
}

P.S.: Actually, for loading functions, dataPath() isn’t necessary. Just use it for saving-type functions:

static final String
  IMAGE_SUBFOLDER = "images/",
  IMAGE_FILENAME  = "nokia_font_dark_strip.png";

PImage img;

void setup() {
  img = loadImage(IMAGE_SUBFOLDER + IMAGE_FILENAME);
}