Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android- how can I convert android.net.Uri object to java.net.URI object?

I am trying to get a FileInputStream object on an image that the user selects from the picture gallery. This is the android URI returned by android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI

content://media/external/images/media/3 

When I try to construct a java URI object from this object, I get an IllegalArgumentException with the exception description Expected file scheme in URI: content://media/external/images/media/3 whereas the android URI shows the scheme as content

Update: Never found a solution for the original question. But if you want the byte stream of an image in the pictures gallery, this piece of code will do that.

Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); ByteArrayInputStream fileInputStream = new ByteArrayInputStream(bytes.toByteArray()); 
like image 606
lostInTransit Avatar asked Feb 18 '09 05:02

lostInTransit


People also ask

What is the format of Uri in Android?

Represents a Uniform Resource Identifier (URI) reference. Aside from some minor deviations noted below, an instance of this class represents a URI reference as defined by RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, amended by RFC 2732: Format for Literal IPv6 Addresses in URLs.

What is Uri parse in android?

It is an immutable one-to-one mapping to a resource or data. The method Uri. parse creates a new Uri object from a properly formated String .


2 Answers

You could use the toString method of the android Uri in combination of the String based constructor of the Java URI.

android.net.Uri auri = new android.net.Uri(what ever); java.net.URI juri = new java.net.URI(auri.toString()); 

Android URI | Java URI

like image 189
Brian Gianforcaro Avatar answered Sep 19 '22 13:09

Brian Gianforcaro


Found the correct way to open InputStream from content URI:

InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri); 

That's all!

like image 22
Fedor Avatar answered Sep 22 '22 13:09

Fedor