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
android retrofit file-upload progress-bar percentage


source to share


No one has answered this question yet

See similar questions:

34
Android Retrofit - onProgressUpdate to show progress notification
eleven
Is there a way to get the correct loading using HttpUrlConncetion

or similar:

964
Download file from Android and show progress in ProgressDialog
862
What are the "tools: context" in android layout files?
694
JQuery Ajax file upload
582
How to upload files to server using JSP / Servlet?
559
Android Network Libraries Comparison: OkHTTP, Retrofit and Volleyball
421
How to change the progress color of a step in Android
358
How does HTTP file download work?
139
File upload progress bar with jQuery
five
Session cookie is unstable on Android update
1
Get file upload progress using HttpPost in Android



All Articles
Loading...
X
Show
Funny
Dev
Pics