I am trying to display upload progress on my Android Client using a way mentioned below
MultipartEntity reqEntity = new MultipartEntity(){
@Override
public void writeTo(final OutputStream outstream) throws IOException {
super.writeTo(new UploadOutputStream(outstream,listener));
}
};
public class UploadOutputStream extends FilterOutputStream {
private final ProgressListener listener;
private long transferred;
public UploadOutputStream(final OutputStream out, final ProgressListener listener)
{
super(out);
this.listener = listener;
this.transferred = 0;
}
public void write(byte[] b, int off, int len) throws IOException
{
out.write(b, off, len);
this.transferred += len;
this.listener.transferred(this.transferred);
}
public void write(int b) throws IOException
{
out.write(b);
this.transferred++;
this.listener.transferred(this.transferred);
}
}
I am adding image to multipart ByteArrayBody as below
reqEntity.addPart("image", bab);
The problem is that I see the progress update only when the whole image has been uploaded. I want the progress when chunks are being uploaded.
Any help is greatly appreciated
Many Thanks
Manan
I don't think this is a problem with multipart encoding. Most likely, the HTTP client is optimizing the writing by sending a single write()
call, since it knows the size and it fits in its buffers. This call most likely blocks and your listener is executed immediately after that.
You can split that out.write(b, off, len)
call into multiple smaller calls, at the slight expense of speed.
Figured out the problem was because I was using ByteArrayBody that does NOT send the file in chunks instead writes the whole stream at once. Used FileBody to resolve the Issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.