Here’s some quick code I am doing. (I removed some of it to make it more readable.) Essentially, I’m opening a file, and processing 3k chunks at a time. These get encoded to Base64 (4k chunk now) and uploaded via HTTP post. After each call to DataOutputStream.writeBytes() I also call DataOutputStream.flush() and then I update a progress bar based on how much has been sent.
File myImage = new File(somepath);
int bytesAvailable = myImage.length();
while (bytesAvailable > 0)
{
byte[] buffer = new byte[Math.min(12288, (int)bytesAvailable)];
bytesRead = fileInputStream.read(buffer, 0, Math.min(12288, (int)bytesAvailable));
if (bytesRead > 0)
{
s2 = Base64.encodeBytes(buffer);
bytesAvailable = fileInputStream.available();
dataStream.writeBytes(s2);
dataStream.flush();
// UPDATE THE PROGRESS BAR HERE
}
}
Now, when I run the application, the image is successfully uploaded each time. However if I watch the mobile connection indicator (up/down arrows), they do not light up while the progress bar moves. The bar will go from 0 to 100% over the course of 10 seconds or so for a 3MB image (far too fast for my 3G phone). Then at 100%, after this loop is done, the dataStream.close() method is called. This is when the data transmission starts (as indicated by the arrows). This sits for a few minutes before finishing.
Am I not understanding flush() correctly? Shouldn’t it force data to be transmitted?
It sounds to me like you’re adding 4k chunks into a data stream and then sending that data stream in a http post to a server. If this is the case, then your loop is essentially iterating over a memory copy, and the http transfer will happen after you finish with the data stream. The only way to know for sure how the upload is going will be to get your hands on the socket and its data stream. This looks quite difficult if your using DefaultHttpClientConnection and so on.