android - Tracking progress of multipart file upload using OKHTTP -
i trying implement a progress bar indicate progress of multipart file upload.
i have read comment on answer - https://stackoverflow.com/a/24285633/1022454 have wrap sink passed requestbody , provide callback tracks bytes moved.
i have created custom requestbody , wrapped sink customsink class, through debugging can see bytes being written realbufferedsink ln 44 , custom sink write method run once, not allowing me track bytes moved.
private class customrequestbody extends requestbody { mediatype contenttype; byte[] content; private customrequestbody(final mediatype contenttype, final byte[] content) { this.contenttype = contenttype; this.content = content; } @override public mediatype contenttype() { return contenttype; } @override public long contentlength() { return content.length; } @override public void writeto(bufferedsink sink) throws ioexception { customsink customsink = new customsink(sink); customsink.write(content); } } private class customsink implements bufferedsink { private static final string tag = "custom_sink"; bufferedsink bufferedsink; private customsink(bufferedsink bufferedsink) { this.bufferedsink = bufferedsink; } @override public void write(buffer source, long bytecount) throws ioexception { log.d(tag, "source size: " + source.size() + " bytecount" + bytecount); bufferedsink.write(source, bytecount); } @override public void flush() throws ioexception { bufferedsink.flush(); } @override public timeout timeout() { return bufferedsink.timeout(); } @override public void close() throws ioexception { bufferedsink.close(); } @override public buffer buffer() { return bufferedsink.buffer(); } @override public bufferedsink write(bytestring bytestring) throws ioexception { return bufferedsink.write(bytestring); } @override public bufferedsink write(byte[] source) throws ioexception { return bufferedsink.write(source); } @override public bufferedsink write(byte[] source, int offset, int bytecount) throws ioexception { return bufferedsink.write(source, offset, bytecount); } @override public long writeall(source source) throws ioexception { return bufferedsink.writeall(source); } @override public bufferedsink writeutf8(string string) throws ioexception { return bufferedsink.writeutf8(string); } @override public bufferedsink writestring(string string, charset charset) throws ioexception { return bufferedsink.writestring(string, charset); } @override public bufferedsink writebyte(int b) throws ioexception { return bufferedsink.writebyte(b); } @override public bufferedsink writeshort(int s) throws ioexception { return bufferedsink.writeshort(s); } @override public bufferedsink writeshortle(int s) throws ioexception { return bufferedsink.writeshortle(s); } @override public bufferedsink writeint(int i) throws ioexception { return bufferedsink.writeint(i); } @override public bufferedsink writeintle(int i) throws ioexception { return bufferedsink.writeintle(i); } @override public bufferedsink writelong(long v) throws ioexception { return bufferedsink.writelong(v); } @override public bufferedsink writelongle(long v) throws ioexception { return bufferedsink.writelongle(v); } @override public bufferedsink emitcompletesegments() throws ioexception { return bufferedsink.emitcompletesegments(); } @override public outputstream outputstream() { return bufferedsink.outputstream(); } }
does have example of how go doing this?
you have create custom requestbody , override writeto method, , there have send files down sink in segments. important flush sink after each segment, otherwise progress bar fill without file being sent on network, because contents stay in sink (which acts buffer).
public class countingfilerequestbody extends requestbody { private static final int segment_size = 2048; // okio.segment.size private final file file; private final progresslistener listener; private final string contenttype; public countingfilerequestbody(file file, string contenttype, progresslistener listener) { this.file = file; this.contenttype = contenttype; this.listener = listener; } @override public long contentlength() { return file.length(); } @override public mediatype contenttype() { return mediatype.parse(contenttype); } @override public void writeto(bufferedsink sink) throws ioexception { source source = null; try { source = okio.source(file); long total = 0; long read; while ((read = source.read(sink.buffer(), segment_size)) != -1) { total += read; sink.flush(); this.listener.transferred(total); } } { util.closequietly(source); } } public interface progresslistener { void transferred(long num); } }
you can find complete implementation supports displaying progress in adapterview , cancelling uploads @ gist: https://gist.github.com/eduardb/dd2dc530afd37108e1ac
Comments
Post a Comment