Search code examples
javaandroidfilenames

How do I get the folder name from a String containing the Absolute file path in android?


Path name is : /storage/emulated/0/Xender/video/MyVideo.mp4

I am able to get last file name [MyVideo.mp4] from path using

String path="/storage/emulated/0/Xender/video/MyVideo.mp4";
String filename=path.substring(path.lastIndexOf("/")+1);

https://stackoverflow.com/a/26570321/5035015

Now i want to extract path [/storage/emulated/0/Xender/video] from this path.

I have one use of this path in my code so that i want to do this like this.

How can i do this?

Any help will be appreciated.


Solution

  • new File(path).getParentFile().getName() should work.

    With regards to your current code, don't implement your own path parser. Use File.

    Also note that this has nothing to do with Android specifically; this is a general Java question.