That's your problem. If you check the documentation for InputStream.read(byte, int, int) you'll notice it says:
length = fis.read(b, 0, 1024);
} while (length >= 1024);
So you shouldn't assume length will be 1024 as long as there is data avaiable. If we again look at the documentation for that method we'll see:
An attempt is made to read as many as len bytes, but a smaller number may be read, possibly zero
So what you need to do is change the last line of that segment to:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
} while (length != -1);