chRyNaN
chRyNaN

Reputation: 3692

How do you test if String is in URL format?

Okay, there must be an easy approach I'm missing here, but I'm drawing a blank. I have a custom ListView that gets some of its images from files on the external storage, some from the internet, and if neither of those it displays a PNG file from the drawable folder. For my logic, I need to be able to tell if a String (image location) is an URL type or File type.

So, how would I be able to tell if a String is in URL format or File format?

Upvotes: 2

Views: 1891

Answers (2)

fge
fge

Reputation: 121860

Use URI to try and parse your string:

try {
    new URI(theString);
} catch (URISyntaxException e) {
    // Certainly not an URL
}

You will have to make additional checks, such as checking this URI's scheme (or calling .toURL() on it and check that it does not throw a MalformedURLException).

Upvotes: 4

syb0rg
syb0rg

Reputation: 8247

A malformed URL will give you an exception. To know if you the URL is active or not you have to perform a handshake with the URL.

There is no other way that I know of.

Upvotes: 0

Related Questions