Is there a way to convert from Uri to URL? I need this for a library I'm using, it only accepts an URL but I need to use an image on my device.
Asked
Active
Viewed 2.9k times
21
Blackbelt
- 152,872
- 27
- 286
- 297
CaptainForge
- 1,305
- 5
- 21
- 44
-
3`new URL(uri.toString())` ? – Blackbelt Jun 21 '16 at 13:29
-
That works.... I'm sorry, I never thought that would work, I thought I had to do a method conversion. – CaptainForge Jun 21 '16 at 13:32
2 Answers
33
If the scheme of the Uri is http or https, new URL(uri.toString()) should work.
If the scheme of the Uri is file, that may still also work, though I get nervous.
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.
If the scheme of the Uri is anything else — such as content — this will not work, and you need to find a better library.
CommonsWare
- 954,112
- 185
- 2,315
- 2,367
7
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:
Pramod Waghmare
- 1,254
- 14
- 21
-
new android.net.Uri is abstract class, use android.net.Uri.parse("you url") – Bhavesh Apr 21 '20 at 05:33