views:

17

answers:

1

Suppose an app gets a content stream by being called with the android.intent.action.SEND filter. Also suppose the Uri and the stream is received like this:

Uri contentUri = (Uri) extras.get(Intent.EXTRA_STREAM);
InputStream in = getContentResolver().openInputStream(contentUri);

Now, how can I determine the total length of the content without reading the whole stream? Is it even possible? in.available() isn't necessarily accurate, so I guess I do have to read the stream until the end, first?

The content might be very large, like about 50MB or more. So, I'd rather only read it once.

A: 

You can't determine the amount of data in a stream without reading it; you can, however pass the length via some other extra if you are the source of input.

I would read into a ByteArrayOutputStream and then call toByteArray() to get the resultant byte array. You don't need to define the size in advance (although it's possibly an optimisation if you know it. In many cases you won't)

Pentium10