3

In Fragment B, I have a submit button used to submit image to server. When submit button is clicked, it will return to Fragment A. I using Multipart to upload image to server, and the process is running on Thread so user can do other task while waiting image to upload.

Fragment B (when submit button is clicked)

 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>>

I am able to upload image to server, but how can I show a upload image progress on Fragment A ?

Any help is appreciated.

John Joe
  • 12,412
  • 16
  • 70
  • 135
  • 1
    This? https://stackoverflow.com/questions/33338181/is-it-possible-to-show-progress-bar-when-upload-image-via-retrofit-2 – Karan Modi Apr 01 '19 at 03:45

3 Answers3

1

so after just a little bit of research I found two ways for you

1- if you are using RxJava I would suggest to follow the instructions and cautions in this link display progress of multipart with retrofit using RxJava

2- if you are not using RxJava add a ProgressResponseBody to the retrofit builder that would look something like this:

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();
}

for this one you should use this answer

Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36
1

I guess you can use WorkManaget or IntenService for showing the progress. A sample to send via retrofit2 You can start the process in service. The service can send a new event.

Alexander
  • 511
  • 4
  • 14
-1

Custom RequestBody

    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));
                    }
                }
            }
        }
}

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

John Joe
  • 12,412
  • 16
  • 70
  • 135
fancyyou
  • 965
  • 6
  • 7