7

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.

Federico Taschin
  • 2,027
  • 3
  • 15
  • 28

1 Answers1

5

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();
luismiguelss
  • 155
  • 2
  • 16
aslamhossin
  • 1,217
  • 12
  • 22