Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Uri.Builder and not have the "//" part?

Tags:

android

uri

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?

like image 239
BoD Avatar asked Dec 16 '11 13:12

BoD


1 Answers

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.

like image 96
sschuberth Avatar answered Oct 11 '22 10:10

sschuberth