There's another problem too, which is that if the byte array is very
large (100MB+), which could easily happen with video files, Android is
going to throw an OutOfMemoryError. So there's a broader question: how
can I get from a URI to a byte array to add to a multipart message,
without crashing Android?

On Mar 13, 11:23 am, Anna PS <annapowellsm...@googlemail.com> wrote:
> Hi all
>
> I want to get from an Android Uri (of the kind returned by
> onActivityResult) to a simple byte array, in order to upload a file as
> part of a multipart message. Can anyone provide an example of how to
> get from a Uri to a byte[] array?
>
> My current code (below) is almost there, but falls down because I
> can't work out the LENGTH_OF_FILE from an InputStream. I'd have the
> same problem using ByteArrayOutputStream.
>
> I've been looking at the docs on Content 
> Providershttp://developer.android.com/guide/topics/providers/content-providers...
> and wondering if getBlob() is the method I need, but it says it only
> applies to small amounts of data (<50KB) and I need to handle larger
> files.
>
> thanks!
>
> Current code (doesn't work):
>
>         public static byte[] getBytesFromFile(ContentResolver cR, String
> fileUriString) throws IOException {
>                 Uri tempuri = Uri.parse(fileUriString);
>                 InputStream is = cR.openInputStream(tempuri);
>                 byte[] b3 = new byte[LENGTH_OF_FILE];
>                 is.read(b3);

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to