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.

ClassLoaderclassLoader= Thread.currentThread().getContextClassLoader();
URLurl= classLoader.getResource("path/to/folder");
Filefile=newFile(url.toURI());
// ...

Solution 2:

Are you looking for the root folder of the application? Then I would use

Stringpath= getClass().getClassLoader().getResource(".").getPath();

to actually "find out where I am".

Solution 3:

FilerelativeFile=newFile(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;

publicclassMainClass {

  publicstaticvoidmain(String[] args) {

    Filerelative=newFile("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?"