Yes its possible. You would have to create your own request body.
public class PausableRequestBody extends RequestBody {
private static final int BUFFER_SIZE = 2048;
private File mFile;
private long uploadedData;
public PausableRequestBody(final File file) {
mFile = file;
}
@Override
public MediaType contentType() {
return MediaType.parse("image/*");
}
@Override
public long contentLength() throws IOException {
return mFile.length();
}
@Override
public void writeTo(BufferedSink bs) throws IOException {
long fileLength = mFile.length();
byte[] buffer = new byte[BUFFER_SIZE];
FileInputStream in = new FileInputStream(mFile);
try {
int read;
while ((read = in.read(buffer)) != -1) {
this.uploadedData += read;
bs.write(buffer, 0, read);
}
} finally {
in.close();
}
}
public long getUploadedData() {
return uploadedData;
}
}
use append instead of write..
Use following Retrofit call
PausableRequestBody fileBody = new PausableRequestBody(file, this);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", file.getName(), fileBody);
Call<JsonObject> request = RetrofitClient.uploadImage(filepart);
request.enqueue(new Callback<JsonObject>{...});
to pause the request you would have to cancel the call
request.cancel()
to restart
use the same PausableRequestBody object with the same method mentioned above