Skip to content Skip to sidebar Skip to footer

How Can I Get Relative Path Of The Folders In My Android Project?

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 pat

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"


Solution 5:

You can check this sample code to understand how you can access the relative path using the java sample code

import java.io.File;

public class MainClass {

  public static void main(String[] args) {

    File relative = new File("html/javafaq/index.html");

    System.out.println("relative: ");
    System.out.println(relative.getName());
    System.out.println(relative.getPath());
  }
}

Here getPath will display the relative path of the file.


Post a Comment for "How Can I Get Relative Path Of The Folders In My Android Project?"