如何在Android项目中获取文件夹的相对路径? [英] How can I get relative path of the folders in my android project?

查看:597
本文介绍了如何在Android项目中获取文件夹的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用代码获取项目中的文件夹的相对路径?

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.

我试图在我的类中执行它,扩展了 android.app.Activity

I'm trying to do it in my class which extends android.app.Activity.

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

推荐答案

使用类路径。

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

这篇关于如何在Android项目中获取文件夹的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆