Modernizing file uploads with progress bar not working in android
In my application, I used Retrofit to upload files to the server. Now I want to show the percentage of file upload in the progress bar. I found a solution from this link .
But the progress bar fills up quickly for all files of different sizes. I think this progress is not the actual amount of data being uploaded to the server. Is there any other solution in Retrofit for this problem ?? I also checked with this .
Is this possible in Retrofit ??
Look at my code
this is a custom file
public class CountingTypedFile extends TypedFile {
private static final int BUFFER_SIZE = 4096;
private final ProgressListener listener;
public CountingTypedFile(String mimeType, File file, ProgressListener listener) {
super(mimeType, file);
this.listener = listener;
}
@Override
public void writeTo(OutputStream out) throws IOException {
byte[] buffer = new byte[BUFFER_SIZE];
FileInputStream in = new FileInputStream(super.file());
long total = 0;
try {
int read;
while ((read = in.read(buffer)) != -1) {
total += read;
out.flush();
this.listener.transferred(total);
out.write(buffer, 0, read);
}
} finally {
in.close();
}
}
}
This is a piece of my code executed in the dobackground AsyncTask
Uri myUri = Uri.parse(mImgUri);
File file = new File(AppUtils.getRealPathFromURI(context, myUri));
totalSize = file.length();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants.BASE_URL)
.setLogLevel(RestAdapter.LogLevel.BASIC)
.setClient(new OkClient(new OkHttpClient()))
.build();
RetrofitCommonService mRetrofitCommonService = restAdapter.create(RetrofitCommonService.class);
Log.d("TAG", "File is not null when converting from bitmap");
TypedFile fileToSend = new CountingTypedFile("image/png", file, new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});
ImageUploadResponse imageUploadResponse = mRetrofitCommonService.upLoadImage(new AppUtils(context).getPhone(),
AppUtils.getDeviceId(context), fileToSend);
This is my service "Retrofitting"
@Multipart
@POST("/user/asset")
@Headers("Accept:application/json")
ImageUploadResponse upLoadImage(@Header("mobile-number") String mPhone, @Header("uid") String imei,
@Part("file") TypedFile mBody);
+3
source to share
No one has answered this question yet
See similar questions:
or similar: