tom91136
tom91136

Reputation: 8972

android get URL path

I've got a string:

public://imageifarm/3600.jpg

How can I extract the

imageifarm/3600.jpg

Part out using android?

What I've tried so far:

URL drupalQuestionNodeImageURI = new URL("public://imageifarm/3600.jpg");
Log.d("TAG", drupalQuestionNodeImageURI.getPath());

but it throws this exception:

09-16 17:24:39.992: W/System.err(3763): java.net.MalformedURLException: Unknown protocol: public

How can I solve this?

I know I can use regular expressions but that seems to defeat the purpose of URL(URI) in this case.

Upvotes: 2

Views: 3935

Answers (3)

Borg8
Borg8

Reputation: 1662

If you want have to use URL class (when you image sits on Internet) you have to provide valid URL (that begins from valid URL prefix, like http://, https:// etc). In you case you should use Uri class. Uri object can point on files in your local file system. For example:

Uri.fromFile(new File("public://imageifarm/3600.jpg"));

Upvotes: 1

nandeesh
nandeesh

Reputation: 24820

You should use android.net.Uri

Uri mUri = Uri.parse(public://imageifarm/3600.jpg);    
String extract = mUri.getEncodedSchemeSpecificPart();

Upvotes: 2

user207421
user207421

Reputation: 311052

Use java.net.URI, not java.net.URL.

Upvotes: 1

Related Questions