1

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?

learner
  • 11,490
  • 26
  • 97
  • 169
  • Sorry - why does it have to be apache-commons IOUtils? You can do this with an InputStream: http://stackoverflow.com/questions/2436385/android-getting-from-a-uri-to-an-inputstream-to-a-byte-array – ılǝ Mar 13 '14 at 02:32
  • I didn't want to roll out my own method and then spend too much time hunting down possible bugs. – learner Mar 13 '14 at 02:46

2 Answers2

1

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)

ılǝ
  • 3,440
  • 2
  • 33
  • 47
1

Apache commons works fine with the following:

context.getContentResolver().openInputStream(uri);
byte[] attachmentBites = IOUtils.toByteArray(in);
learner
  • 11,490
  • 26
  • 97
  • 169
  • Your question was how to use IOUtils.toByteArray with android.net.Uri . You don't need an InputStream. I think my answer is accurate and up to the question, please be scrupulous and accept/ upvote an answer – ılǝ Mar 17 '14 at 01:23
  • Thanks for the input. Given this answer uses much fewer lines, I accept it. I +1 your response. thanks. – learner Mar 17 '14 at 15:57