package com.github.monotonewang; import android.os.Handler; import android.os.Looper; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import okhttp3.MediaType; import okhttp3.RequestBody; import okio.BufferedSink; public class ProgressRequestBody extends RequestBody { private File mFile; private String mPath; private UploadCallbacks mListener; //每个RequestBody对应一个tag,存放在map中,保证计算的时候不会出现重复 private String tag; //用于第二次实际写入的数据判断 显示progressListener private boolean mIsSecond; private static final int DEFAULT_BUFFER_SIZE = 2048; public interface UploadCallbacks { void onProgressUpdate(int percentage); void onError(); void onFinish(); } public ProgressRequestBody(final File file, final UploadCallbacks listener, String tag) { mFile = file; mListener = listener; this.tag = tag; } @Override public MediaType contentType() { // i want to upload only images return MediaType.parse("image/*"); } @Override public long contentLength() throws IOException { return mFile.length(); } @Override public void writeTo(BufferedSink sink) throws IOException { System.out.println("---------------" + " " + "writeTo"); long fileLength = mFile.length(); System.out.println("---------------" + " " + "writeTo" + "fileLength=" + fileLength); byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; FileInputStream in = new FileInputStream(mFile); long uploaded = 0; try { int read; Handler handler = new Handler(Looper.getMainLooper()); if (mIsSecond) // update progress on UI thread handler.post(new ProgressUpdater(0, uploaded, fileLength)); while ((read = in.read(buffer)) != -1) { if (mIsSecond) // update progress on UI thread handler.post(new ProgressUpdater(read, uploaded, fileLength)); System.out.println("---------------" + " " + "writeTo" + "uploaded=" + uploaded + "read=" + read); uploaded += read; sink.write(buffer, 0, read); } } finally { in.close(); } mIsSecond = true; } private class ProgressUpdater implements Runnable { private long mUploaded; private long mTotal; private long read; public ProgressUpdater(long read, long uploaded, long total) { this.read = read; mUploaded = uploaded; mTotal = total; } @Override public void run() { mListener.onProgressUpdate((int) (100 * (mUploaded + read) / mTotal)); } } }
https://stackoverflow.com/questions/33338181/is-it-possible-to-show-progress-bar-when-upload-image-via-retrofit-2?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+0
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+43
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+86
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+100
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+0
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+7
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+15
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+23
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+30
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+38
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+46
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+54
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+61
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+69
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+77
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+84
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+92
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+100
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+0
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+43
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+86
---------------xxxxxxxxxxxxxxxxxxonProgressUpdate+100