Question

I want to use the method

byte[] IOUtils.toByteArray(Uri uri)

from org.apache.commons.io.IOUtils. My Uri is a typical android uri of type android.net.Uri. Does anyone know how I may fix this? An intermediary might be getting the input stream from the uri. Does anyone know how to do that?

Was it helpful?

Solution 2

Apache commons works fine with the following:

context.getContentResolver().openInputStream(uri);
byte[] attachmentBites = IOUtils.toByteArray(in);

OTHER TIPS

The method uses a java.net.URI (ref: IOUtils javadoc), not an android.net.Uri.

So the example use would be something like:

android.net.URI auri = new android.net.URI(whatever);
java.net.URI juri = new java.net.URI(auri.toString());
try {
    byte[] output = IOUtils.toByteArray(juri);
} catch (IOException e) {
    e.printStackTrace();
}

(presuming you have already imported and included in your build path the org.apache.commons.io library)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top