我想通过use intent.setData(Uri uri)
来传递从网址获得的数据。为此,我需要能够从URL (或从URL读取的byte[],或从byte[]
创建的ByteArrayInputStream
等)创建Uri。然而,我不知道应该如何做到这一点。
那么,有没有办法在不先将数据写入本地文件的情况下,根据从URL获得的数据创建Uri呢?
发布于 2011-11-23 17:57:35
使用URL.toURI()
(Android doc)方法。
示例:
URL url = new URL("http://www.google.com"); //Some instantiated URL object
URI uri = url.toURI();
请确保处理相关异常,如URISyntaxException
。
发布于 2011-11-23 17:56:50
我想你的答案可以从这里找到..
Uri.Builder.build()
在普通URL上工作得很好,但在支持端口号时却失败了。
我发现让它支持端口号的最简单的方法是让它首先解析给定的URL,然后再使用它。
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();
来源:http://twigstechtips.blogspot.com/2011/01/android-create-url-using.html
发布于 2016-04-16 02:30:13
来自How to create a Uri from a URL?
Uri uri = Uri.parse( "http://www.facebook.com" );
https://stackoverflow.com/questions/8240139
复制相似问题