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