从java(android)文件路径获取目录 [英] Get the directory from a file path in java (android)
本文介绍了从java(android)文件路径获取目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一个函数来获取文件路径的目录部分?
所以从
String a="/root/sdcard/Pictures/img0001.jpg";
你得到
"/root/sdcard/Pictures"
推荐答案
p>是的。首先,构建一个 文件
表示图像路径:
Yes. First, construct a File
representing the image path:
File file = new File(a);
如果您从相对路径开始:
If you're starting from a relative path:
file = new File(file.getAbsolutePath());
然后,获得父级:
String dir = file.getParent();
或者,如果您希望目录为文件
object,
Or, if you want the directory as a File
object,
File dirAsFile = file.getParentFile();
这篇关于从java(android)文件路径获取目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文