65

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());
Swati Garg
  • 995
  • 1
  • 10
  • 21
lostInTransit
  • 70,519
  • 61
  • 198
  • 274

5 Answers5

61

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

Jeankowkow
  • 814
  • 13
  • 33
Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
  • 4
    Thanks. Tried that. It gives an exception (java.net.URI should have a valid scheme) – lostInTransit Feb 19 '09 at 12:37
  • 14
    this is wrong and will fail if the URI has characters that need encoding (like space). android.net.Uri.toString() decodes encoded characters, and java.net.URI will throw an exception if you try to construct it on a non-encoded string. the correct solution `huri = new java.net.URI(URLEncoder.encode(auri.toString(), "UTF-8"));` – Jeffrey Blattman Mar 01 '13 at 18:45
  • @Jeffrey: Uri.toString() returns encoded characters on my Android 4.1.2 device – Rémy DAVID Mar 20 '13 at 15:34
  • 4
    @RémyDAVID it does not. i just tried it. for a `a.n.Uri` with a space in it, the space does *not* get encoded. try it yourself: `Uri.parse("content://foo.bar.com/0?file=my goodness").toString()`. you are right that the javadocs state the it returns an encoded string, but it doesn't say what sort of encoding is used. – Jeffrey Blattman Mar 20 '13 at 15:56
43

Found the correct way to open InputStream from content URI:

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

That's all!

Fedor
  • 43,261
  • 10
  • 79
  • 89
  • 2
    Thank you!!! so simple, I knew it was there somewhere. The accepted answer might work in some cases, but does not seem to be safe/functional in all cases. – plainjimbo Aug 19 '11 at 19:03
  • Thanks a lot!!!! Works as a charm when you need to get byte[] from an image picked in the built in gallery! – Alex Bush Feb 28 '12 at 06:02
  • This force closes my app and open Binder.class with no source attached. I am sending Contacts.CONTENT_VCF_URI – tasomaniac Oct 13 '12 at 23:54
3

There is a solution to your original question (convert Uri to URI):

  1. Get the real file path (look this code: Get filename and path from URI from mediastore)

  2. Get the URI using the real path and the constructor: URI(String uri)

If you need more details, look here:

How to delete a video recorded using an Intent with ACTION_VIDEO_CAPTURE?

Community
  • 1
  • 1
jgilrincon
  • 124
  • 1
  • 8
2

I voted for jgilrincon's answer. I can't comment due to low reputation, and here goes some additional info - you can use FileHelper.java from Apache Cordova project, it has functions that you need for file handling from Uri strings, considering mediastore as well (and app assets folder)

Particularly this method provides InputStream from Uri:

public static InputStream getInputStreamFromUriString(String uriString, Activity cordova)
Mixaz
  • 4,068
  • 1
  • 29
  • 55
0

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
  • 26,564
  • 11
  • 58
  • 77
  • 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 Feb 21 '09 at 06:42
  • I think you meant getScheme and getSchemeSpecificPart, right? – Piotr Jun 26 '14 at 20:36