
android无法获取res资源文件夹路径,只能通过系统提供的封装函数访问。
资源文件夹有:
/res/drawable
,通过getresources()访问
/res/values
,通过getresources()访问
/res/layout,通过getresources()访问
/res/xml,通过getresources()访问
/res/raw,通过getresources()访问
/assets,通过getassets()访问
Environment 常用方法:
方法:getDataDirectory()
解释:返回 File ,获取 Android
数据目录。
方法:getDownloadCacheDirectory()
解释:返回 File ,获取 Android
下载/缓存内容目录。
方法:getExternalStorageDirectory()
解释:返回 File ,获取外部存储目录即
SDCard
方法:getExternalStoragePublicDirectory(String type)
解释:返回 File
,取一个高端的公用的外部存储器目录来摆放某些类型的文件
方法:getExternalStorageState()
解释:返回 File
,获取外部存储设备的当前状态
方法:getRootDirectory()
解释:返回 File ,获取 Android 的根目录
file的getPath getAbsolutePath和getCanonicalPath的不同
File的这三个方法在api中都有说明,仅以程序为例说明。
package test;
import
javaioFile;
import javaioIOException;
public class TestFilePath
{
public static void main(String[] args) {
// TODO Auto-generated
methodstub
Systemoutprintln(SystemgetProperty("userdir"));
try
{
Systemoutprintln("-----默认相对路径:取得路径不同------");
File file1 =new
File("\\src\\test1txt");
Systemoutprintln(file1getPath());
Systemoutprintln(file1getAbsolutePath());
Systemoutprintln(file1getCanonicalPath());
Systemoutprintln("-----默认相对路径:取得路径不同------");
File
file =new
File("\\test1txt");
Systemoutprintln(filegetPath());
Systemoutprintln(filegetAbsolutePath());
Systemoutprintln(filegetCanonicalPath());
Systemoutprintln("-----默认绝对路径:取得路径相同------");
File
file2 =new
File("D:\\workspace\\test\\test1txt");
Systemoutprintln(file2getPath());
Systemoutprintln(file2getAbsolutePath());
Systemoutprintln(file2getCanonicalPath());
}
catch (IOException e) {
// TODOAuto-generated catch
block
eprintStackTrace();
}
}
}
程序执行结果如下:
F:\eclipseworkspace\testejb
-----默认相对路径:取得路径不同------
\src\test1txt
F:\eclipseworkspace\testejb\\src\test1txt
F:\eclipseworkspace\src\test1txt
-----默认相对路径:取得路径不同------
\test1txt
F:\eclipseworkspace\testejb\\test1txt
F:\eclipseworkspace\testejb\test1txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1txt
D:\workspace\test\test1txt
D:\workspace\test\test1txt
结论:
当输入为绝对路径时,返回的都是绝对路径。
当输入为相对路径时:
getPath()返回的是File构造方法里的路径,是什么就是什么,不增不减
getAbsolutePath()返回的其实是userdir+getPath()的内容,从上面F:\eclipseworkspace\testejb、F:\eclipseworkspace\testejb\\src\test1txt、F:\eclipseworkspace\testejb\\test1txt可以得出。
getCanonicalPath()返回的就是标准的将符号完全解析的路径
public String
getAbsolutePath()返回抽象路径名的绝对路径名字符串。
如果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath()
方法一样。如果此抽象路径名是空的抽象路径名,则返回当前用户目录的路径名字符串,该目录由系统属性 userdir
指定。否则,使用与系统有关的方式分析此路径名。在 UNIX 系统上,通过根据当前用户目录分析某一相对路径名,可使该路径名成为绝对路径名。在 Microsoft
Windows
系统上,通过由路径名指定的当前驱动器目录(如果有)来分析某一相对路径名,可使该路径名成为绝对路径名;否则,可以根据当前用户目录来分析它。
返回:
绝对路径名字符串,它与此抽象路径名表示相同的文件或目录的
抛出:
SecurityException
- 如果无法访问所需的系统属性值。
另请参见:
isAbsolute()
public String getCanonicalPath()
throws
IOException返回抽象路径名的规范路径名字符串。
规范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换成绝对路径名,这与调用
getAbsolutePath() 方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。这通常涉及到从路径名中移除多余的名称(比如 "" 和
"")、分析符号连接(对于 UNIX 平台),以及将驱动器名转换成标准大小写形式(对于 Microsoft Windows
平台)。
表示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式。非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。
返回:
表示与此抽象路径名相同的文件或目录的规范路径名字符串
抛出:
IOException
- 如果发生 I/O 错误(可能是因为构造规范路径名需要进行文件系统查询)
SecurityException -
如果无法访问所需的系统属性值,或者存在安全管理器,且其 SecurityManagercheckRead(javaioFileDescriptor)
方法拒绝对该文件进行读取访问
从以下版本开始:
JDK11
这个是由于android中的安全机制的缘故,由于android继承了Linux系统的传统,对于某个特定的目录有用户的权限,一共分为三种--可读,可写,可执行;虽然说可以设置某个特定的目录的权限,但是对于目录里面的子目录和子文件都可以进行权限的设置,也就是说出了根目录权限之外,子目录本身的权限也决定了子目录可否访问,这一点需要清楚了解,所以在判断完了是否是目录之外,还需要在进行listFiles()获取File[]数据后判断获取的数组是否为空,如果为空的话,文件夹是不可访问的。
package comhangchengutil;
import javaioFile;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javautilArrayList;
import javautilHashMap;
import javautilList;
import androidappActivity;
import androidcontentContext;
import androidcontentresAssetManager;
public class GetfilesFromAssets {
private Activity activity;
private String[] files;
public GetfilesFromAssets(Context context) {
thisactivity = (Activity) context;
}
// public void deepFile(Context ctxDealFile, String path) {
// try {
// String str[] = ctxDealFilegetAssets()list(path);
// if (strlength > 0) {// 如果是目录
// File file = new File("/data/" + path);
// filemkdirs();
// for (String string : str) {
// path = path + "/" + string;
// Systemoutprintln("zhoulc:\t" + path);
// // textViewsetText(textViewgetText()+"\t"+path+"\t");
// deepFile(ctxDealFile, path);
// path = pathsubstring(0, pathlastIndexOf('/'));
// }
// } else {// 如果是文件
// InputStream is = ctxDealFilegetAssets()open(path);
// FileOutputStream fos = new FileOutputStream(new File("/data/"
// + path));
// byte[] buffer = new byte[1024];
// int count = 0;
// while (true) {
// count++;
// int len = isread(buffer);
// if (len == -1) {
// break;
// }
// foswrite(buffer, 0, len);
// }
// isclose();
// fosclose();
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// eprintStackTrace();
// }
// }
public String[] getfileFromAssets(String path) {
AssetManager assetManager = activitygetAssets();
// String[] files;
try {
files = assetManagerlist(path);
} catch (IOException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return files;
}
public List listHtmlOfAssets() {
List list = new ArrayList();
files = getfileFromAssets("html");
for (int i = 0; i < fileslength; i++) {
HashMap map = new HashMap();
mapput("htmlname", files[i]);
listadd(map);
}
return list;
}
}
这个还要看你是什么文件,举个例子获取文件。
Bitmap bitmap = BitmapFactorydecodeFile(androidosEnvironmentgetExternalStorageDirectory()getAbsolutePath()+ "/data/abc/def/ghijkl")
以上就是关于android下怎么获取res资源文件夹的路径全部的内容,包括:android下怎么获取res资源文件夹的路径、android怎样拿到file的路径、安卓开发怎么获取文件夹下面所有文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)