Java 如何获取我的 android 项目中文件夹的相对路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2698002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:58:01  来源:igfitidea点击:

How can I get relative path of the folders in my android project?

javaandroideclipseresources

提问by Alex Kapustian

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.

我试图在我的班级中做到这一点,它扩展了android.app.Activity.

Perhaps something similar to "get file path from asset".

也许类似于“从资产中获取文件路径”

回答by BalusC

Make use of the classpath.

使用类路径。

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("path/to/folder");
File file = new File(url.toURI());
// ...

回答by gmhk

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

您可以查看此示例代码以了解如何使用 java 示例代码访问相对路径

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.

这里 getPath 将显示文件的相对路径。

回答by JRL

In Android, application-level meta data is accessed through the Contextreference, which an activity is a descendant of.

在 Android 中,应用程序级元数据是通过Context引用访问的,Activity 是它的后代。

For example, you can get the source directory via the getApplicationInfo().sourceDirproperty. There are methods for other folders as well (assets directory, data dir, database dir, etc.).

例如,您可以通过getApplicationInfo().sourceDir属性获取源目录。还有其他文件夹的方法(资产目录、数据目录、数据库目录等)。

回答by Kennet

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".

实际上“找出我在哪里”。

回答by user1254399

With System.getProperty("user.dir")you get the "Base of non-absolute paths"look at

随着System.getProperty("user.dir")你得到“非绝对路径的基础”看看

Java Library Description

Java 库描述

回答by Zin Win Htet

File relativeFile = new File(getClass().getResource("/icons/forIcon.png").toURI());
myJFrame.setIconImage(tk.getImage(relativeFile.getAbsolutePath()));

回答by dchang

With this I found my project path:

有了这个,我找到了我的项目路径:

new File("").getAbsolutePath();

this return "c:\Projects\SampleProject"

这个返回“c:\Projects\SampleProject”

回答by Jigar Stark

Generally we want to add images, txt, doc and etc files inside our Java project and specific folder such as /images. I found in search that in JAVA, we can get path from Root to folder which we specify as,

通常我们想在我们的Java项目和特定文件夹(例如/images)中添加图像、txt、doc等文件。我在搜索中发现在 JAVA 中,我们可以获取从 Root 到我们指定的文件夹的路径,

String myStorageFolder= "/images"; // this is folder name in where I want to store files.

String getImageFolderPath= request.getServletContext().getRealPath(myStorageFolder);

Here, request is object of HttpServletRequest. It will get the whole path from Root to /images folder. You will get output like,

这里,request 是 HttpServletRequest 的对象。它将获取从 Root 到 /images 文件夹的整个路径。你会得到这样的输出,

C:\Users\STARK\Workspaces\MyEclipse.metadata.me_tcat7\webapps\JavaProject\images

C:\Users\STARK\Workspaces\MyEclipse.metadata.me_tcat7\webapps\JavaProject\images