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"
The method java. io. File. getAbsoluteFile() is used to get the File object with the absolute path for the directory or file.
io. File. getAbsolutePath() is used to obtain the absolute path of a file in the form of a string. This method requires no parameters.
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());
Then, get the parent:
String dir = file.getParent();
Or, if you want the directory as a File
object,
File dirAsFile = file.getParentFile();
A better way, use getParent()
from File
Class..
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With