peterS
peterS

Reputation: 71

How to use a relative path instead of an absolute path in processing?

I am fairly new to processing (and programming in general).

In a project that I'm doing I need to access the following path:

f = new File("C:/Users/Matthew/ColourFeature/data/image1.jpg");

This, being an absolute path, works without any problem.

However since this program will be run on different machines I am trying to figure out how to make use of an relative path in processing to access the path above. By the way, for the purpose of my project I cannot make use of processing's loadImage().

Any help will be greatly appreciated

Upvotes: 1

Views: 11384

Answers (4)

peterS
peterS

Reputation: 71

Thanks for the help. I turned out to be quite simple. The solution is the following:

f = new File(dataPath(""), "/image1.jpg");

However it is worth mentioning that using dataPath("") as a global variable give a different path (I assume it is the sketchbook path) then using dataPath("") within the setup method.

I sorry if I didn't use the correct technical words but i'm fairly new to programming.

Thanks again

Upvotes: 0

Anselan
Anselan

Reputation: 396

I believe that Processing makes this quite easy if you simply use the "data" path that is used by default with each sketch - simply a subfolder called "data" in your sketch folder.

Put all your images (or any other files, for that matter) in the data folder, and you will typically not need to specify an absolute path in most cases (e.g. with loadImage()).

Of course, since you said you can't use loadImage(), you can always access the path to the data folder using the provided method dataPath(""). For example, when I do

println(dataPath(""));

from my example sketch, I get the following output: /Users/stephenbuchanan/Documents/Processing/testsketch/data

Upvotes: 1

Holloway
Holloway

Reputation: 7377

If the file is within your code, you could use the getResource method of the classLoader.

eg

File newFile = new File(YourClass.class
                   .getResource("relative/path/from/YourClass.java")
                   .toString());

If you need to access files in a parent directory of the class you could use the method described by @palacsint here

The normalize() methods (there are four of them) in the FilenameUtils class could help you. It's in the Apache Commons IO library.

final String name =  "/a/b/../";
final String normalizedName = FilenameUtils.normalize(name, true); // "/a/"
getClass().getResource(normalizedName);

Upvotes: 1

icza
icza

Reputation: 418455

You can get the user's home folder by:

String userHome = System.getProperty("user.home");

Which in your case would return "C:/Users/Matthew".

If you agree to use a fixed path inside of this, you can get your image file like:

f = new File(userHome, "/ColourFeature/data/image1.jpg");

This constructor of File takes 2 arguments: a parent folder and a relative child folder and/or file.

Now this f file will point to a proper file in all machines if the currently logged-in user has a file named "image1.jpg" in the "/ColourFeature/data" folder inside his/her home folder.

Upvotes: 2

Related Questions