I am trying to build a mailto: uri to send a mail using the GMail app.
I would like to use the android.net.Uri.Builder class to do this, but the resulting uri is in the form mailto://test@example.com, which makes the GMail app think the recipient is //test@example.com, instead of just test@example.com.
I ended up doing this:
String uriStr = uriBuilder.toString();
uriStr = uriStr.replaceAll("//", "");
final Uri uri = Uri.parse(uriStr);
but clearly, this is an ugly hack…
Is there no way to build the uri without the // part?
There are several issues here. While it is possible to get rid of the
//part, you will loose the query strings then. The main problem is that Uri.Builder won’t let you use queries with opaque URIs (an opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character, likemailto:URIs).That said, you should use
uriBuilder.opaquePart()instead ofuriBuilder.authority()because the latter implicitly sets your URI to hierarchical, i.e. non-opaque. This will get rid of the//, but you’re lacking the query part then, and you cannot set it, because any call touriBuilder.appendQueryParameter()also implies a hierarchical URI.Long story short, to construct an opaque
mailto:URI that includes queries, you’ll have to useinstead. Of course, the literal
titleandtextshould beUri.encode()ed.