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"

Share:
124,030
Alex Kapustian
Author by

Alex Kapustian

Updated on August 28, 2020

Comments

  • Alex Kapustian
    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
    Alex Kapustian about 14 years
    If 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
    BalusC about 14 years
    This 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
    BalusC about 14 years
    It'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 of url or file. Play with it and you'll understand.
  • gmhk
    gmhk about 14 years
    @BalusC, Can you suggest any other way?
  • Alex Kapustian
    Alex Kapustian about 14 years
    I still don't understand what do I put instead of -> "path/to/folder"
  • Alex Kapustian
    Alex Kapustian about 14 years
    I didn't find how can I get the app path with this.can you be more specific?
  • Alex Kapustian
    Alex Kapustian about 14 years
    I tried this before and I get "/" but this isn't right. (cause when I try to find a file it doesn't exists)
  • BalusC
    BalusC about 14 years
    Well, the path to the desired folder. Start with an empty string "" and see.
  • BalusC
    BalusC about 14 years
    Isn't your problem more that you don't understand what the classpath is?
  • Kennet
    Kennet about 14 years
    On 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
    Alex Kapustian about 14 years
    My 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
    Alex Kapustian about 14 years
    This 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
    Mike Yockey almost 12 years
    This 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
    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.