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

lostInTransit picture lostInTransit · Feb 18, 2009 · Viewed 60.6k times · Source

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());

Answer

Brian Gianforcaro picture Brian Gianforcaro · Feb 18, 2009

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