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

I am trying to get a FileInputStream object for an image that the user selects from the image 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 create a Java URI object from this object, I get an IllegalArgumentException with a description of the expected Expected file scheme in the URI: content: // media / external / images / media / 3, while in the android URI the scheme is displayed as content

Update : I never found a solution to the original question. But if you need an image byte stream in the image gallery, this piece of code will do it.

 Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); ByteArrayInputStream fileInputStream = new ByteArrayInputStream(bytes.toByteArray()); 
+55
android uri fileinputstream android-bitmap
Feb 18 '09 at 5:05
source share
5 answers

You can use the android Uri toString method in conjunction with the String Java URI based constructor.

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

Android URI | Java URI

+50
Feb 18 '09 at 6:03
source share

Found the correct way to open an InputStream from a content URI:

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

What all!

+39
Apr 24 '09 at 6:03
source share

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

  • Get the real path to the file (see this code: Get the file name and path from the URI from the media star )

  • Get URIs using real path and constructor: URI (String uri)

If you need more information, see here:

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

+2
Jun 30 '11 at 11:45
source share

I voted for jgilrincon's answer. I can’t comment because of the low reputation, and there is additional information here - you can use FileHelper.java from the Apache Cordova project, it has the functions that you need to process files from Uri strings, taking into account also the mediator (and the folder with application folders)

In particular, this method provides an InputStream from Uri:

 public static InputStream getInputStreamFromUriString(String uriString, Activity cordova) 
+2
Sep 19 '13 at
source share

Since building a String does not work, have you tried just building it yourself?

 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 you are receiving valid data from the Android URI class. The documents listed in my other answer discuss how it hardly checks for errors. If there is an infact error, the class simply spills out garbage anyway and throws no exceptions. This could probably be the reason that the java class that is performing the check throws an exception.

0
Feb 19 '09 at 14:15
source share



All Articles