[英]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 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" . 也许类似于“从资产中获取文件路径” 。
Make use of the classpath. 利用类路径。
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL url = classLoader.getResource("path/to/folder");
File file = new File(url.toURI());
// ...
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". 实际上“找出我在哪里”。
File relativeFile = new File(getClass().getResource("/icons/forIcon.png").toURI());
myJFrame.setIconImage(tk.getImage(relativeFile.getAbsolutePath()));
With this I found my project path: 有了这个,我找到了我的项目路径:
new File("").getAbsolutePath();
this return "c:\\Projects\\SampleProject" 返回“c:\\ Projects \\ SampleProject”
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将显示文件的相对路径。
In Android, application-level meta data is accessed through the Context
reference, which an activity is a descendant of. 在Android中,应用程序级元数据通过
Context
引用访问,后者是一个活动的后代。
For example, you can get the source directory via the getApplicationInfo().sourceDir
property. 例如,您可以通过
getApplicationInfo().sourceDir
属性获取源目录。 There are methods for other folders as well (assets directory, data dir, database dir, etc.). 还有其他文件夹的方法(资产目录,数据目录,数据库目录等)。
Generally we want to add images, txt, doc and etc files inside our Java project and specific folder such as /images. 通常我们希望在Java项目和特定文件夹(如/ images)中添加图像,txt,doc等文件。 I found in search that in JAVA, we can get path from Root to folder which we specify as,
我在搜索中发现,在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. 这里,request是HttpServletRequest的对象。 It will get the whole path from Root to /images folder.
它将获得从Root到/ images文件夹的整个路径。 You will get output like,
你会得到像,
C:\\Users\\STARK\\Workspaces\\MyEclipse.metadata.me_tcat7\\webapps\\JavaProject\\images
C:\\用户\\ STARK \\工作区\\ MyEclipse.metadata.me_tcat7 \\的webapps \\ JavaProject \\图片
With System.getProperty("user.dir")
you get the "Base of non-absolute paths" look at 使用
System.getProperty("user.dir")
您将看到“非绝对路径的基础”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.