如何从java.net.URL创建android.net.Uri? [英] How to create a android.net.Uri from a java.net.URL?
问题描述
我想使用intent.setData(Uri uri)
来传递从URL获取的数据。为了做到这一点,我需要能够从URL(或从我从URL读取的byte []或从我创建的 ByteArrayInputStream
创建一个Uri byte []
等)。但是,我无法弄清楚应该如何做到这一点。
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.
那么,无论如何从URL获取的数据创建Uri而不首先将数据写入本地文件?
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()
( Android 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
.
这篇关于如何从java.net.URL创建android.net.Uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!