如何将 android.net.Uri 转换为 java.net.URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37945809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert android.net.Uri to java.net.URL?
提问by CaptainForge
Is there a way to convert from Uri
to URL
? I need this for a library I'm using, it onlyaccepts an URL
but I need to use an image on my device.
有没有办法从 转换Uri
为URL
?我需要这个用于我正在使用的库,它只接受一个,URL
但我需要在我的设备上使用一个图像。
采纳答案by CommonsWare
If the scheme of the Uri
is http
or https
, new URL(uri.toString())
should work.
如果 的方案Uri
是http
或https
,则new URL(uri.toString())
应该可以工作。
If the scheme of the Uri
is file
, that may still also work, though I get nervous.
如果 的方案Uri
是file
,那可能仍然有效,尽管我很紧张。
If the scheme of the Uri
is ftp
or jar
, that may still also work, though we will wonder why you have a Uri
with those schemes.
如果 的方案Uri
是ftp
或jar
,那可能仍然有效,但我们会想知道为什么你有Uri
这些方案。
If the scheme of the Uri
is anything else — such as content
— this will not work, and you need to find a better library.
如果 的方案Uri
是其他任何东西——比如content
——这将不起作用,你需要找到一个更好的库。
回答by Pramod Waghmare
Try this
尝试这个
android.net.URI auri = new android.net.URI("your url goes here");
java.net.URI juri = new java.net.URI(auri.toString());
For more refer:
更多请参考: