如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:49:29  来源:igfitidea点击:

How to convert android.net.Uri to java.net.URL?

javaandroid

提问by CaptainForge

Is there a way to convert from Urito URL? I need this for a library I'm using, it onlyaccepts an URLbut I need to use an image on my device.

有没有办法从 转换UriURL?我需要这个用于我正在使用的库,它只接受一个,URL但我需要在我的设备上使用一个图像。

采纳答案by CommonsWare

If the scheme of the Uriis httpor https, new URL(uri.toString())should work.

如果 的方案Urihttphttps,则new URL(uri.toString())应该可以工作。

If the scheme of the Uriis file, that may still also work, though I get nervous.

如果 的方案Urifile,那可能仍然有效,尽管我很紧张。

If the scheme of the Uriis ftpor jar, that may still also work, though we will wonder why you have a Uriwith those schemes.

如果 的方案Uriftpjar,那可能仍然有效,但我们会想知道为什么你有Uri这些方案。

If the scheme of the Uriis 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:

更多请参考:

Android Uri

安卓 Uri

Java URI

Java URI