How can I get relative path of the folders in my android project?
124,030
Solution 1
Make use of the classpath.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("path/to/folder");
File file = new File(url.toURI());
// ...
Solution 2
Are you looking for the root folder of the application? Then I would use
String path = getClass().getClassLoader().getResource(".").getPath();
to actually "find out where I am".
Solution 3
File relativeFile = new File(getClass().getResource("/icons/forIcon.png").toURI());
myJFrame.setIconImage(tk.getImage(relativeFile.getAbsolutePath()));
Solution 4
With this I found my project path:
new File("").getAbsolutePath();
this return "c:\Projects\SampleProject"
Author by
Alex Kapustian
Updated on August 28, 2020Comments
-
Alex Kapustian almost 4 years
How can I get the relative path of the folders in my project using code?
I've created a new folder in my project and I want its relative path so no matter where the app is, the path will be correct.
I'm trying to do it in my class which extends
android.app.Activity
.Perhaps something similar to "get file path from asset".
-
Alex Kapustian about 14 yearsIf by "path/to/folder" you mean that I need to insert the path to the project folder so what is the point in it....?
-
BalusC about 14 yearsThis is the wrong way. The path will be relative to the working directory. I.e. the folder from which you executed the program, not the folder where the program itself is running in. Never use relative paths in
java.io.File
, that's only portability trouble. -
BalusC about 14 yearsIt's relative to the classpath root. You don't need
c:/absolute/path/to/java/program
in front of it. Try the above. Print the outcome ofurl
orfile
. Play with it and you'll understand. -
gmhk about 14 years@BalusC, Can you suggest any other way?
-
Alex Kapustian about 14 yearsI still don't understand what do I put instead of -> "path/to/folder"
-
Alex Kapustian about 14 yearsI didn't find how can I get the app path with this.can you be more specific?
-
Alex Kapustian about 14 yearsI tried this before and I get "/" but this isn't right. (cause when I try to find a file it doesn't exists)
-
BalusC about 14 yearsWell, the path to the desired folder. Start with an empty string
""
and see. -
BalusC about 14 yearsIsn't your problem more that you don't understand what the classpath is?
-
Kennet about 14 yearsOn which platform/OS are you running? Just "/" would indicate Unix/linux/OSX, and that you're running your application from the root path of the file system. If that's not correct, please give more info.
-
Alex Kapustian about 14 yearsMy OS is win7 the path is: "D:\Eclipse\Workspace\Project Name" Don't forget that this is an android app that I'm running using emulator
-
Alex Kapustian about 14 yearsThis way I can get only folders that are in the package. I need to get a relative path to a folder that I created inside the project (not inside the package). Folder like assets. Can you give me the function that can do it?
-
Mike Yockey almost 12 yearsThis little snippet of code helped me debug a strange problem I was having with obtaining classpath resources in someone else's integration test suite. Thanks.
-
AJMansfield over 11 years@Alex Kapustian To make a relative pathname, you use stuff like "..\myFolder\myFile" to reference the myFile in myFolder, which is in the same directory as the folder your program is in. ".." is like the 'up one' button in your filesystem browser.