tags:

views:

6102

answers:

4

Thanks for any help.

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

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());
+4  A: 

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

Brian Gianforcaro
Thanks. Tried that. It gives an exception (java.net.URI should have a valid scheme)
lostInTransit
A: 

Since the String constructing doesn't work have you tried just constructing it your self?

android.net.URI auri = new android.net.URI(what ever);
java.net.URI juri = new java.net.URI(auri.getSchema(),
                                     auri.getSchemaSpecificPart(),
                                     auri.getFragment());

You might also want to double check that your getting valid data out of Android URI class. The docs as listed in my other answer discuss how it does pretty much no error checking. If there is infact an error the class just spits out garbage anyway and doesn't throw any exceptions. Which could very likely be why the java class which does do validation is throwing an exception.

Brian Gianforcaro
Thanks Brian. The uri is returned by android itself. it contains the path of an image in the picture gallery of the phone. so can't do anything about the image. I'll try and post the uri in the questions
lostInTransit
A: 

Hi, i am having the same problem as discussed above:

FileInputStream (content://media/external/images/media/6);

file not found exception

any solution?

Do you just want to get the byte stream? posting a solution in the question if that helps
lostInTransit
This is not an answer
Casebash
+4  A: 

Found the correct way to open InputStream from content URI:

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

That's all!

Fedor