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://[email protected]
, which makes the GMail app think the recipient is //[email protected]
, instead of just [email protected]
.
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, like mailto:
URIs).
That said, you should use uriBuilder.opaquePart()
instead of uriBuilder.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 to uriBuilder.appendQueryParameter()
also implies a hierarchical URI.
Long story short, to construct an opaque mailto:
URI that includes queries, you'll have to use
Uri uri = Uri.parse("mailto:[email protected]?subject=title&body=text");
instead. Of course, the literal title
and text
should be Uri.encode()
ed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With