Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between Java.Net.Uri and Android.Net.Uri

I am trying to get a Uri from a file object like so:

File file = new File("/sdcard/MyFolder/MyFile.txt");
var androidUri = Android.Net.Uri.FromFile(file).ToString();
var javaUri = file.ToURI().ToString();

this returns the following values:

androidUri = "file:///sdcard/MyFolder/MyFile.txt"
javaUri = "file:/sdcard/MyFolder/MyFile.txt"

so my question is whats the difference between the Java.Net.Uri and Android.Net.Uri are these two values supposed to be different? When should they be used?

Update

I found the two documentation pages Java.Net.Uri and Android.Net.Uri and both say:

Builds and parses URI references which conform to RFC 2396.

Therefore surely this is a bug and they should return the same string?

like image 797
User1 Avatar asked Jul 22 '15 10:07

User1


People also ask

What is the difference between URI and URI in Android?

Uri . A URI reference includes a URI and a fragment, the component of the URI following a '#'. Builds and parses URI references which conform to RFC 2396. In the interest of performance, this class performs little to no validation.

What is Java URI Android?

A URI is a uniform resource identifier while a URL is a uniform resource locator.

What is Java Net URI?

What is URI? URI stands for Uniform Resource Identifier. A Uniform Resource Identifier is a sequence of characters used for identification of a particular resource. It enables for the interaction of the representation of the resource over the network using specific protocols.


2 Answers

The difference is that Android.Net.Uri is Google's own implementation of RFC 2396.

Android.Net.Uri is immutable, hence it is thread-safe. Their implementation is also, according to the comments in the source, more forgiving. So while Java.Net.Uri would throw an Exception you attempt to use a garbage Uri, the Android implementation would just return you a Uri with that garbage.

As far as I can tell, Android.Net.Uri will only throw NullPointerException and seemingly no other exceptions. While the Java.Net.Uri implementation will throw other exceptions such as URISyntaxException and IllegalArgumentException

Otherwise they seem very similar.

The Uri you get file:/sdcard/MyFolder/MyFile.txt is valid, and when throwing it through java.net.URI I get following:

java> String uri = "file:/sdcard/MyFolder/MyFile.txt";
java> import java.net.*
java> URI urr = new URI(uri);
java.net.URI urr = file:/sdcard/MyFolder/MyFile.txt
java> urr.getScheme();
java.lang.String res2 = "file"
java> urr.getPath();
java.lang.String res3 = "/sdcard/MyFolder/MyFile.txt"
like image 153
Cheesebaron Avatar answered Oct 23 '22 10:10

Cheesebaron


Here is one practical difference:

java.net.uri supports IPv4 and IPv6 addresses whereas android.net.uri only support IPv4.

like image 38
Daniel Avatar answered Oct 23 '22 09:10

Daniel