Federico Taschin
Federico Taschin

Reputation: 2195

Convert from URI to Uri and the other way around

what's the best way to convert an Uri (android) object to a URI (java.net) object and vice versa? I'm converting Uri to URI by using:

Uri androidUri;
URI netURI= new URI(androidUri.toString());

but I don't know if that's the best way to do it and I don't know how to reverse it.

Upvotes: 10

Views: 6599

Answers (1)

aslamhossin
aslamhossin

Reputation: 1277

Converting a URI instance to Uri

Option 1:

URI androidUri;
Uri newUri = Uri.parse(androidUri.toString());

Option 2

URI androidUri;
Uri newUri  = new Uri.Builder()
    .scheme(androidUri.getScheme())
    .encodedAuthority(androidUri.getRawAuthority())
    .encodedPath(androidUri.getRawPath())
    .query(androidUri.getRawQuery())
    .fragment(androidUri.getRawFragment())
    .build();

Upvotes: 7

Related Questions