Search code examples
androiduriandroid-file

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.


Solution

  • 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();