Android Path Create getAbsolutePathFromResource(String resource)

Here you can find the source of getAbsolutePathFromResource(String resource)

Description

Returns the absolute path of the resource.

License

Open Source License

Parameter

Parameter Description
urlResource a parameter

Declaration

public static String getAbsolutePathFromResource(String resource) 

Method Source Code

//package com.java2s;
import java.net.URL;
import java.net.URLDecoder;

public class Main {
    /**/*from  w  w  w .jav  a  2s.c om*/
     * Returns the absolute path of the resource. All special characters like
     * spaces etc. are decoded properly.
     * 
     * @param class
     * @param urlResource
     * @return
     */
    public static String getAbsolutePathFromResource(String resource) {
        return getAbsolutePathFromResource(Object.class, resource);
    }

    /**
     * Returns the absolute path of the resource. All special characters like
     * spaces etc. are decoded properly.
     * 
     * @param urlResource
     * @return
     */
    public static String getAbsolutePathFromResource(Class reference,
            String resource) {
        URL urlResource = reference.getResource(resource);
        if (urlResource == null)
            return null;

        //    return URI.create(urlResource.toString()).getPath();
        //    problem: URI can't parse spaces in URL at all.

        String urlString = URLDecoder.decode(urlResource.toString());

        if (urlString.startsWith("jar:"))
            urlString = urlString.substring("jar:".length());
        if (urlString.startsWith("file:"))
            urlString = urlString.substring("file:".length());

        //linux: /path/dir
        //windows: /C:/path/dir , also weg mit lead /
        if (urlString.indexOf(':') == 2)
            urlString = urlString.substring(1);

        //other ideas?

        return urlString;
    }
}

Related

  1. fileDir(String filename)
  2. fileDir(String folder, String folderName)
  3. getAbsoluteFile(String filename, String basedir)
  4. getAbsolutePath(String strCurrenDir, String strFileName)
  5. getAbsolutePathFromResource(Class reference, String resource)
  6. getCanonicalPath(File file)
  7. getDirAndFullName(File f)
  8. getDirName(String dir)
  9. getDownloadPath()