如何从一个URL创建一个开放的?创建一个、URL

2023-09-06 02:13:22 作者:柠檬之夏

我想使用intent.setData(URI)通过从一个URL获得的数据。为了做到这一点,我需要能够从一个URL创建一个URI(或一个byte []我从URL中读取,或 ByteArrayInputStream的我从创建在字节[] 等)。不过,我想不通这是怎么应该做。

I would like to use intent.setData(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.

所以,反正是有创建从一个URL获得的数据的开放的我们无需首先将数据写入到本地文件?

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

推荐答案

使用URL.toURI() (安卓DOC )方法。

例如:

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

确认来处理相关的异常,如URISyntaxException.

Make sure to handle relevant exception, such as URISyntaxException.