Android Utililty Methods Path Relative Get

List of utility methods to do Path Relative Get


The list of methods to do Path Relative Get are organized into topic(s).


StringconvertToRelativePath(String absolutePath, String relativeTo)
taken from
StringBuilder relativePath = null;
absolutePath = absolutePath.replaceAll("\\\\", "/");
relativeTo = relativeTo.replaceAll("\\\\", "/");
if (absolutePath.equals(relativeTo) == true) {
} else {
    String[] absoluteDirectories = absolutePath.split("/");
    String[] relativeDirectories = relativeTo.split("/");
    int length = absoluteDirectories.length < relativeDirectories.length ? absoluteDirectories.length
StringgetPathRelativeTo(File fileChild, File fileRoot)
Returns that portion of the path of fileChild which is relative to the path of fileRoot.
if (fileChild.equals(fileRoot))
    return "";
String pathChild = fileChild.getPath();
String pathRoot = fileRoot.getPath();
if (!pathChild.startsWith(pathRoot))
    throw new IllegalArgumentException("fileChild = " + pathChild
StringgetRelativeFileName(File file, File basedir)
Gets a relative file from a filename against a base directory.
String canonicalFilePath = file.getCanonicalPath();
String canonicalBaseDirPath = basedir.getCanonicalPath();
if (canonicalFilePath.startsWith(canonicalBaseDirPath)) {
    int length = canonicalBaseDirPath.length();
    if (length < canonicalFilePath.length()) {
        return canonicalFilePath.substring(length + 1);
return null;
StringgetRelativePath(File parent, File file)
get Relative Path
return file.getAbsolutePath().substring(
        parent.getAbsolutePath().length() + 1);
StringgetWindowsRelativePath(String basedir, String path)
get Windows Relative Path
return getRelativePath(basedir, path, true);
StringrelativePathFromBase(File file, File basedir)
relative Path From Base
String absolutePath = basedir.getAbsolutePath();
if (file.getAbsolutePath().startsWith(absolutePath) == false) {
    throw new IllegalArgumentException("File not in basedir: "
            + file);
String relativePath = file.getAbsolutePath().substring(
return relativePath;
StringgetParentRelativeTo(File fileChild, File fileRoot)
Returns a String that represents that part of fileChild's parent directory path which is contained inside fileRoot (that is, fileChild's parent's path relative to fileRoot).
File parent = fileChild.getParentFile();
String pathParent = parent.getPath();
String pathRoot = fileRoot.getPath();
if (!pathParent.startsWith(pathRoot))
    throw new IllegalArgumentException("fileChild's parent = "