从 java (android) 中的文件路径获取目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28594297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:29:00  来源:igfitidea点击:

Get the directory from a file path in java (android)

javaandroidpathdirectory

提问by tru7

Is there a function to get the directory part of a file path?

是否有获取文件路径的目录部分的函数?

so from

所以从

String a="/root/sdcard/Pictures/img0001.jpg";

you get

你得到

"/root/sdcard/Pictures"

采纳答案by nanofarad

Yes. First, construct a Filerepresenting the image path:

是的。首先,构造一个File代表图片路径:

File file = new File(a);

If you're starting from a relative path:

如果您从相对路径开始:

file = new File(file.getAbsolutePath());

Then, get the parent:

然后,获取父级

String dir = file.getParent();

Or, if you want the directory as a Fileobject,

或者,如果您希望目录作为File对象,

File dirAsFile = file.getParentFile();

回答by user370305

A better way, use getParent()from FileClass..

更好的方法,getParent()FileClass使用..

String a="/root/sdcard/Pictures/img0001.jpg"; // A valid file path 
File file = new File(a); 
String getDirectoryPath = file.getParent(); // Only return path if physical file exist else return null

http://developer.android.com/reference/java/io/File.html#getParent%28%29

http://developer.android.com/reference/java/io/File.html#getParent%28%29

回答by winterDroid

You could also use FilenameUtils from Apache. It provides you at least the following features for the example C:\dev\project\file.txt:

您还可以使用Apache 的 FilenameUtils。它至少为示例 C:\dev\project\file.txt 提供了以下功能:

  • the prefix - C:\
  • the path - dev\project\
  • the full path - C:\dev\project\
  • the name - file.txt
  • the base name - file
  • the extension - txt
  • 前缀 - C:\
  • 路径 - dev\project\
  • 完整路径 - C:\dev\project\
  • 名称 - file.txt
  • 基本名称 - 文件
  • 扩展名 - txt

回答by Mahadev Mane

I have got solution on this after 4 days, Please note following points while giving path to File class in Android(Java):

我在 4 天后得到了解决方案,请注意以下几点,同时在 Android(Java) 中提供 File 类的路径:

  1. Use path for internal storage String path="/storage/sdcard0/myfile.txt";
  2. path="/storage/sdcard1/myfile.txt";
  3. mention permissions in Manifest file.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

  4. First check file length for confirmation.
  5. Check paths in ES File Explorer regarding sdcard0 & sdcard1 is this same or else......
  1. 使用内部存储路径 String path="/storage/sdcard0/myfile.txt";
  2. path="/storage/sdcard1/myfile.txt";
  3. 在清单文件中提及权限。

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

  4. 首先检查文件长度进行确认。
  5. 检查 ES 文件资源管理器中关于 sdcard0 和 sdcard1 的路径是否相同,否则......

e.g.

例如

File file=new File(path);
long=file.length();//in Bytes