从URI打开流(Open stream from uri)

2019-09-17 08:10发布

我得到了一个URI( java.net.URI ),如http://www.example.com 。 如何打开它作为Java的流?

难道我真的要使用URL类呢?

Answer 1:

你将不得不创建一个新的URL上的对象,然后打开流URL实例。 下面是一个例子。

try {

   URL url = uri.toURL(); //get URL from your uri object
   InputStream stream = url.openStream();

} catch (MalformedURLException e) {
   e.printStackTrace();
} catch (URISyntaxException e) {
   e.printStackTrace();
}catch (IOException e) {
   e.printStackTrace();
}


Answer 2:

URLConnection connection = uri.toURL().openConnection()

是的,你必须使用URL类以某种方式或其他。



Answer 3:

uri.toURL().openStream()uri.toURL().openConnection().getInputStream()



Answer 4:

您可以使用URLConnection阅读给定的URL数据。 - URLConnection的



Answer 5:

您应该使用ContentResolver的获得的InputStream:

InputStream is = getContentResolver().openInputStream(uri);

代码是活动对象范围内有效。



文章来源: Open stream from uri
标签: java uri