27

I would like to use intent.setData(Uri uri) to pass data obtained from a URL. In order to do this, I need to be able to create a Uri from a URL (or from a byte[] I read from the URL, or a ByteArrayInputStream I create from the byte[], etc). However, I cannot figure out how this is supposed to be done.

So, is there anyway to create a Uri from data obtained from a URL without first writing the data to a local file?

tir38
  • 8,776
  • 6
  • 56
  • 93
ab11
  • 19,320
  • 39
  • 109
  • 199

6 Answers6

42

Use URL.toURI() (Android doc) method.

Example:

URL url = new URL("http://www.google.com"); //Some instantiated URL object
URI uri = url.toURI();

Make sure to handle relevant exception, such as URISyntaxException.

Buhake Sindi
  • 85,564
  • 27
  • 164
  • 223
13

I think your answer can be found from here..

Uri.Builder.build() works quite well with normal URLs, but it fails with port number support.

The easiest way that I discovered to make it support port numbers was to make it parse a given URL first then work with it.

Uri.Builder b = Uri.parse("http://www.yoursite.com:12345").buildUpon();

b.path("/path/to/something/");
b.appendQueryParameter("arg1", String.valueOf(42));

if (username != "") {
  b.appendQueryParameter("username", username);
}

String url = b.build().toString(); 

Source : http://twigstechtips.blogspot.com/2011/01/android-create-url-using.html

Buhake Sindi
  • 85,564
  • 27
  • 164
  • 223
Shalin
  • 404
  • 4
  • 16
  • 1
    Also, instead of hardcoding "http://...", you could use `yourUrlVariable.toString()` instead. – dmon Nov 23 '11 at 16:30
11

From How to create a Uri from a URL?

Uri uri =  Uri.parse( "http://www.facebook.com" );
Community
  • 1
  • 1
NathanH
  • 194
  • 1
  • 7
4

Note that in Android, Uri's are different from Java URI's. Here's how to avoid using hardcoded strings, and at the same time, create a Uri with just the path portion of the http URL string encoded to conform to RFC2396:

Sample Url String:

String thisUrl = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=[redacted]&mapid=value"

method:

private Uri.Builder builder;
public Uri getUriFromUrl(String thisUrl) {
    URL url = new URL(thisUrl);
    builder =  new Uri.Builder()
                            .scheme(url.getProtocol())
                            .authority(url.getAuthority())
                            .appendPath(url.getPath());
    return builder.build();
}

To handle query strings you will need to parse the url.getQuery() as described here and then feed that into builder. appendQueryParameter().

Community
  • 1
  • 1
Phileo99
  • 5,426
  • 2
  • 46
  • 52
2
try {
    uri = new URI(url.toString());
} catch (URISyntaxException e) {
}
0
URI uri = null;
URL url = null;

// Create a URI
try {
    uri = new URI("www.abc.com");
} catch (URISyntaxException e) {
}

// Convert an absolute URI to a URL
try {
    url = uri.toURL();
} catch (IllegalArgumentException e) {
    // URI was not absolute
} catch (MalformedURLException e) {
}

// Convert a URL to a URI
try {
    uri = new URI(url.toString());
} catch (URISyntaxException e) {
}
Sumit Singh
  • 24,095
  • 8
  • 74
  • 100