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());
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.
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 .
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
Found the correct way to open InputStream from content URI:
InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri);
That's all!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With