0

Hi i want to display file upload progress in a progress dialog to the user but the progress dialog is showing only 0% and 100% ( Not displaying the actual progress). I'm using retrofit for file upload and using counting file.

This is the code i am using:

 new AsyncTask<String, Integer, Void>()
        {
            private ProgressListener listener;
            private long totalSizeValue;
            private ProgressDialog pDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(ManualAdd.this);
                pDialog.setMessage("Uploading...");
                pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            @Override
            protected Void doInBackground(String... params) {

                String realPath = MediaUtils.getRealPathFromURI(fileURI, ManualAdd.this);
                File file = new File(realPath);
                totalSizeValue = file.length();
                final String filename = MediaUtils.getFileName(fileURI, ManualAdd.this);


                listener = new ProgressListener() {
                    @Override
                    public void transferred(long num) {

                        publishProgress((int)(((float)num*100f)/(float)totalSizeValue));

                    }
                };



                final Attachment attachment = new Attachment();
                attachment.setFileUri(fileURI);

                final String filePath = realPath;

                FileUploadService service = ServiceGenerator.createService(FileUploadService.class, FileUploadService.BASE_URL,Activityname);

                Map<String,String> path = new HashMap<>();
                path.put("path","fileUpload");

                service.upload(path,new CountingTypedFile("image/"+MediaUtils.getFileExt(filename),file,listener), new Callback<Pk_Response>() {
                    @Override
                    public void success(Pk_Response pk_response, retrofit.client.Response response) {
// Success logic
                        pDialog.dismiss();
                    }
                    @Override
                    public void failure(RetrofitError error) {
                        Toast.makeText(activity,"Failed to upload attachments...",Toast.LENGTH_LONG).show();
                        pDialog.dismiss();
                    }
                });

                return null;
            }

            protected void onProgressUpdate(Integer... progress) {               
                pDialog.setProgress(progress[0]);
            }
        }.execute();

Please help me, Thanks in advance

EDIT: changed the publish progress math equation:

publishProgress((int) ((num / (float) totalSizeValue) * 100));
Manikanta
  • 3,421
  • 4
  • 30
  • 51
  • The (float) cast are unneccesary because when you multiply with 100f num will be casted to float anyways. This will cause totalSizeValue to be casted to float. Also what values do you pass to publishProgress? Are you sure they range from 0 - 100? Add a Log.v call or debug it. – davidgiga1993 Jun 09 '15 at 05:29
  • I will post the log in a minute. thanks for responding. – Manikanta Jun 09 '15 at 05:31
  • Check out : http://stackoverflow.com/questions/9008467/asynctask-publishprogress-not-updating-my-progressdialog – Haresh Chhelana Jun 09 '15 at 05:35
  • I was not able to get logs @davidgiga1993, so i debugged it an evaluated the values at each step, these are the values i am getting 17,54,100 pDialog.setProgress(progress[0]); is getting called with the values but the progress dialog is not updating – Manikanta Jun 09 '15 at 05:50
  • try this : http://stackoverflow.com/questions/23348812/android-retrofit-onprogressupdate-for-showing-progress-notification –  Jun 09 '15 at 06:10

0 Answers0