简体   繁体   English

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

[英]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")您将看到“非绝对路径的基础”

Java Library Description Java库描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 gradle 6.5.1 中获取我的项目的相对路径 apk - How to get relative path apk of my project in gradle 6.5.1 如何使用相对路径在项目文件中使用FileWriter创建文件 - How can I create a file with FileWriter within my project file using relative path 在maven项目中我如何指定文件的相对路径 - In maven project how i can specify a relative path to file 如何从 jar 下的相对路径获取 URL? - How can I get a URL from relative path under a jar? 如何获得Jackson JsonTypeInfo以使用相对路径 - How can I get Jackson JsonTypeInfo to use relative path 我需要在Java项目中使用什么相对路径 - What relative path do I need to use in my Java Project Talend:如何在tFileOutputExcel中使用相对路径或如何获取项目目录? - Talend: How to use relative path in tFileOutputExcel or how to get the project dir? 如何获取相对路径作为同一项目文件夹中图像文件的字符串? - How to get relative path as String of an image file in same project folder? 如何在Maven项目中获取资源文件的相对路径? - How to get a relative path of a resource file in a Maven project? 我怎么能确定我的javaFX应用程序是从相对路径加载图像? - How can I be sure that my javaFX application is loading images from a relative path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM