显示上传图片进度 Multipart

Show upload image progress Multipart

在片段 B 中,我有一个提交按钮,用于将图像提交到服务器。单击提交按钮时,它将 return 到片段 A。我使用 Multipart 将图像上传到服务器,过程是 运行 在 Thread 上,因此用户可以执行其他操作等待图片上传时的任务。

片段 B(单击提交按钮时)

 inner class SimpleThread() :Thread() {
            override fun run() {
                for (i in newList!!) {
                    val service = RetrofitFactory.makeRetrofitService()
                    GlobalScope.launch(Dispatchers.IO) {
                        val request2 = WebApi.createImage(
                            activity,File(i)
                        )
                    }
                }
            }

WebApi

suspend fun createImage(
        file: File?
    ): WebApiResponse.Image? {
        val image = file?.let {
            MultipartBody.Part.createFormData(
                "image",
                it.getName(),
                RequestBody.create(MediaType.parse("image/*"), it)
            )
        }

        return RetrofitFactory.apiCall(context) {
            RetrofitFactory.makeRetrofitService().submitImages(
              image
            )
        }
    }

RetrofitService

@Multipart
    @POST("create_image")
    fun submitImages(
        @Part image: MultipartBody.Part?
    ): Deferred<Response<WebApiResponse.Image>>

我可以将图片上传到服务器,但是如何在片段 A 上显示上传图片的进度?

感谢任何帮助。

自定义请求正文

    public class FileProgressRequestBody extends RequestBody {

        public interface ProgressListener {
            void transferred(int size);
        }
        private RequestBody mRequestBody;

        protected ProgressListener listener;

        public FileProgressRequestBody(File file, String contentType, ProgressListener listener) {
            this.mRequestBody = RequestBody.create(MediaType.parse(contentType), file);
            this.listener = listener;
        }

        @Override
        public long contentLength() throws IOException{
            return mRequestBody.contentLength();
        }

        @Override
        public MediaType contentType() {
            return mRequestBody.contentType();
        }

        @Override
        public void writeTo(@NonNull BufferedSink sink) throws IOException {
            if (sink instanceof Buffer) {
                // Log Interceptor
                mRequestBody.writeTo(sink);
                return;
            }
            CountingSink countingSink = new CountingSink(sink);
            BufferedSink bufferedSink = Okio.buffer(countingSink);
            mRequestBody.writeTo(bufferedSink);
            bufferedSink.flush();
        }

        protected final class CountingSink extends ForwardingSink {
            private long bytesWritten = 0;

            public CountingSink(Sink delegate) {
                super(delegate);
            }

            @Override
            public void write(@NonNull Buffer source, long byteCount) throws IOException {
                super.write(source, byteCount);
                bytesWritten += byteCount;
                if (listener != null) {
                    long length = contentLength();
                    if (length != 0) {
                        listener.transferred(MathUtils.clamp((int) (bytesWritten * 100 / length), 0, 100));
                    }
                }
            }
        }
}

使用MultipartBody.Part.createFormData("image", file.getName(), new FileProgressRequestBody(file, "image/*", youListener));

所以经过一些研究,我找到了两种适合你的方法

1- 如果您正在使用 RxJava,我建议您遵循此 link 中的说明和注意事项 display progress of multipart with retrofit using RxJava

2- 如果您不使用 RxJava 添加一个 ProgressResponseBody 到看起来像这样的改造构建器:

public class ProgressRequestBody extends RequestBody {
private File mFile;
private String mPath;
private UploadCallbacks mListener;
private String content_type;

private static final int DEFAULT_BUFFER_SIZE = 2048;

public interface UploadCallbacks {
    void onProgressUpdate(int percentage);
    void onError();
    void onFinish();
}

对于这个你应该使用

我想你可以使用 WorkManaget 或 IntenService 来显示进度。 您可以启动服务中的流程。该服务可以发送新事件。