Given just a location as a string, is there a reliable way to determine if this is a local file (such as /mnt/sdcard/test.jpg) or a remote resource (such as http://www.xyz.com/test.jpg)?
Converting it to a Uri with Uri.parse doesn't seem to give me anything to indicate where the file is.
I don't really want to have to look for // in the string!
A local path is the path to a folder or file on your local computer (e.g. C:\Program Files\Sitebulb). A UNC path is the path to a folder or file on a network and contains the server name in the path (e.g. \\server01\sitebulb\path).
File. isDirectory() checks whether a file with the specified abstract path name is a directory or not. This method returns true if the file specified by the abstract path name is a directory and false otherwise.
You can also check with the android.webkit.URLUtil class 
URLUtil.isFileUrl(file) || URLUtil.isContentUrl(file)
or any other member function of the aforementioned class. Preceding it with validation is advised:
URLUtil.isValidUrl(file)
uri format is
<protocol>://<server:port>/<path>
local files have:
file:///mnt/...
or just
 /mnt
so if string starts with
\w+?://
and this is not file:// then this is url
I also had the same problem and tried to use Penkov Vladimir solution but it didn't work because the Uri had the schema of 'content' which is also not a remote resource.
I used the following code and it worked great.
List<Uri> urls = new ArrayList<>();
List<Uri> locals = new ArrayList<>();
for (Uri uri : uris) {
    if (uri.getScheme() != null && (uri.getScheme().equals("content") || uri.getScheme().equals("file"))) {
        locals.add(uri);
    } else {
        urls.add(uri);
    }
}
To avoid hard-coding:
import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test {
  public static void main( String args[] ) throws Exception {
    final String[] inputs = {
      "/tmp/file.txt",
      "http://www.stackoverflow.com",
      "file:///~/calendar",
      "mailto:[email protected]",
      "urn:isbn:096139210x",
      "gopher://host.com:70/path",
      "wais://host.com:210/path",
      "news:newsgroup",
      "nntp://host.com:119/newsgroup",
      "finger://[email protected]/",
      "ftp://user:[email protected]:2121/",
      "telnet://user:[email protected]",
      "//localhost/index.html"
    };
    for( final String input : inputs ) {
      System.out.println( "---------------------------------------------" );
      final String protocol = getProtocol( input );
      System.out.println( "protocol: " + protocol );
      if( "file".equalsIgnoreCase( protocol ) ) {
        System.out.println( "file    : " + input );
      }
      else {
        System.out.println( "not file: " + input );
      }
    }
  }
  /**
   * Returns the protocol for a given URI or filename.
   *
   * @param source Determine the protocol for this URI or filename.
   *
   * @return The protocol for the given source.
   */
  private static String getProtocol( final String source ) {
    assert source != null;
    String protocol = null;
    try {
      final URI uri = new URI( source );
      if( uri.isAbsolute() ) {
        protocol = uri.getScheme();
      }
      else {
        final URL url = new URL( source );
        protocol = url.getProtocol();
      }
    } catch( final Exception e ) {
      // Could be HTTP, HTTPS?
      if( source.startsWith( "//" ) ) {
        throw new IllegalArgumentException( "Relative context: " + source );
      }
      else {
        final File file = new File( source );
        protocol = getProtocol( file );
      }
    }
    return protocol;
  }
  /**
   * Returns the protocol for a given file.
   *
   * @param file Determine the protocol for this file.
   *
   * @return The protocol for the given file.
   */
  private static String getProtocol( final File file ) {
    String result;
    try {
      result = file.toURI().toURL().getProtocol();
    } catch( Exception e ) {
      result = "unknown";
    }
    return result;
  }
}
Output:
---------------------------------------------
protocol: file
file    : /tmp/file.txt
---------------------------------------------
protocol: http
not file: http://www.stackoverflow.com
---------------------------------------------
protocol: file
file    : file:///~/calendar
---------------------------------------------
protocol: mailto
not file: mailto:[email protected]
---------------------------------------------
protocol: urn
not file: urn:isbn:096139210x
---------------------------------------------
protocol: gopher
not file: gopher://host.com:70/path
---------------------------------------------
protocol: wais
not file: wais://host.com:210/path
---------------------------------------------
protocol: news
not file: news:newsgroup
---------------------------------------------
protocol: nntp
not file: nntp://host.com:119/newsgroup
---------------------------------------------
protocol: finger
not file: finger://[email protected]/
---------------------------------------------
protocol: ftp
not file: ftp://user:[email protected]:2121/
---------------------------------------------
protocol: telnet
not file: telnet://user:[email protected]
---------------------------------------------
Exception in thread "main" java.lang.IllegalArgumentException: Relative context: //localhost/index.html
    at Test.getProtocol(Test.java:67)
    at Test.main(Test.java:30)
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